KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > attachments > Base64


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.axis2.attachments;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 /**
23  * @author TAMURA Kent <kent@trl.ibm.co.jp>
24  */

25 public class Base64 {
26     private static final char[] S_BASE64CHAR = { 'A', 'B', 'C', 'D', 'E', 'F',
27             'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
28             'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
29             'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
30             't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
31             '6', '7', '8', '9', '+', '/' };
32
33     private static final char S_BASE64PAD = '=';
34
35     private static final byte[] S_DECODETABLE = new byte[128];
36
37     static {
38         for (int i = 0; i < S_DECODETABLE.length; i++)
39             S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
40
for (int i = 0; i < S_BASE64CHAR.length; i++)
41             // 0 to 63
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)
48             outlen = 2;
49         if (ibuf[2] == S_BASE64PAD)
50             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 JavaDoc("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 || ch < S_DECODETABLE.length
84                     && 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 JavaDoc 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 || ch < S_DECODETABLE.length
110                     && 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,
129             OutputStream JavaDoc ostream) throws IOException JavaDoc {
130         char[] ibuf = new char[4];
131         int ibufcount = 0;
132         byte[] obuf = new byte[3];
133         for (int i = off; i < off + len; i++) {
134             char ch = data[i];
135             if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
136                     && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
137                 ibuf[ibufcount++] = ch;
138                 if (ibufcount == ibuf.length) {
139                     ibufcount = 0;
140                     int obufcount = decode0(ibuf, obuf, 0);
141                     ostream.write(obuf, 0, obufcount);
142                 }
143             }
144         }
145     }
146
147     /**
148      *
149      */

150     public static void decode(String JavaDoc data, OutputStream JavaDoc ostream)
151             throws IOException JavaDoc {
152         char[] ibuf = new char[4];
153         int ibufcount = 0;
154         byte[] obuf = new byte[3];
155         for (int i = 0; i < data.length(); i++) {
156             char ch = data.charAt(i);
157             if (ch == S_BASE64PAD || ch < S_DECODETABLE.length
158                     && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
159                 ibuf[ibufcount++] = ch;
160                 if (ibufcount == ibuf.length) {
161                     ibufcount = 0;
162                     int obufcount = decode0(ibuf, obuf, 0);
163                     ostream.write(obuf, 0, obufcount);
164                 }
165             }
166         }
167     }
168
169     /**
170      * Returns base64 representation of specified byte array.
171      */

172     public static String JavaDoc encode(byte[] data) {
173         return encode(data, 0, data.length);
174     }
175
176     /**
177      * Returns base64 representation of specified byte array.
178      */

179     public static String JavaDoc encode(byte[] data, int off, int len) {
180         if (len <= 0)
181             return "";
182         char[] out = new char[len / 3 * 4 + 4];
183         int rindex = off;
184         int windex = 0;
185         int rest = len - off;
186         while (rest >= 3) {
187             int i = ((data[rindex] & 0xff) << 16)
188                     + ((data[rindex + 1] & 0xff) << 8)
189                     + (data[rindex + 2] & 0xff);
190             out[windex++] = S_BASE64CHAR[i >> 18];
191             out[windex++] = S_BASE64CHAR[(i >> 12) & 0x3f];
192             out[windex++] = S_BASE64CHAR[(i >> 6) & 0x3f];
193             out[windex++] = S_BASE64CHAR[i & 0x3f];
194             rindex += 3;
195             rest -= 3;
196         }
197         if (rest == 1) {
198             int i = data[rindex] & 0xff;
199             out[windex++] = S_BASE64CHAR[i >> 2];
200             out[windex++] = S_BASE64CHAR[(i << 4) & 0x3f];
201             out[windex++] = S_BASE64PAD;
202             out[windex++] = S_BASE64PAD;
203         } else if (rest == 2) {
204             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
205             out[windex++] = S_BASE64CHAR[i >> 10];
206             out[windex++] = S_BASE64CHAR[(i >> 4) & 0x3f];
207             out[windex++] = S_BASE64CHAR[(i << 2) & 0x3f];
208             out[windex++] = S_BASE64PAD;
209         }
210         return new String JavaDoc(out, 0, windex);
211     }
212
213     /**
214      * Outputs base64 representation of the specified byte array to a byte
215      * stream.
216      */

217     public static void encode(byte[] data, int off, int len,
218             OutputStream JavaDoc ostream) throws IOException JavaDoc {
219         if (len <= 0)
220             return;
221         byte[] out = new byte[4];
222         int rindex = off;
223         int rest = len - off;
224         while (rest >= 3) {
225             int i = ((data[rindex] & 0xff) << 16)
226                     + ((data[rindex + 1] & 0xff) << 8)
227                     + (data[rindex + 2] & 0xff);
228             out[0] = (byte) S_BASE64CHAR[i >> 18];
229             out[1] = (byte) S_BASE64CHAR[(i >> 12) & 0x3f];
230             out[2] = (byte) S_BASE64CHAR[(i >> 6) & 0x3f];
231             out[3] = (byte) S_BASE64CHAR[i & 0x3f];
232             ostream.write(out, 0, 4);
233             rindex += 3;
234             rest -= 3;
235         }
236         if (rest == 1) {
237             int i = data[rindex] & 0xff;
238             out[0] = (byte) S_BASE64CHAR[i >> 2];
239             out[1] = (byte) S_BASE64CHAR[(i << 4) & 0x3f];
240             out[2] = (byte) S_BASE64PAD;
241             out[3] = (byte) S_BASE64PAD;
242             ostream.write(out, 0, 4);
243         } else if (rest == 2) {
244             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
245             out[0] = (byte) S_BASE64CHAR[i >> 10];
246             out[1] = (byte) S_BASE64CHAR[(i >> 4) & 0x3f];
247             out[2] = (byte) S_BASE64CHAR[(i << 2) & 0x3f];
248             out[3] = (byte) S_BASE64PAD;
249             ostream.write(out, 0, 4);
250         }
251     }
252
253     /**
254      * Outputs base64 representation of the specified byte array to a character
255      * stream.
256      */

257     public static void encode(byte[] data, int off, int len, Writer JavaDoc writer)
258             throws IOException JavaDoc {
259         if (len <= 0)
260             return;
261         char[] out = new char[4];
262         int rindex = off;
263         int rest = len - off;
264         int output = 0;
265         while (rest >= 3) {
266             int i = ((data[rindex] & 0xff) << 16)
267                     + ((data[rindex + 1] & 0xff) << 8)
268                     + (data[rindex + 2] & 0xff);
269             out[0] = S_BASE64CHAR[i >> 18];
270             out[1] = S_BASE64CHAR[(i >> 12) & 0x3f];
271             out[2] = S_BASE64CHAR[(i >> 6) & 0x3f];
272             out[3] = S_BASE64CHAR[i & 0x3f];
273             writer.write(out, 0, 4);
274             rindex += 3;
275             rest -= 3;
276             output += 4;
277             if (output % 76 == 0)
278                 writer.write("\n");
279         }
280         if (rest == 1) {
281             int i = data[rindex] & 0xff;
282             out[0] = S_BASE64CHAR[i >> 2];
283             out[1] = S_BASE64CHAR[(i << 4) & 0x3f];
284             out[2] = S_BASE64PAD;
285             out[3] = S_BASE64PAD;
286             writer.write(out, 0, 4);
287         } else if (rest == 2) {
288             int i = ((data[rindex] & 0xff) << 8) + (data[rindex + 1] & 0xff);
289             out[0] = S_BASE64CHAR[i >> 10];
290             out[1] = S_BASE64CHAR[(i >> 4) & 0x3f];
291             out[2] = S_BASE64CHAR[(i << 2) & 0x3f];
292             out[3] = S_BASE64PAD;
293             writer.write(out, 0, 4);
294         }
295     }
296 }
Popular Tags