KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > Base64


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import java.io.IOException JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.io.Writer JavaDoc;
8
9 /**
10  * One of the fastest implementation of the Base64 encoding.
11  * Used by IBM, Reuters etc. Jakarta and others are slower.
12  */

13 public class Base64 {
14     private static final char[] S_BASE64CHAR = {
15         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
16         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
17         'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
18         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
19         'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
20         'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
21         '8', '9', '+', '/'
22     };
23     private static final char S_BASE64PAD = '=';
24     private static final byte[] S_DECODETABLE = new byte[128];
25     static {
26         for (int i = 0; i < S_DECODETABLE.length; i ++)
27             S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
28
for (int i = 0; i < S_BASE64CHAR.length; i ++) // 0 to 63
29
S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
30     }
31
32     private static int decode0(char[] ibuf, byte[] obuf, int wp) {
33         int outlen = 3;
34         if (ibuf[3] == S_BASE64PAD) outlen = 2;
35         if (ibuf[2] == S_BASE64PAD) outlen = 1;
36         int b0 = S_DECODETABLE[ibuf[0]];
37         int b1 = S_DECODETABLE[ibuf[1]];
38         int b2 = S_DECODETABLE[ibuf[2]];
39         int b3 = S_DECODETABLE[ibuf[3]];
40         switch (outlen) {
41           case 1:
42             obuf[wp] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
43             return 1;
44           case 2:
45             obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
46             obuf[wp] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
47             return 2;
48           case 3:
49             obuf[wp++] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
50             obuf[wp++] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
51             obuf[wp] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
52             return 3;
53           default:
54             throw new RuntimeException JavaDoc("Internal Errror");
55         }
56     }
57     
58     /**
59      * Decode the base64 data.
60      * @param data The base64 encoded data to be decoded
61      * @param off The offset within the encoded data at which to start decoding
62      * @param len The length of data to decode
63      * @return The decoded data
64      */

65     public static byte[] decode(char[] data, int off, int len) {
66         char[] ibuf = new char[4];
67         int ibufcount = 0;
68         byte[] obuf = new byte[(len >> 2) *3+3];
69         int obufcount = 0;
70         for (int i = off; i < off+len; i ++) {
71             char ch = data[i];
72             if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
73                 ibuf[ibufcount++] = ch;
74                 if (ibufcount == ibuf.length) {
75                     ibufcount = 0;
76                     obufcount += decode0(ibuf, obuf, obufcount);
77                 }
78             }
79         }
80         if (obufcount == obuf.length)
81             return obuf;
82         byte[] ret = new byte[obufcount];
83         System.arraycopy(obuf, 0, ret, 0, obufcount);
84         return ret;
85     }
86
87     public static final int BUF_SIZE = 256;
88     /**
89      * Decode the base64 data.
90      * @param data The base64 encoded data to be decoded
91      * @return The decoded data
92      */

93     public static byte[] decode(String JavaDoc data) {
94         int ibufcount = 0;
95         int slen = data.length();
96         char[] ibuf = new char[slen < BUF_SIZE +3 ? slen : BUF_SIZE + 3];
97         byte[] obuf = new byte[(slen >> 2) *3+3];
98         int obufcount = 0;
99         int blen;
100     
101         for (int i = 0; i < slen; i +=BUF_SIZE ) {
102             // buffer may contain unprocessed characters from previous step
103
if (i + BUF_SIZE <= slen) {
104                 data.getChars(i, i+BUF_SIZE , ibuf, ibufcount);
105                 blen = BUF_SIZE + ibufcount;
106             } else {
107                 data.getChars(i, slen, ibuf, ibufcount);
108                 blen = slen - i+ibufcount;
109             }
110     
111             for (int j=ibufcount; j<blen; j++) {
112                 char ch = ibuf[j];
113                 if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
114                     ibuf[ibufcount++] = ch;
115                     // as soon as we have 4 chars process them
116
if (ibufcount == 4) {
117                         ibufcount = 0;
118                         obufcount += decode0(ibuf, obuf, obufcount);
119                     }
120                 }
121             }
122         }
123         if (obufcount == obuf.length) {
124             return obuf;
125         }
126         byte[] ret = new byte[obufcount];
127         System.arraycopy(obuf, 0, ret, 0, obufcount);
128         return ret;
129     }
130
131     /**
132      * Decode the base64 data.
133      * @param data The base64 encoded data to be decoded
134      * @param off The offset within the encoded data at which to start decoding
135      * @param len The length of data to decode
136      * @param ostream The OutputStream to which the decoded data should be
137      * written
138      */

139     public static void decode(char[] data, int off, int len, OutputStream JavaDoc ostream) throws IOException JavaDoc {
140         char[] ibuf = new char[4];
141         int ibufcount = 0;
142         byte[] obuf = new byte[3];
143         for (int i = off; i < off+len; i ++) {
144             char ch = data[i];
145             if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
146                 ibuf[ibufcount++] = ch;
147                 if (ibufcount == ibuf.length) {
148                     ibufcount = 0;
149                     int obufcount = decode0(ibuf, obuf, 0);
150                     ostream.write(obuf, 0, obufcount);
151                 }
152             }
153         }
154     }
155     
156     /**
157      * Decode the base64 data.
158      * @param data The base64 encoded data to be decoded
159      * @param ostream The OutputStream to which the decoded data should be
160      * written
161      */

162     public static void decode(String JavaDoc data, OutputStream JavaDoc ostream) throws IOException JavaDoc {
163         char[] ibuf = new char[BUF_SIZE + 4];
164         byte[] obuf = new byte[3];
165         int slen = data.length();
166         int blen;
167         int ibufcount = 0;
168     
169         for (int i = 0; i < slen; i +=BUF_SIZE ) {
170             // buffer may contain unprocessed characters from previous step
171
if (i + BUF_SIZE <= slen) {
172                 data.getChars(i, i + BUF_SIZE , ibuf, ibufcount);
173                 blen = BUF_SIZE+ibufcount;
174             } else {
175                 data.getChars(i, slen, ibuf, ibufcount);
176                 blen = slen - i+ibufcount;
177             }
178     
179             for (int j=ibufcount; j<blen; j++) {
180                 char ch = ibuf[j];
181                 if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
182                     ibuf[ibufcount++] = ch;
183                     
184                     // as sson as we have 4 chars process them
185
if (ibufcount == 4) {
186                         ibufcount = 0;
187                         int obufcount = decode0(ibuf, obuf, 0);
188                         ostream.write(obuf, 0, obufcount);
189                     }
190                 }
191             }
192         }
193     }
194
195     /**
196      * Returns base64 representation of specified byte array.
197      * @param data The data to be encoded
198      * @return The base64 encoded data
199      */

200     public static String JavaDoc encode(byte[] data) {
201         return encode(data, 0, data.length);
202     }
203
204     public static String JavaDoc encode(String JavaDoc s) {
205         return encode(s.getBytes(), 0, s.length());
206     }
207
208     /**
209      * Returns base64 representation of specified byte array.
210      * @param data The data to be encoded
211      * @param off The offset within the data at which to start encoding
212      * @param len The length of the data to encode
213      * @return The base64 encoded data
214      */

215     public static String JavaDoc encode(byte[] data, int off, int len) {
216         if (len <= 0) return "";
217         char[] out = new char[(len / 3 << 2) +4];
218         int rindex = off;
219         int windex = 0;
220         int rest = len;
221         while (rest >= 3) {
222             int i = ((data[rindex]&0xff)<<16)
223                 +((data[rindex+1]&0xff)<<8)
224                 +(data[rindex+2]&0xff);
225             out[windex++] = S_BASE64CHAR[i>>18];
226             out[windex++] = S_BASE64CHAR[(i>>12)&0x3f];
227             out[windex++] = S_BASE64CHAR[(i>>6)&0x3f];
228             out[windex++] = S_BASE64CHAR[i&0x3f];
229             rindex += 3;
230             rest -= 3;
231         }
232         if (rest == 1) {
233             int i = data[rindex]&0xff;
234             out[windex++] = S_BASE64CHAR[i>>2];
235             out[windex++] = S_BASE64CHAR[(i<<4)&0x3f];
236             out[windex++] = S_BASE64PAD;
237             out[windex++] = S_BASE64PAD;
238         } else if (rest == 2) {
239             int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
240             out[windex++] = S_BASE64CHAR[i>>10];
241             out[windex++] = S_BASE64CHAR[(i>>4)&0x3f];
242             out[windex++] = S_BASE64CHAR[(i<<2)&0x3f];
243             out[windex++] = S_BASE64PAD;
244         }
245         return new String JavaDoc(out, 0, windex);
246     }
247
248     /**
249      * Outputs base64 representation of the specified byte array to a byte stream.
250      * @param data The data to be encoded
251      * @param off The offset within the data at which to start encoding
252      * @param len The length of the data to encode
253      * @param ostream The OutputStream to which the encoded data should be
254      * written
255      */

256     public static void encode(byte[] data, int off, int len, OutputStream JavaDoc ostream) throws IOException JavaDoc {
257         if (len <= 0) return;
258         byte[] out = new byte[4];
259         int rindex = off;
260         int rest = len;
261         while (rest >= 3) {
262             int i = ((data[rindex]&0xff)<<16)
263                 +((data[rindex+1]&0xff)<<8)
264                 +(data[rindex+2]&0xff);
265             out[0] = (byte)S_BASE64CHAR[i>>18];
266             out[1] = (byte)S_BASE64CHAR[(i>>12)&0x3f];
267             out[2] = (byte)S_BASE64CHAR[(i>>6)&0x3f];
268             out[3] = (byte)S_BASE64CHAR[i&0x3f];
269             ostream.write(out, 0, 4);
270             rindex += 3;
271             rest -= 3;
272         }
273         if (rest == 1) {
274             int i = data[rindex]&0xff;
275             out[0] = (byte)S_BASE64CHAR[i>>2];
276             out[1] = (byte)S_BASE64CHAR[(i<<4)&0x3f];
277             out[2] = (byte)S_BASE64PAD;
278             out[3] = (byte)S_BASE64PAD;
279             ostream.write(out, 0, 4);
280         } else if (rest == 2) {
281             int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
282             out[0] = (byte)S_BASE64CHAR[i>>10];
283             out[1] = (byte)S_BASE64CHAR[(i>>4)&0x3f];
284             out[2] = (byte)S_BASE64CHAR[(i<<2)&0x3f];
285             out[3] = (byte)S_BASE64PAD;
286             ostream.write(out, 0, 4);
287         }
288     }
289     
290     /**
291      * Outputs base64 representation of the specified byte array to a character stream.
292      * @param data The data to be encoded
293      * @param off The offset within the data at which to start encoding
294      * @param len The length of the data to encode
295      * @param writer The Writer to which the encoded data should be
296      * written
297      */

298     public static void encode(byte[] data, int off, int len, Writer JavaDoc writer) throws IOException JavaDoc {
299         if (len <= 0) return;
300         char[] out = new char[4];
301         int rindex = off;
302         int rest = len;
303         int output = 0;
304         while (rest >= 3) {
305             int i = ((data[rindex]&0xff)<<16) +((data[rindex+1]&0xff)<<8) +(data[rindex+2]&0xff);
306             out[0] = S_BASE64CHAR[i>>18];
307             out[1] = S_BASE64CHAR[(i>>12)&0x3f];
308             out[2] = S_BASE64CHAR[(i>>6)&0x3f];
309             out[3] = S_BASE64CHAR[i&0x3f];
310             writer.write(out, 0, 4);
311             rindex += 3;
312             rest -= 3;
313             output += 4;
314             if (output % 76 == 0)
315                 writer.write("\n");
316         }
317         if (rest == 1) {
318             int i = data[rindex]&0xff;
319             out[0] = S_BASE64CHAR[i>>2];
320             out[1] = S_BASE64CHAR[(i<<4)&0x3f];
321             out[2] = S_BASE64PAD;
322             out[3] = S_BASE64PAD;
323             writer.write(out, 0, 4);
324         } else if (rest == 2) {
325             int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
326             out[0] = S_BASE64CHAR[i>>10];
327             out[1] = S_BASE64CHAR[(i>>4)&0x3f];
328             out[2] = S_BASE64CHAR[(i<<2)&0x3f];
329             out[3] = S_BASE64PAD;
330             writer.write(out, 0, 4);
331         }
332     }
333 }
334
335
Popular Tags