11/*
2- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//codec/src/java/org/apache/commons/codec/base64/Attic/Base64.java,v 1.1 2003/04/25 17:50:56 tobrien Exp $
3- * $Revision: 1.1 $
4- * $Date: 2003/04/25 17:50:56 $
2+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//codec/src/java/org/apache/commons/codec/base64/Attic/Base64.java,v 1.2 2003/05/29 23:03:28 tobrien Exp $
3+ * $Revision: 1.2 $
4+ * $Date: 2003/05/29 23:03:28 $
55 * ====================================================================
66 *
77 * The Apache Software License, Version 1.1
7676 * 1996. Available at: http://www.ietf.org/rfc/rfc2045.txt
7777 * </p>
7878 * @author Jeffrey Rodriguez
79- * @version $Revision: 1.1 $ $Date: 2003/04/25 17:50:56 $
79+ * @version $Revision: 1.2 $ $Date: 2003/05/29 23:03:28 $
8080 *
8181 * @deprecated This class has been replaced by
8282 * {@link org.apache.commons.codec.binary.Base64}
8383 */
8484public final class Base64 {
8585
8686 protected static final String DEFAULT_CHAR_ENCODING = "ISO-8859-1" ;
87- private static final int BASELENGTH = 255 ;
88- private static final int LOOKUPLENGTH = 64 ;
89- private static final int TWENTYFOURBITGROUP = 24 ;
90- private static final int EIGHTBIT = 8 ;
91- private static final int SIXTEENBIT = 16 ;
92- private static final int SIXBIT = 6 ;
93- private static final int FOURBYTE = 4 ;
94- private static final int SIGN = -128 ;
95- private static final byte PAD = (byte ) '=' ;
96- private static final byte [] EMPTY_BYTE_ARRAY = new byte [0 ];
87+
88+ /**
89+ * The bsae length
90+ */
91+ static final int BASELENGTH = 255 ;
92+
93+ /**
94+ * Lookup length
95+ */
96+ static final int LOOKUPLENGTH = 64 ;
97+
98+ /**
99+ * Used to calculate the number of bits in a byte.
100+ */
101+ static final int EIGHTBIT = 8 ;
102+
103+ /**
104+ * Used when encoding something which has fewer than 24 bits
105+ */
106+ static final int SIXTEENBIT = 16 ;
107+
108+ /**
109+ * Constant used to determine how many bits data contains
110+ */
111+ static final int TWENTYFOURBITGROUP = 24 ;
112+
113+ /**
114+ * Used to get the number of Quadruples
115+ */
116+ static final int FOURBYTE = 4 ;
117+
118+ /**
119+ * Used to test the sign of a byte
120+ */
121+ static final int SIGN = -128 ;
122+
123+ /**
124+ * Byte used to pad output
125+ */
126+ static final byte PAD = (byte ) '=' ;
127+
128+ // Create arrays to hold the base64 characters and a
129+ // lookup for base64 chars
97130 private static byte [] base64Alphabet = new byte [BASELENGTH ];
131+
132+ private static final byte [] EMPTY_BYTE_ARRAY = new byte [0 ];
133+
134+ /**
135+ * Lookup table
136+ */
98137 private static byte [] lookUpBase64Alphabet = new byte [LOOKUPLENGTH ];
99138
100139 static {
@@ -133,17 +172,37 @@ public final class Base64 {
133172
134173 }
135174
175+ /**
176+ * Tests to see whether the bytes of this string are
177+ * Base64
178+ *
179+ * @param isValidString String to test
180+ * @return trus if String is base64
181+ */
136182 public static boolean isBase64 (String isValidString ) {
137183 return (isBase64 (isValidString .getBytes ()));
138184 }
139185
140-
186+ /**
187+ * Tests a byte to see whether it falls within the Base64
188+ * alphabet (or if it is a padding character).
189+ *
190+ * @param octect byte to test
191+ * @return true if byte is in alphabet or padding
192+ */
141193 public static boolean isBase64 (byte octect ) {
142194 // Should we ignore white space?
143195 return (octect == PAD || base64Alphabet [octect ] != -1 );
144196 }
145197
146-
198+ /**
199+ * Tests byte array to see if all characters are within the
200+ * Base64 alphabet
201+ *
202+ * @param arrayOctect A byte[] to test
203+ * @return true if all data falls within the Base64 alphabet OR if the
204+ * array is empty.
205+ */
147206 public static boolean isBase64 (byte [] arrayOctect ) {
148207 int length = arrayOctect .length ;
149208 if (length == 0 ) {
@@ -177,8 +236,7 @@ public static byte[] encode(byte[] binaryData) {
177236 if (fewerThan24bits != 0 ) {
178237 //data not divisible by 24 bit
179238 encodedData = new byte [(numberTriplets + 1 ) * 4 ];
180- }
181- else {
239+ } else {
182240 // 16 or 8 bit
183241 encodedData = new byte [numberTriplets * 4 ];
184242 }
@@ -212,8 +270,10 @@ public static byte[] encode(byte[] binaryData) {
212270 : (byte ) ((b3 ) >> 6 ^ 0xfc );
213271
214272 encodedData [encodedIndex ] = lookUpBase64Alphabet [val1 ];
215- encodedData [encodedIndex + 1 ] = lookUpBase64Alphabet [val2 | (k << 4 )];
216- encodedData [encodedIndex + 2 ] = lookUpBase64Alphabet [(l << 2 ) | val3 ];
273+ encodedData [encodedIndex + 1 ] = lookUpBase64Alphabet [val2
274+ | (k << 4 )];
275+ encodedData [encodedIndex + 2 ] = lookUpBase64Alphabet [(l << 2 )
276+ | val3 ];
217277 encodedData [encodedIndex + 3 ] = lookUpBase64Alphabet [b3 & 0x3f ];
218278 }
219279
@@ -231,8 +291,7 @@ public static byte[] encode(byte[] binaryData) {
231291 encodedData [encodedIndex + 1 ] = lookUpBase64Alphabet [k << 4 ];
232292 encodedData [encodedIndex + 2 ] = PAD ;
233293 encodedData [encodedIndex + 3 ] = PAD ;
234- }
235- else if (fewerThan24bits == SIXTEENBIT ) {
294+ } else if (fewerThan24bits == SIXTEENBIT ) {
236295 b1 = binaryData [dataIndex ];
237296 b2 = binaryData [dataIndex + 1 ];
238297 l = (byte ) (b2 & 0x0f );
@@ -247,7 +306,8 @@ else if (fewerThan24bits == SIXTEENBIT) {
247306 : (byte ) ((b2 ) >> 4 ^ 0xf0 );
248307
249308 encodedData [encodedIndex ] = lookUpBase64Alphabet [val1 ];
250- encodedData [encodedIndex + 1 ] = lookUpBase64Alphabet [val2 | (k << 4 )];
309+ encodedData [encodedIndex + 1 ] = lookUpBase64Alphabet [val2
310+ | (k << 4 )];
251311 encodedData [encodedIndex + 2 ] = lookUpBase64Alphabet [l << 2 ];
252312 encodedData [encodedIndex + 3 ] = PAD ;
253313 }
@@ -266,8 +326,7 @@ else if (fewerThan24bits == SIXTEENBIT) {
266326 public static String encode (String data ) {
267327 try {
268328 return encode (data , DEFAULT_CHAR_ENCODING );
269- }
270- catch (UnsupportedEncodingException uee ) {
329+ } catch (UnsupportedEncodingException uee ) {
271330 throw new IllegalStateException (uee .toString ());
272331 }
273332 }
@@ -281,12 +340,11 @@ public static String encode(String data) {
281340 *
282341 * @param data String of data to convert
283342 * @param charEncoding the character encoding to use when converting
284- * a String to a byte[]
343+ * a String to a byte[]
285344 * @return Base64-encoded String
286345 */
287346 public static String encode (String data , String charEncoding )
288- throws UnsupportedEncodingException
289- {
347+ throws UnsupportedEncodingException {
290348
291349 // Check arguments
292350 if (data == null ) {
@@ -301,8 +359,7 @@ public static String encode(String data, String charEncoding)
301359 OutputStreamWriter osw = new OutputStreamWriter (bos , charEncoding );
302360 try {
303361 osw .write (data );
304- }
305- catch (IOException ioe ) {
362+ } catch (IOException ioe ) {
306363 throw new RuntimeException (ioe .toString ());
307364 }
308365
@@ -316,8 +373,7 @@ public static String encode(String data, String charEncoding)
316373 bos = new ByteArrayOutputStream (encodedData .length );
317374 try {
318375 bos .write (encodedData );
319- }
320- catch (IOException ioe ) {
376+ } catch (IOException ioe ) {
321377 throw new RuntimeException (ioe .toString ());
322378 }
323379
@@ -327,7 +383,7 @@ public static String encode(String data, String charEncoding)
327383 /**
328384 * Decodes Base64 data into octects
329385 *
330- * @param binaryData Byte array containing Base64 data
386+ * @param base64Data Byte array containing Base64 data
331387 * @return Array containing decoded data.
332388 */
333389 public static byte [] decode (byte [] base64Data ) {
@@ -370,19 +426,19 @@ public static byte[] decode(byte[] base64Data) {
370426 b4 = base64Alphabet [marker1 ];
371427
372428 decodedData [encodedIndex ] = (byte ) (b1 << 2 | b2 >> 4 );
373- decodedData [encodedIndex + 1 ] = (byte ) (((b2 & 0xf ) << 4 ) | ((b3 >> 2 ) & 0xf ));
429+ decodedData [encodedIndex + 1 ] = (byte ) (((b2 & 0xf ) << 4 )
430+ | ((b3 >> 2 ) & 0xf ));
374431 decodedData [encodedIndex + 2 ] = (byte ) (b3 << 6 | b4 );
375- }
376- else if (marker0 == PAD ) {
432+ } else if (marker0 == PAD ) {
377433 //Two PAD e.g. 3c[Pad][Pad]
378434 decodedData [encodedIndex ] = (byte ) (b1 << 2 | b2 >> 4 );
379- }
380- else if (marker1 == PAD ) {
435+ } else if (marker1 == PAD ) {
381436 //One PAD e.g. 3cQ[Pad]
382437 b3 = base64Alphabet [marker0 ];
383438
384439 decodedData [encodedIndex ] = (byte ) (b1 << 2 | b2 >> 4 );
385- decodedData [encodedIndex + 1 ] = (byte ) (((b2 & 0xf ) << 4 ) | ((b3 >> 2 ) & 0xf ));
440+ decodedData [encodedIndex + 1 ] = (byte ) (((b2 & 0xf ) << 4 )
441+ | ((b3 >> 2 ) & 0xf ));
386442 }
387443 encodedIndex += 3 ;
388444 }
0 commit comments