1 | /* |
---|
2 | * @(#)Base64.java 1.4 03/01/23 |
---|
3 | * |
---|
4 | * Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
---|
5 | * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Static methods for translating Base64 encoded strings to byte arrays |
---|
10 | * and vice-versa. |
---|
11 | * |
---|
12 | * @author Josh Bloch |
---|
13 | * @version 1.4, 01/23/03 |
---|
14 | * @see Preferences |
---|
15 | * @since 1.4 |
---|
16 | */ |
---|
17 | public class Base64 { |
---|
18 | /** |
---|
19 | * Translates the specified byte array into a Base64 string as per |
---|
20 | * Preferences.put(byte[]). |
---|
21 | */ |
---|
22 | static String byteArrayToBase64(byte[] a) { |
---|
23 | return byteArrayToBase64(a, false); |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Translates the specified byte array into an "aternate representation" |
---|
28 | * Base64 string. This non-standard variant uses an alphabet that does |
---|
29 | * not contain the uppercase alphabetic characters, which makes it |
---|
30 | * suitable for use in situations where case-folding occurs. |
---|
31 | */ |
---|
32 | static String byteArrayToAltBase64(byte[] a) { |
---|
33 | return byteArrayToBase64(a, true); |
---|
34 | } |
---|
35 | |
---|
36 | private static String byteArrayToBase64(byte[] a, boolean alternate) { |
---|
37 | int aLen = a.length; |
---|
38 | int numFullGroups = aLen/3; |
---|
39 | int numBytesInPartialGroup = aLen - 3*numFullGroups; |
---|
40 | int resultLen = 4*((aLen + 2)/3); |
---|
41 | StringBuffer result = new StringBuffer(resultLen); |
---|
42 | char[] intToAlpha = (alternate ? intToAltBase64 : intToBase64); |
---|
43 | |
---|
44 | // Translate all full groups from byte array elements to Base64 |
---|
45 | int inCursor = 0; |
---|
46 | for (int i=0; i<numFullGroups; i++) { |
---|
47 | int byte0 = a[inCursor++] & 0xff; |
---|
48 | int byte1 = a[inCursor++] & 0xff; |
---|
49 | int byte2 = a[inCursor++] & 0xff; |
---|
50 | result.append(intToAlpha[byte0 >> 2]); |
---|
51 | result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]); |
---|
52 | result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]); |
---|
53 | result.append(intToAlpha[byte2 & 0x3f]); |
---|
54 | } |
---|
55 | |
---|
56 | // Translate partial group if present |
---|
57 | if (numBytesInPartialGroup != 0) { |
---|
58 | int byte0 = a[inCursor++] & 0xff; |
---|
59 | result.append(intToAlpha[byte0 >> 2]); |
---|
60 | if (numBytesInPartialGroup == 1) { |
---|
61 | result.append(intToAlpha[(byte0 << 4) & 0x3f]); |
---|
62 | result.append("=="); |
---|
63 | } else { |
---|
64 | // assert numBytesInPartialGroup == 2; |
---|
65 | int byte1 = a[inCursor++] & 0xff; |
---|
66 | result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]); |
---|
67 | result.append(intToAlpha[(byte1 << 2)&0x3f]); |
---|
68 | result.append('='); |
---|
69 | } |
---|
70 | } |
---|
71 | // assert inCursor == a.length; |
---|
72 | // assert result.length() == resultLen; |
---|
73 | return result.toString(); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * This array is a lookup table that translates 6-bit positive integer |
---|
78 | * index values into their "Base64 Alphabet" equivalents as specified |
---|
79 | * in Table 1 of RFC 2045. |
---|
80 | */ |
---|
81 | private static final char intToBase64[] = { |
---|
82 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
---|
83 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
---|
84 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
---|
85 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
---|
86 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
---|
87 | }; |
---|
88 | |
---|
89 | /** |
---|
90 | * This array is a lookup table that translates 6-bit positive integer |
---|
91 | * index values into their "Alternate Base64 Alphabet" equivalents. |
---|
92 | * This is NOT the real Base64 Alphabet as per in Table 1 of RFC 2045. |
---|
93 | * This alternate alphabet does not use the capital letters. It is |
---|
94 | * designed for use in environments where "case folding" occurs. |
---|
95 | */ |
---|
96 | private static final char intToAltBase64[] = { |
---|
97 | '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':', |
---|
98 | ';', '<', '>', '@', '[', ']', '^', '`', '_', '{', '|', '}', '~', |
---|
99 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
---|
100 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
---|
101 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '?' |
---|
102 | }; |
---|
103 | |
---|
104 | /** |
---|
105 | * Translates the specified Base64 string (as per Preferences.get(byte[])) |
---|
106 | * into a byte array. |
---|
107 | * |
---|
108 | * @throw IllegalArgumentException if <tt>s</tt> is not a valid Base64 |
---|
109 | * string. |
---|
110 | */ |
---|
111 | static byte[] base64ToByteArray(String s) { |
---|
112 | return base64ToByteArray(s, false); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Translates the specified "aternate representation" Base64 string |
---|
117 | * into a byte array. |
---|
118 | * |
---|
119 | * @throw IllegalArgumentException or ArrayOutOfBoundsException |
---|
120 | * if <tt>s</tt> is not a valid alternate representation |
---|
121 | * Base64 string. |
---|
122 | */ |
---|
123 | static byte[] altBase64ToByteArray(String s) { |
---|
124 | return base64ToByteArray(s, true); |
---|
125 | } |
---|
126 | |
---|
127 | private static byte[] base64ToByteArray(String s, boolean alternate) { |
---|
128 | byte[] alphaToInt = (alternate ? altBase64ToInt : base64ToInt); |
---|
129 | int sLen = s.length(); |
---|
130 | int numGroups = sLen/4; |
---|
131 | if (4*numGroups != sLen) |
---|
132 | throw new IllegalArgumentException( |
---|
133 | "String length must be a multiple of four."); |
---|
134 | int missingBytesInLastGroup = 0; |
---|
135 | int numFullGroups = numGroups; |
---|
136 | if (sLen != 0) { |
---|
137 | if (s.charAt(sLen-1) == '=') { |
---|
138 | missingBytesInLastGroup++; |
---|
139 | numFullGroups--; |
---|
140 | } |
---|
141 | if (s.charAt(sLen-2) == '=') |
---|
142 | missingBytesInLastGroup++; |
---|
143 | } |
---|
144 | byte[] result = new byte[3*numGroups - missingBytesInLastGroup]; |
---|
145 | |
---|
146 | // Translate all full groups from base64 to byte array elements |
---|
147 | int inCursor = 0, outCursor = 0; |
---|
148 | for (int i=0; i<numFullGroups; i++) { |
---|
149 | int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
150 | int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
151 | int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
152 | int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
153 | result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); |
---|
154 | result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); |
---|
155 | result[outCursor++] = (byte) ((ch2 << 6) | ch3); |
---|
156 | } |
---|
157 | |
---|
158 | // Translate partial group, if present |
---|
159 | if (missingBytesInLastGroup != 0) { |
---|
160 | int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
161 | int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
162 | result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); |
---|
163 | |
---|
164 | if (missingBytesInLastGroup == 1) { |
---|
165 | int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt); |
---|
166 | result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); |
---|
167 | } |
---|
168 | } |
---|
169 | // assert inCursor == s.length()-missingBytesInLastGroup; |
---|
170 | // assert outCursor == result.length; |
---|
171 | return result; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Translates the specified character, which is assumed to be in the |
---|
176 | * "Base 64 Alphabet" into its equivalent 6-bit positive integer. |
---|
177 | * |
---|
178 | * @throw IllegalArgumentException or ArrayOutOfBoundsException if |
---|
179 | * c is not in the Base64 Alphabet. |
---|
180 | */ |
---|
181 | private static int base64toInt(char c, byte[] alphaToInt) { |
---|
182 | int result = alphaToInt[c]; |
---|
183 | if (result < 0) |
---|
184 | throw new IllegalArgumentException("Illegal character " + c); |
---|
185 | return result; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * This array is a lookup table that translates unicode characters |
---|
190 | * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) |
---|
191 | * into their 6-bit positive integer equivalents. Characters that |
---|
192 | * are not in the Base64 alphabet but fall within the bounds of the |
---|
193 | * array are translated to -1. |
---|
194 | */ |
---|
195 | private static final byte base64ToInt[] = { |
---|
196 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
---|
197 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
---|
198 | -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, |
---|
199 | 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, |
---|
200 | 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
---|
201 | 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
---|
202 | 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 |
---|
203 | }; |
---|
204 | |
---|
205 | /** |
---|
206 | * This array is the analogue of base64ToInt, but for the nonstandard |
---|
207 | * variant that avoids the use of uppercase alphabetic characters. |
---|
208 | */ |
---|
209 | private static final byte altBase64ToInt[] = { |
---|
210 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
---|
211 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, |
---|
212 | 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1 , 52, 53, 54, 55, 56, 57, |
---|
213 | 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1, |
---|
214 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
---|
215 | -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33, |
---|
216 | 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, |
---|
217 | 51, 22, 23, 24, 25 |
---|
218 | }; |
---|
219 | } |
---|