1515 * @version 1.4, 01/23/03
1616 * @see Preferences
1717 * @since 1.4
18+ *
19+ * Remove alternative Base64. Support no line breaks and parse white
20+ * space ' ' as '+'.
21+ * @author Zhou Renjian
22+ * @version Dec 6, 2010
1823 */
1924class Base64 {
2025 /**
2126 * Translates the specified byte array into a Base64 string as per
2227 * Preferences.put(byte[]).
2328 */
2429 static String byteArrayToBase64 (byte [] a ) {
25- return byteArrayToBase64 (a , false );
26- }
27-
28- /**
29- * Translates the specified byte array into an "aternate representation"
30- * Base64 string. This non-standard variant uses an alphabet that does
31- * not contain the uppercase alphabetic characters, which makes it
32- * suitable for use in situations where case-folding occurs.
33- */
34- static String byteArrayToAltBase64 (byte [] a ) {
35- return byteArrayToBase64 (a , true );
36- }
37-
38- private static String byteArrayToBase64 (byte [] a , boolean alternate ) {
3930 int aLen = a .length ;
4031 int numFullGroups = aLen /3 ;
4132 int numBytesInPartialGroup = aLen - 3 *numFullGroups ;
4233 int resultLen = 4 *((aLen + 2 )/3 );
4334 StringBuffer result = new StringBuffer (resultLen );
44- char [] intToAlpha = ( alternate ? intToAltBase64 : intToBase64 ) ;
35+ char [] intToAlpha = intToBase64 ;
4536
4637 // Translate all full groups from byte array elements to Base64
4738 int inCursor = 0 ;
@@ -88,21 +79,6 @@ private static String byteArrayToBase64(byte[] a, boolean alternate) {
8879 '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/'
8980 };
9081
91- /**
92- * This array is a lookup table that translates 6-bit positive integer
93- * index values into their "Alternate Base64 Alphabet" equivalents.
94- * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045.
95- * This alternate alphabet does not use the capital letters. It is
96- * designed for use in environments where "case folding" occurs.
97- */
98- private static final char intToAltBase64 [] = {
99- '!' , '"' , '#' , '$' , '%' , '&' , '\'' , '(' , ')' , ',' , '-' , '.' , ':' ,
100- ';' , '<' , '>' , '@' , '[' , ']' , '^' , '`' , '_' , '{' , '|' , '}' , '~' ,
101- 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' ,
102- 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' ,
103- '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '?'
104- };
105-
10682 /**
10783 * Translates the specified Base64 string (as per Preferences.get(byte[]))
10884 * into a byte array.
@@ -111,23 +87,7 @@ private static String byteArrayToBase64(byte[] a, boolean alternate) {
11187 * string.
11288 */
11389 static byte [] base64ToByteArray (String s ) {
114- return base64ToByteArray (s , false );
115- }
116-
117- /**
118- * Translates the specified "aternate representation" Base64 string
119- * into a byte array.
120- *
121- * @throw IllegalArgumentException or ArrayOutOfBoundsException
122- * if <tt>s</tt> is not a valid alternate representation
123- * Base64 string.
124- */
125- static byte [] altBase64ToByteArray (String s ) {
126- return base64ToByteArray (s , true );
127- }
128-
129- private static byte [] base64ToByteArray (String s , boolean alternate ) {
130- byte [] alphaToInt = (alternate ? altBase64ToInt : base64ToInt );
90+ byte [] alphaToInt = base64ToInt ;
13191 int sLen = s .length ();
13292 int numGroups = sLen /4 ;
13393 if (4 *numGroups != sLen )
@@ -196,51 +156,12 @@ private static int base64toInt(char c, byte[] alphaToInt) {
196156 */
197157 private static final byte base64ToInt [] = {
198158 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
199- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , - 1 , -1 ,
159+ -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 ,
200160 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 62 , -1 , -1 , -1 , 63 , 52 , 53 , 54 ,
201161 55 , 56 , 57 , 58 , 59 , 60 , 61 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 , 2 , 3 , 4 ,
202162 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ,
203163 24 , 25 , -1 , -1 , -1 , -1 , -1 , -1 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 ,
204164 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51
205165 };
206166
207- /**
208- * This array is the analogue of base64ToInt, but for the nonstandard
209- * variant that avoids the use of uppercase alphabetic characters.
210- */
211- private static final byte altBase64ToInt [] = {
212- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
213- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , 0 , 1 ,
214- 2 , 3 , 4 , 5 , 6 , 7 , 8 , -1 , 62 , 9 , 10 , 11 , -1 , 52 , 53 , 54 , 55 , 56 , 57 ,
215- 58 , 59 , 60 , 61 , 12 , 13 , 14 , -1 , 15 , 63 , 16 , -1 , -1 , -1 , -1 , -1 , -1 ,
216- -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
217- -1 , -1 , -1 , 17 , -1 , 18 , 19 , 21 , 20 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 ,
218- 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 ,
219- 51 , 22 , 23 , 24 , 25
220- };
221-
222- /*
223- public static void main(String args[]) {
224- int numRuns = Integer.parseInt(args[0]);
225- int numBytes = Integer.parseInt(args[1]);
226- java.util.Random rnd = new java.util.Random();
227- for (int i=0; i<numRuns; i++) {
228- for (int j=0; j<numBytes; j++) {
229- byte[] arr = new byte[j];
230- for (int k=0; k<j; k++)
231- arr[k] = (byte)rnd.nextInt();
232-
233- String s = byteArrayToBase64(arr);
234- byte [] b = base64ToByteArray(s);
235- if (!java.util.Arrays.equals(arr, b))
236- System.out.println("Dismal failure!");
237-
238- s = byteArrayToAltBase64(arr);
239- b = altBase64ToByteArray(s);
240- if (!java.util.Arrays.equals(arr, b))
241- System.out.println("Alternate dismal failure!");
242- }
243- }
244- }
245- */
246167}
0 commit comments