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