@@ -56,21 +56,62 @@ public class Base64 implements BinaryEncoder, BinaryDecoder {
5656 *
5757 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>
5858 */
59- static final byte [] CHUNK_SEPARATOR = "\r \n " .getBytes ();
59+ static final byte [] CHUNK_SEPARATOR = {'\r' ,'\n' };
60+
61+ /**
62+ * This array is a lookup table that translates 6-bit positive integer
63+ * index values into their "Base64 Alphabet" equivalents as specified
64+ * in Table 1 of RFC 2045.
65+ *
66+ * Thanks to "commons" project in ws.apache.org for this code.
67+ * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
68+ */
69+ private static final byte [] intToBase64 = {
70+ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
71+ 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
72+ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' ,
73+ 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
74+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/'
75+ };
6076
6177 /**
6278 * Byte used to pad output.
6379 */
6480 private static final byte PAD = '=' ;
6581
82+ /**
83+ * This array is a lookup table that translates unicode characters
84+ * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
85+ * into their 6-bit positive integer equivalents. Characters that
86+ * are not in the Base64 alphabet but fall within the bounds of the
87+ * array are translated to -1.
88+ *
89+ * Thanks to "commons" project in ws.apache.org for this code.
90+ * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
91+ */
92+ private static final byte [] base64ToInt = {
93+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
94+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
95+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 , 52 , 53 , 54 ,
96+ 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 ,
97+ 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ,
98+ 24 , 25 , -1 , -1 , -1 , -1 , -1 , -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 ,
99+ 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51
100+ };
101+
102+ /** Mask used to extract 6 bits, used when encoding */
103+ private static final int MASK_6BITS = 0x3f ;
104+
105+ /** Mask used to extract 8 bits, used in decoding base64 bytes */
106+ private static final int MASK_8BITS = 0xff ;
66107
67108 // The static final fields above are used for the original static byte[] methods on Base64.
68109 // The private member fields below are used with the new streaming approach, which requires
69110 // some state be preserved between calls of encode() and decode().
70111
71112
72113 /**
73- * Line length for encoding. Not used when decoding. Any value of zero or less implies
114+ * Line length for encoding. Not used when decoding. A value of zero or less implies
74115 * no chunking of the base64 encoded data.
75116 */
76117 private final int lineLength ;
@@ -198,42 +239,6 @@ public Base64(int lineLength, byte[] lineSeparator) {
198239 }
199240 }
200241
201- /**
202- * This array is a lookup table that translates 6-bit positive integer
203- * index values into their "Base64 Alphabet" equivalents as specified
204- * in Table 1 of RFC 2045.
205- *
206- * Thanks to "commons" project in ws.apache.org for this code.
207- * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
208- */
209- private static final byte [] intToBase64 = {
210- 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' ,
211- 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ,
212- 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' ,
213- 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
214- '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/'
215- };
216-
217- /**
218- * This array is a lookup table that translates unicode characters
219- * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
220- * into their 6-bit positive integer equivalents. Characters that
221- * are not in the Base64 alphabet but fall within the bounds of the
222- * array are translated to -1.
223- *
224- * Thanks to "commons" project in ws.apache.org for this code.
225- * http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
226- */
227- private static final byte [] base64ToInt = {
228- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
229- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
230- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 , 52 , 53 , 54 ,
231- 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 ,
232- 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ,
233- 24 , 25 , -1 , -1 , -1 , -1 , -1 , -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 ,
234- 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51
235- };
236-
237242 /**
238243 * Returns true if this Base64 object has buffered data for reading.
239244 *
@@ -342,16 +347,16 @@ void encode(byte[] in, int inPos, int inAvail) {
342347 }
343348 switch (modulus ) {
344349 case 1 :
345- buf [pos ++] = intToBase64 [(x >> 2 ) & 0x3f ];
346- buf [pos ++] = intToBase64 [(x << 4 ) & 0x3f ];
350+ buf [pos ++] = intToBase64 [(x >> 2 ) & MASK_6BITS ];
351+ buf [pos ++] = intToBase64 [(x << 4 ) & MASK_6BITS ];
347352 buf [pos ++] = PAD ;
348353 buf [pos ++] = PAD ;
349354 break ;
350355
351356 case 2 :
352- buf [pos ++] = intToBase64 [(x >> 10 ) & 0x3f ];
353- buf [pos ++] = intToBase64 [(x >> 4 ) & 0x3f ];
354- buf [pos ++] = intToBase64 [(x << 2 ) & 0x3f ];
357+ buf [pos ++] = intToBase64 [(x >> 10 ) & MASK_6BITS ];
358+ buf [pos ++] = intToBase64 [(x >> 4 ) & MASK_6BITS ];
359+ buf [pos ++] = intToBase64 [(x << 2 ) & MASK_6BITS ];
355360 buf [pos ++] = PAD ;
356361 break ;
357362 }
@@ -369,10 +374,10 @@ void encode(byte[] in, int inPos, int inAvail) {
369374 if (b < 0 ) { b += 256 ; }
370375 x = (x << 8 ) + b ;
371376 if (0 == modulus ) {
372- buf [pos ++] = intToBase64 [(x >> 18 ) & 0x3f ];
373- buf [pos ++] = intToBase64 [(x >> 12 ) & 0x3f ];
374- buf [pos ++] = intToBase64 [(x >> 6 ) & 0x3f ];
375- buf [pos ++] = intToBase64 [x & 0x3f ];
377+ buf [pos ++] = intToBase64 [(x >> 18 ) & MASK_6BITS ];
378+ buf [pos ++] = intToBase64 [(x >> 12 ) & MASK_6BITS ];
379+ buf [pos ++] = intToBase64 [(x >> 6 ) & MASK_6BITS ];
380+ buf [pos ++] = intToBase64 [x & MASK_6BITS ];
376381 currentLinePos += 4 ;
377382 if (lineLength > 0 && lineLength <= currentLinePos ) {
378383 System .arraycopy (lineSeparator , 0 , buf , pos , lineSeparator .length );
@@ -424,9 +429,9 @@ void decode(byte[] in, int inPos, int inAvail) {
424429 case 3 :
425430 x = x << 6 ;
426431 case 0 :
427- buf [pos ++] = (byte ) ((x >> 16 ) & 0xff );
432+ buf [pos ++] = (byte ) ((x >> 16 ) & MASK_8BITS );
428433 if (modulus == 0 ) {
429- buf [pos ++] = (byte ) ((x >> 8 ) & 0xff );
434+ buf [pos ++] = (byte ) ((x >> 8 ) & MASK_8BITS );
430435 }
431436 default :
432437 // WE'RE DONE!!!!
@@ -440,9 +445,9 @@ void decode(byte[] in, int inPos, int inAvail) {
440445 modulus = (++modulus ) % 4 ;
441446 x = (x << 6 ) + result ;
442447 if (modulus == 0 ) {
443- buf [pos ++] = (byte ) ((x >> 16 ) & 0xff );
444- buf [pos ++] = (byte ) ((x >> 8 ) & 0xff );
445- buf [pos ++] = (byte ) (x & 0xff );
448+ buf [pos ++] = (byte ) ((x >> 16 ) & MASK_8BITS );
449+ buf [pos ++] = (byte ) ((x >> 8 ) & MASK_8BITS );
450+ buf [pos ++] = (byte ) (x & MASK_8BITS );
446451 }
447452 }
448453 }
0 commit comments