KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > encoding > Base64


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.encoding;
56
57 import org.jboss.axis.utils.JavaUtils;
58 import org.jboss.axis.utils.Messages;
59
60 import java.io.IOException JavaDoc;
61 import java.io.OutputStream JavaDoc;
62 import java.io.Writer JavaDoc;
63
64 /**
65  * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
66  */

67 public class Base64
68 {
69    private static final char[] S_BASE64CHAR = {
70       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
71       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
72       'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
73       'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
74       'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
75       'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
76       '8', '9', '+', '/'
77    };
78    private static final char S_BASE64PAD = '=';
79    private static final byte[] S_DECODETABLE = new byte[128];
80
81    static
82    {
83       for (int i = 0; i < S_DECODETABLE.length; i++)
84          S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
85
for (int i = 0; i < S_BASE64CHAR.length; i++) // 0 to 63
86
S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
87    }
88
89    private static int decode0(char[] ibuf, byte[] obuf, int wp)
90    {
91       int outlen = 3;
92       if (ibuf[3] == S_BASE64PAD) outlen = 2;
93       if (ibuf[2] == S_BASE64PAD) outlen = 1;
94       int b0 = S_DECODETABLE[ibuf[0]];
95       int b1 = S_DECODETABLE[ibuf[1]];
96       int b2 = S_DECODETABLE[ibuf[2]];
97       int b3 = S_DECODETABLE[ibuf[3]];
98       switch (outlen)
99       {
100          case 1:
101             obuf[wp] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
102             return 1;
103          case 2:
104             obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
105             obuf[wp] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
106             return 2;
107          case 3:
108             obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
109             obuf[wp++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
110             obuf[wp] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f);
111             return 3;
112          default:
113             throw new RuntimeException JavaDoc(Messages.getMessage("internalError00"));
114       }
115    }
116
117    /**
118     *
119     */

120    public static byte[] decode(char[] data, int off, int len)
121    {
122       char[] ibuf = new char[4];
123       int ibufcount = 0;
124       byte[] obuf = new byte[len / 4 * 3 + 3];
125       int obufcount = 0;
126       for (int i = off; i < off + len; i++)
127       {
128          char ch = data[i];
129          if (ch == S_BASE64PAD
130                  || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE)
131          {
132             ibuf[ibufcount++] = ch;
133             if (ibufcount == ibuf.length)
134             {
135                ibufcount = 0;
136                obufcount += decode0(ibuf, obuf, obufcount);
137             }
138          }
139       }
140       if (obufcount == obuf.length)
141          return obuf;
142       byte[] ret = new byte[obufcount];
143       System.arraycopy(obuf, 0, ret, 0, obufcount);
144       return ret;
145    }
146
147    /**
148     *
149     */

150    public static byte[] decode(String JavaDoc data)
151    {
152       char[] ibuf = new char[4];
153       int ibufcount = 0;
154       byte[] obuf = new byte[data.length() / 4 * 3 + 3];
155       int obufcount = 0;
156       for (int i = 0; i < data.length(); i++)
157       {
158          char ch = data.charAt(i);
159          if (ch == S_BASE64PAD
160                  || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE)
161          {
162             ibuf[ibufcount++] = ch;
163             if (ibufcount == ibuf.length)
164             {
165                ibufcount = 0;
166                obufcount += decode0(ibuf, obuf, obufcount);
167             }
168          }
169       }
170       if (obufcount == obuf.length)
171          return obuf;
172       byte[] ret = new byte[obufcount];
173       System.arraycopy(obuf, 0, ret, 0, obufcount);
174       return ret;
175    }
176
177    /**
178     *
179     */

180    public static void decode(char[] data, int off, int len, OutputStream JavaDoc ostream) throws IOException JavaDoc
181    {
182       char[] ibuf = new char[4];
183       int ibufcount = 0;
184       byte[] obuf = new byte[3];
185       for (int i = off; i < off + len; i++)
186       {
187          char ch = data[i];
188          if (ch == S_BASE64PAD
189                  || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE)
190          {
191             ibuf[ibufcount++] = ch;
192             if (ibufcount == ibuf.length)
193             {
194                ibufcount = 0;
195                int obufcount = decode0(ibuf, obuf, 0);
196                ostream.write(obuf, 0, obufcount);
197             }
198          }
199       }
200    }
201
202    /**
203     *
204     */

205    public static void decode(String JavaDoc data, OutputStream JavaDoc ostream) throws IOException JavaDoc
206    {
207       char[] ibuf = new char[4];
208       int ibufcount = 0;
209       byte[] obuf = new byte[3];
210       for (int i = 0; i < data.length(); i++)
211       {
212          char ch = data.charAt(i);
213          if (ch == S_BASE64PAD
214                  || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE)
215          {
216             ibuf[ibufcount++] = ch;
217             if (ibufcount == ibuf.length)
218             {
219                ibufcount = 0;
220                int obufcount = decode0(ibuf, obuf, 0);
221                ostream.write(obuf, 0, obufcount);
222             }
223          }
224       }
225    }
226
227    /**
228     * Returns base64 representation of specified byte array.
229     */

230    public static String JavaDoc encode(byte[] data)
231    {
232       return encode(data, 0, data.length);
233    }
234
235    /**
236     * Returns base64 representation of specified byte array.
237     */

238    public static String JavaDoc encode(byte[] data, int off, int len)
239    {
240       if (len <= 0) return "";
241       char[] out = new char[len / 3 * 4 + 4];
242       int rindex = off;
243       int windex = 0;
244       int rest = len - off;
245       while (rest >= 3)
246       {
247          int i = ((data[rindex] & 0xff) << 16)
248                  + ((data[rindex + 1] & 0xff) << 8)
249                  + (data[rindex + 2] & 0xff);
250          out[windex++] = S_BASE64CHAR[i >> 18];
251          out[windex++] = S_BASE64CHAR[(i >> 12) & 0x3f];
252          out[windex++] = S_BASE64CHAR[(i >> 6) & 0x3f];
253          out[windex++] = S_BASE64CHAR[i & 0x3f];
254          rindex += 3;
255          rest -= 3;
256       }
257       if (rest == 1)
258       {
259          int i = data[rindex] & 0xff;
260          out[windex++] = S_BASE64CHAR[i >> 2];
261          out[windex++] = S_BASE64CHAR[(i << 4) & 0x3f];
262          out[windex++] = S_BASE64PAD;
263          out[windex++] = S_BASE64PAD;
264       }
265       else if (rest == 2)
266       {
267          int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
268          out[windex++] = S_BASE64CHAR[i >> 10];
269          out[windex++] = S_BASE64CHAR[(i >> 4) & 0x3f];
270          out[windex++] = S_BASE64CHAR[(i << 2) & 0x3f];
271          out[windex++] = S_BASE64PAD;
272       }
273       return new String JavaDoc(out, 0, windex);
274    }
275
276    /**
277     * Outputs base64 representation of the specified byte array to a byte stream.
278     */

279    public static void encode(byte[] data, int off, int len, OutputStream JavaDoc ostream) throws IOException JavaDoc
280    {
281       if (len <= 0) return;
282       byte[] out = new byte[4];
283       int rindex = off;
284       int rest = len - off;
285       while (rest >= 3)
286       {
287          int i = ((data[rindex] & 0xff) << 16)
288                  + ((data[rindex + 1] & 0xff) << 8)
289                  + (data[rindex + 2] & 0xff);
290          out[0] = (byte)S_BASE64CHAR[i >> 18];
291          out[1] = (byte)S_BASE64CHAR[(i >> 12) & 0x3f];
292          out[2] = (byte)S_BASE64CHAR[(i >> 6) & 0x3f];
293          out[3] = (byte)S_BASE64CHAR[i & 0x3f];
294          ostream.write(out, 0, 4);
295          rindex += 3;
296          rest -= 3;
297       }
298       if (rest == 1)
299       {
300          int i = data[rindex] & 0xff;
301          out[0] = (byte)S_BASE64CHAR[i >> 2];
302          out[1] = (byte)S_BASE64CHAR[(i << 4) & 0x3f];
303          out[2] = (byte)S_BASE64PAD;
304          out[3] = (byte)S_BASE64PAD;
305          ostream.write(out, 0, 4);
306       }
307       else if (rest == 2)
308       {
309          int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
310          out[0] = (byte)S_BASE64CHAR[i >> 10];
311          out[1] = (byte)S_BASE64CHAR[(i >> 4) & 0x3f];
312          out[2] = (byte)S_BASE64CHAR[(i << 2) & 0x3f];
313          out[3] = (byte)S_BASE64PAD;
314          ostream.write(out, 0, 4);
315       }
316    }
317
318    /**
319     * Outputs base64 representation of the specified byte array to a character stream.
320     */

321    public static void encode(byte[] data, int off, int len, Writer JavaDoc writer) throws IOException JavaDoc
322    {
323       if (len <= 0) return;
324       char[] out = new char[4];
325       int rindex = off;
326       int rest = len - off;
327       int output = 0;
328       while (rest >= 3)
329       {
330          int i = ((data[rindex] & 0xff) << 16)
331                  + ((data[rindex + 1] & 0xff) << 8)
332                  + (data[rindex + 2] & 0xff);
333          out[0] = S_BASE64CHAR[i >> 18];
334          out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
335          out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
336          out[3] = S_BASE64CHAR[i & 0x3f];
337          writer.write(out, 0, 4);
338          rindex += 3;
339          rest -= 3;
340          output += 4;
341          if (output % 76 == 0)
342             writer.write(JavaUtils.LS);
343       }
344       if (rest == 1)
345       {
346          int i = data[rindex] & 0xff;
347          out[0] = S_BASE64CHAR[i >> 2];
348          out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
349          out[2] = S_BASE64PAD;
350          out[3] = S_BASE64PAD;
351          writer.write(out, 0, 4);
352       }
353       else if (rest == 2)
354       {
355          int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
356          out[0] = S_BASE64CHAR[i >> 10];
357          out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
358          out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
359          out[3] = S_BASE64PAD;
360          writer.write(out, 0, 4);
361       }
362    }
363 }
364
Popular Tags