1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.xfire.util ;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.io.Writer;
21
22 /***
23 *
24 * @author TAMURA Kent <kent@trl.ibm.co.jp>
25 */
26 public class Base64 {
27 private static final char[] S_BASE64CHAR = {
28 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
29 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
30 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
31 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
32 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
33 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
34 '8', '9', '+', '/'
35 };
36 private static final char S_BASE64PAD = '=';
37 private static final byte[] S_DECODETABLE = new byte[128];
38 static {
39 for (int i = 0; i < S_DECODETABLE.length; i ++)
40 S_DECODETABLE[i] = Byte.MAX_VALUE;
41 for (int i = 0; i < S_BASE64CHAR.length; i ++)
42 S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
43 }
44
45 private static int decode0(char[] ibuf, byte[] obuf, int wp) {
46 int outlen = 3;
47 if (ibuf[3] == S_BASE64PAD) outlen = 2;
48 if (ibuf[2] == S_BASE64PAD) outlen = 1;
49 int b0 = S_DECODETABLE[ibuf[0]];
50 int b1 = S_DECODETABLE[ibuf[1]];
51 int b2 = S_DECODETABLE[ibuf[2]];
52 int b3 = S_DECODETABLE[ibuf[3]];
53 switch (outlen) {
54 case 1:
55 obuf[wp] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
56 return 1;
57 case 2:
58 obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
59 obuf[wp] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
60 return 2;
61 case 3:
62 obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
63 obuf[wp++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
64 obuf[wp] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f);
65 return 3;
66 default:
67 throw new RuntimeException("Couldn't decode.");
68 }
69 }
70
71 /***
72 *
73 */
74 public static byte[] decode(char[] data, int off, int len) {
75 char[] ibuf = new char[4];
76 int ibufcount = 0;
77 byte[] obuf = new byte[len/4*3+3];
78 int obufcount = 0;
79 for (int i = off; i < off+len; i ++) {
80 char ch = data[i];
81 if (ch == S_BASE64PAD
82 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
83 ibuf[ibufcount++] = ch;
84 if (ibufcount == ibuf.length) {
85 ibufcount = 0;
86 obufcount += decode0(ibuf, obuf, obufcount);
87 }
88 }
89 }
90 if (obufcount == obuf.length)
91 return obuf;
92 byte[] ret = new byte[obufcount];
93 System.arraycopy(obuf, 0, ret, 0, obufcount);
94 return ret;
95 }
96
97 /***
98 *
99 */
100 public static byte[] decode(String data) {
101 char[] ibuf = new char[4];
102 int ibufcount = 0;
103 byte[] obuf = new byte[data.length()/4*3+3];
104 int obufcount = 0;
105 for (int i = 0; i < data.length(); i ++) {
106 char ch = data.charAt(i);
107 if (ch == S_BASE64PAD
108 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
109 ibuf[ibufcount++] = ch;
110 if (ibufcount == ibuf.length) {
111 ibufcount = 0;
112 obufcount += decode0(ibuf, obuf, obufcount);
113 }
114 }
115 }
116 if (obufcount == obuf.length)
117 return obuf;
118 byte[] ret = new byte[obufcount];
119 System.arraycopy(obuf, 0, ret, 0, obufcount);
120 return ret;
121 }
122
123 /***
124 *
125 */
126 public static void decode(char[] data, int off, int len, OutputStream ostream) throws IOException {
127 char[] ibuf = new char[4];
128 int ibufcount = 0;
129 byte[] obuf = new byte[3];
130 for (int i = off; i < off+len; i ++) {
131 char ch = data[i];
132 if (ch == S_BASE64PAD
133 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
134 ibuf[ibufcount++] = ch;
135 if (ibufcount == ibuf.length) {
136 ibufcount = 0;
137 int obufcount = decode0(ibuf, obuf, 0);
138 ostream.write(obuf, 0, obufcount);
139 }
140 }
141 }
142 }
143
144 /***
145 *
146 */
147 public static void decode(String data, OutputStream ostream) throws IOException {
148 char[] ibuf = new char[4];
149 int ibufcount = 0;
150 byte[] obuf = new byte[3];
151 for (int i = 0; i < data.length(); i ++) {
152 char ch = data.charAt(i);
153 if (ch == S_BASE64PAD
154 || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
155 ibuf[ibufcount++] = ch;
156 if (ibufcount == ibuf.length) {
157 ibufcount = 0;
158 int obufcount = decode0(ibuf, obuf, 0);
159 ostream.write(obuf, 0, obufcount);
160 }
161 }
162 }
163 }
164
165 /***
166 * Returns base64 representation of specified byte array.
167 */
168 public static String encode(byte[] data) {
169 return encode(data, 0, data.length);
170 }
171
172 /***
173 * Returns base64 representation of specified byte array.
174 */
175 public static String encode(byte[] data, int off, int len) {
176 if (len <= 0) return "";
177 char[] out = new char[len/3*4+4];
178 int rindex = off;
179 int windex = 0;
180 int rest = len-off;
181 while (rest >= 3) {
182 int i = ((data[rindex]&0xff)<<16)
183 +((data[rindex+1]&0xff)<<8)
184 +(data[rindex+2]&0xff);
185 out[windex++] = S_BASE64CHAR[i>>18];
186 out[windex++] = S_BASE64CHAR[(i>>12)&0x3f];
187 out[windex++] = S_BASE64CHAR[(i>>6)&0x3f];
188 out[windex++] = S_BASE64CHAR[i&0x3f];
189 rindex += 3;
190 rest -= 3;
191 }
192 if (rest == 1) {
193 int i = data[rindex]&0xff;
194 out[windex++] = S_BASE64CHAR[i>>2];
195 out[windex++] = S_BASE64CHAR[(i<<4)&0x3f];
196 out[windex++] = S_BASE64PAD;
197 out[windex++] = S_BASE64PAD;
198 } else if (rest == 2) {
199 int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
200 out[windex++] = S_BASE64CHAR[i>>10];
201 out[windex++] = S_BASE64CHAR[(i>>4)&0x3f];
202 out[windex++] = S_BASE64CHAR[(i<<2)&0x3f];
203 out[windex++] = S_BASE64PAD;
204 }
205 return new String(out, 0, windex);
206 }
207
208 /***
209 * Outputs base64 representation of the specified byte array to a byte stream.
210 */
211 public static void encode(byte[] data, int off, int len, OutputStream ostream) throws IOException {
212 if (len <= 0) return;
213 byte[] out = new byte[4];
214 int rindex = off;
215 int rest = len-off;
216 while (rest >= 3) {
217 int i = ((data[rindex]&0xff)<<16)
218 +((data[rindex+1]&0xff)<<8)
219 +(data[rindex+2]&0xff);
220 out[0] = (byte)S_BASE64CHAR[i>>18];
221 out[1] = (byte)S_BASE64CHAR[(i>>12)&0x3f];
222 out[2] = (byte)S_BASE64CHAR[(i>>6)&0x3f];
223 out[3] = (byte)S_BASE64CHAR[i&0x3f];
224 ostream.write(out, 0, 4);
225 rindex += 3;
226 rest -= 3;
227 }
228 if (rest == 1) {
229 int i = data[rindex]&0xff;
230 out[0] = (byte)S_BASE64CHAR[i>>2];
231 out[1] = (byte)S_BASE64CHAR[(i<<4)&0x3f];
232 out[2] = (byte)S_BASE64PAD;
233 out[3] = (byte)S_BASE64PAD;
234 ostream.write(out, 0, 4);
235 } else if (rest == 2) {
236 int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
237 out[0] = (byte)S_BASE64CHAR[i>>10];
238 out[1] = (byte)S_BASE64CHAR[(i>>4)&0x3f];
239 out[2] = (byte)S_BASE64CHAR[(i<<2)&0x3f];
240 out[3] = (byte)S_BASE64PAD;
241 ostream.write(out, 0, 4);
242 }
243 }
244
245 /***
246 * Outputs base64 representation of the specified byte array to a character stream.
247 */
248 public static void encode(byte[] data, int off, int len, Writer writer) throws IOException {
249 if (len <= 0) return;
250 char[] out = new char[4];
251 int rindex = off;
252 int rest = len-off;
253 int output = 0;
254 while (rest >= 3) {
255 int i = ((data[rindex]&0xff)<<16)
256 +((data[rindex+1]&0xff)<<8)
257 +(data[rindex+2]&0xff);
258 out[0] = S_BASE64CHAR[i>>18];
259 out[1] = S_BASE64CHAR[(i>>12)&0x3f];
260 out[2] = S_BASE64CHAR[(i>>6)&0x3f];
261 out[3] = S_BASE64CHAR[i&0x3f];
262 writer.write(out, 0, 4);
263 rindex += 3;
264 rest -= 3;
265 output += 4;
266 if (output % 76 == 0)
267 writer.write("\n");
268 }
269 if (rest == 1) {
270 int i = data[rindex]&0xff;
271 out[0] = S_BASE64CHAR[i>>2];
272 out[1] = S_BASE64CHAR[(i<<4)&0x3f];
273 out[2] = S_BASE64PAD;
274 out[3] = S_BASE64PAD;
275 writer.write(out, 0, 4);
276 } else if (rest == 2) {
277 int i = ((data[rindex]&0xff)<<8)+(data[rindex+1]&0xff);
278 out[0] = S_BASE64CHAR[i>>10];
279 out[1] = S_BASE64CHAR[(i>>4)&0x3f];
280 out[2] = S_BASE64CHAR[(i<<2)&0x3f];
281 out[3] = S_BASE64PAD;
282 writer.write(out, 0, 4);
283 }
284 }
285 }