KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8
9 package org.jboss.axis.utils;
10
11 /**
12  * Unceremoniously lifted from
13  * jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Base64.java
14  * Revision 1.8 2002/01/05 11:15:59
15  * <p/>
16  * Eliminates dependency on sun.misc.Base64Encoding,
17  * which isn't appreciated by the J2EE security manager.
18  * (it's an access exception to load sun.misc.* classes
19  * in application code).
20  * <p/>
21  * <p>Base64 encoder and decoder.</p>
22  * <p/>
23  * This class provides encoding/decoding methods for
24  * the Base64 encoding as defined by RFC 2045,
25  * N. Freed and N. Borenstein.
26  * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
27  * Part One: Format of Internet Message Bodies. Reference
28  * 1996. Available at: http://www.ietf.org/rfc/rfc2045.txt
29  * </p>
30  *
31  * @author Jeffrey Rodriguez
32  * @version $Revision: 1.1.2.2 $ $Date: 2005/04/06 20:29:09 $
33  */

34 final class Base64
35 {
36    private static final int BASELENGTH = 255;
37    private static final int LOOKUPLENGTH = 64;
38    private static final int TWENTYFOURBITGROUP = 24;
39    private static final int EIGHTBIT = 8;
40    private static final int SIXTEENBIT = 16;
41    private static final int SIXBIT = 6;
42    private static final int FOURBYTE = 4;
43    private static final int SIGN = -128;
44    private static final byte PAD = (byte)'=';
45    private static byte[] base64Alphabet = new byte[BASELENGTH];
46    private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
47
48    static
49    {
50
51       for (int i = 0; i < BASELENGTH; i++)
52       {
53          base64Alphabet[i] = -1;
54       }
55       for (int i = 'Z'; i >= 'A'; i--)
56       {
57          base64Alphabet[i] = (byte)(i - 'A');
58       }
59       for (int i = 'z'; i >= 'a'; i--)
60       {
61          base64Alphabet[i] = (byte)(i - 'a' + 26);
62       }
63
64       for (int i = '9'; i >= '0'; i--)
65       {
66          base64Alphabet[i] = (byte)(i - '0' + 52);
67       }
68
69       base64Alphabet['+'] = 62;
70       base64Alphabet['/'] = 63;
71
72       for (int i = 0; i <= 25; i++)
73       {
74          lookUpBase64Alphabet[i] = (byte)('A' + i);
75       }
76
77       for (int i = 26, j = 0; i <= 51; i++, j++)
78       {
79          lookUpBase64Alphabet[i] = (byte)('a' + j);
80       }
81
82       for (int i = 52, j = 0; i <= 61; i++, j++)
83       {
84          lookUpBase64Alphabet[i] = (byte)('0' + j);
85       }
86       lookUpBase64Alphabet[62] = (byte)'+';
87       lookUpBase64Alphabet[63] = (byte)'/';
88
89    }
90
91    static boolean isBase64(String JavaDoc isValidString)
92    {
93       return (isArrayByteBase64(isValidString.getBytes()));
94    }
95
96
97    static boolean isBase64(byte octect)
98    {
99       // Should we ignore white space?
100
return (octect == PAD || base64Alphabet[octect] != -1);
101    }
102
103
104    static boolean isArrayByteBase64(byte[] arrayOctect)
105    {
106       int length = arrayOctect.length;
107       if (length == 0)
108       {
109          return true;
110       }
111       for (int i = 0; i < length; i++)
112       {
113          if (Base64.isBase64(arrayOctect[i]) == false)
114          {
115             return false;
116          }
117       }
118       return true;
119    }
120
121    /**
122     * Encodes hex octects into Base64
123     *
124     * @param binaryData Array containing binaryData
125     * @return Base64-encoded array
126     */

127    static byte[] encode(byte[] binaryData)
128    {
129
130       int lengthDataBits = binaryData.length * EIGHTBIT;
131       int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
132       int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
133       byte encodedData[] = null;
134
135
136       if (fewerThan24bits != 0)
137       {
138          //data not divisible by 24 bit
139
encodedData = new byte[(numberTriplets + 1) * 4];
140       }
141       else
142       {
143          // 16 or 8 bit
144
encodedData = new byte[numberTriplets * 4];
145       }
146
147       byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
148
149       int encodedIndex = 0;
150       int dataIndex = 0;
151       int i = 0;
152       for (i = 0; i < numberTriplets; i++)
153       {
154
155          dataIndex = i * 3;
156          b1 = binaryData[dataIndex];
157          b2 = binaryData[dataIndex + 1];
158          b3 = binaryData[dataIndex + 2];
159
160          l = (byte)(b2 & 0x0f);
161          k = (byte)(b1 & 0x03);
162
163          encodedIndex = i * 4;
164          byte val1 = ((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0);
165
166          byte val2 = ((b2 & SIGN) == 0) ? (byte)(b2 >> 4) : (byte)((b2) >> 4 ^ 0xf0);
167          byte val3 = ((b3 & SIGN) == 0) ? (byte)(b3 >> 6) : (byte)((b3) >> 6 ^ 0xfc);
168
169          encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
170          encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)];
171          encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2) | val3];
172          encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f];
173       }
174
175       // form integral number of 6-bit groups
176
dataIndex = i * 3;
177       encodedIndex = i * 4;
178       if (fewerThan24bits == EIGHTBIT)
179       {
180          b1 = binaryData[dataIndex];
181          k = (byte)(b1 & 0x03);
182          byte val1 = ((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0);
183          encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
184          encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4];
185          encodedData[encodedIndex + 2] = PAD;
186          encodedData[encodedIndex + 3] = PAD;
187       }
188       else if (fewerThan24bits == SIXTEENBIT)
189       {
190          b1 = binaryData[dataIndex];
191          b2 = binaryData[dataIndex + 1];
192          l = (byte)(b2 & 0x0f);
193          k = (byte)(b1 & 0x03);
194
195          byte val1 = ((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0);
196          byte val2 = ((b2 & SIGN) == 0) ? (byte)(b2 >> 4) : (byte)((b2) >> 4 ^ 0xf0);
197
198          encodedData[encodedIndex] = lookUpBase64Alphabet[val1];
199          encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)];
200          encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2];
201          encodedData[encodedIndex + 3] = PAD;
202       }
203       return encodedData;
204    }
205
206
207    /**
208     * Decodes Base64 data into octects
209     *
210     * @param base64Data Byte array containing Base64 data
211     * @return Array containing decoded data.
212     */

213    static byte[] decode(byte[] base64Data)
214    {
215       // Should we throw away anything not in base64Data ?
216

217       // handle the edge case, so we don't have to worry about it later
218
if (base64Data.length == 0)
219       {
220          return new byte[0];
221       }
222
223       int numberQuadruple = base64Data.length / FOURBYTE;
224       byte decodedData[] = null;
225       byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0;
226
227       int encodedIndex = 0;
228       int dataIndex = 0;
229       {
230          // this block sizes the output array properly - rlw
231
int lastData = base64Data.length;
232          // ignore the '=' padding
233
while (base64Data[lastData - 1] == PAD)
234          {
235             if (--lastData == 0)
236             {
237                return new byte[0];
238             }
239          }
240          decodedData = new byte[lastData - numberQuadruple];
241       }
242
243       for (int i = 0; i < numberQuadruple; i++)
244       {
245          dataIndex = i * 4;
246          marker0 = base64Data[dataIndex + 2];
247          marker1 = base64Data[dataIndex + 3];
248
249          b1 = base64Alphabet[base64Data[dataIndex]];
250          b2 = base64Alphabet[base64Data[dataIndex + 1]];
251
252          if (marker0 != PAD && marker1 != PAD)
253          { //No PAD e.g 3cQl
254
b3 = base64Alphabet[marker0];
255             b4 = base64Alphabet[marker1];
256
257             decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4);
258             decodedData[encodedIndex + 1] = (byte)(((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
259             decodedData[encodedIndex + 2] = (byte)(b3 << 6 | b4);
260          }
261          else if (marker0 == PAD)
262          { //Two PAD e.g. 3c[Pad][Pad]
263
decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4);
264          }
265          else if (marker1 == PAD)
266          { //One PAD e.g. 3cQ[Pad]
267
b3 = base64Alphabet[marker0];
268
269             decodedData[encodedIndex] = (byte)(b1 << 2 | b2 >> 4);
270             decodedData[encodedIndex + 1] = (byte)(((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
271          }
272          encodedIndex += 3;
273       }
274       return decodedData;
275    }
276
277 }
278
Popular Tags