KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > verifier > Base64


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.internal.verifier;
12
13 public class Base64 {
14
15     private static final byte equalSign = (byte) '=';
16
17     static char digits[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', //
18
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', //
19
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', //
20
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
21
22     /**
23      * This method decodes the byte array in base 64 encoding into a char array
24      * Base 64 encoding has to be according to the specification given by the
25      * RFC 1521 (5.2).
26      *
27      * @param data the encoded byte array
28      * @return the decoded byte array
29      */

30     public static byte[] decode(byte[] data) {
31         if (data.length == 0)
32             return data;
33         int lastRealDataIndex = data.length - 1;
34         while (data[lastRealDataIndex] == equalSign)
35             lastRealDataIndex--;
36         // original data digit is 8 bits long, but base64 digit is 6 bits long
37
int padBytes = data.length - 1 - lastRealDataIndex;
38         int byteLength = data.length * 6 / 8 - padBytes;
39         byte[] result = new byte[byteLength];
40         // Each 4 bytes of input (encoded) we end up with 3 bytes of output
41
int dataIndex = 0;
42         int resultIndex = 0;
43         int allBits = 0;
44         // how many result chunks we can process before getting to pad bytes
45
int resultChunks = (lastRealDataIndex + 1) / 4;
46         for (int i = 0; i < resultChunks; i++) {
47             allBits = 0;
48             // Loop 4 times gathering input bits (4 * 6 = 24)
49
for (int j = 0; j < 4; j++)
50                 allBits = (allBits << 6) | decodeDigit(data[dataIndex++]);
51             // Loop 3 times generating output bits (3 * 8 = 24)
52
for (int j = resultIndex + 2; j >= resultIndex; j--) {
53                 result[j] = (byte) (allBits & 0xff); // Bottom 8 bits
54
allBits = allBits >>> 8;
55             }
56             resultIndex += 3; // processed 3 result bytes
57
}
58         // Now we do the extra bytes in case the original (non-encoded) data
59
// was not multiple of 3 bytes
60
switch (padBytes) {
61             case 1 :
62                 // 1 pad byte means 3 (4-1) extra Base64 bytes of input, 18
63
// bits, of which only 16 are meaningful
64
// Or: 2 bytes of result data
65
allBits = 0;
66                 // Loop 3 times gathering input bits
67
for (int j = 0; j < 3; j++)
68                     allBits = (allBits << 6) | decodeDigit(data[dataIndex++]);
69                 // NOTE - The code below ends up being equivalent to allBits =
70
// allBits>>>2
71
// But we code it in a non-optimized way for clarity
72
// The 4th, missing 6 bits are all 0
73
allBits = allBits << 6;
74                 // The 3rd, missing 8 bits are all 0
75
allBits = allBits >>> 8;
76                 // Loop 2 times generating output bits
77
for (int j = resultIndex + 1; j >= resultIndex; j--) {
78                     result[j] = (byte) (allBits & 0xff); // Bottom 8
79
// bits
80
allBits = allBits >>> 8;
81                 }
82                 break;
83             case 2 :
84                 // 2 pad bytes mean 2 (4-2) extra Base64 bytes of input, 12 bits
85
// of data, of which only 8 are meaningful
86
// Or: 1 byte of result data
87
allBits = 0;
88                 // Loop 2 times gathering input bits
89
for (int j = 0; j < 2; j++)
90                     allBits = (allBits << 6) | decodeDigit(data[dataIndex++]);
91                 // NOTE - The code below ends up being equivalent to allBits =
92
// allBits>>>4
93
// But we code it in a non-optimized way for clarity
94
// The 3rd and 4th, missing 6 bits are all 0
95
allBits = allBits << 6;
96                 allBits = allBits << 6;
97                 // The 3rd and 4th, missing 8 bits are all 0
98
allBits = allBits >>> 8;
99                 allBits = allBits >>> 8;
100                 result[resultIndex] = (byte) (allBits & 0xff); // Bottom
101
// 8
102
// bits
103
break;
104         }
105         return result;
106     }
107
108     /**
109      * This method converts a Base 64 digit to its numeric value.
110      *
111      * @param data digit (character) to convert
112      * @return value for the digit
113      */

114     static int decodeDigit(byte data) {
115         char charData = (char) data;
116         if (charData <= 'Z' && charData >= 'A')
117             return charData - 'A';
118         if (charData <= 'z' && charData >= 'a')
119             return charData - 'a' + 26;
120         if (charData <= '9' && charData >= '0')
121             return charData - '0' + 52;
122         switch (charData) {
123             case '+' :
124                 return 62;
125             case '/' :
126                 return 63;
127             default :
128                 throw new IllegalArgumentException JavaDoc("Invalid char to decode: " + data); //$NON-NLS-1$
129
}
130     }
131
132     /**
133      * This method encodes the byte array into a char array in base 64 according
134      * to the specification given by the RFC 1521 (5.2).
135      *
136      * @param data the encoded char array
137      * @return the byte array that needs to be encoded
138      */

139     public static byte[] encode(byte[] data) {
140         int sourceChunks = data.length / 3;
141         int len = ((data.length + 2) / 3) * 4;
142         byte[] result = new byte[len];
143         int extraBytes = data.length - (sourceChunks * 3);
144         // Each 4 bytes of input (encoded) we end up with 3 bytes of output
145
int dataIndex = 0;
146         int resultIndex = 0;
147         int allBits = 0;
148         for (int i = 0; i < sourceChunks; i++) {
149             allBits = 0;
150             // Loop 3 times gathering input bits (3 * 8 = 24)
151
for (int j = 0; j < 3; j++)
152                 allBits = (allBits << 8) | (data[dataIndex++] & 0xff);
153             // Loop 4 times generating output bits (4 * 6 = 24)
154
for (int j = resultIndex + 3; j >= resultIndex; j--) {
155                 result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
156
// 6
157
// bits
158
allBits = allBits >>> 6;
159             }
160             resultIndex += 4; // processed 4 result bytes
161
}
162         // Now we do the extra bytes in case the original (non-encoded) data
163
// is not multiple of 4 bytes
164
switch (extraBytes) {
165             case 1 :
166                 allBits = data[dataIndex++]; // actual byte
167
allBits = allBits << 8; // 8 bits of zeroes
168
allBits = allBits << 8; // 8 bits of zeroes
169
// Loop 4 times generating output bits (4 * 6 = 24)
170
for (int j = resultIndex + 3; j >= resultIndex; j--) {
171                     result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
172
// 6
173
// bits
174
allBits = allBits >>> 6;
175                 }
176                 // 2 pad tags
177
result[result.length - 1] = (byte) '=';
178                 result[result.length - 2] = (byte) '=';
179                 break;
180             case 2 :
181                 allBits = data[dataIndex++]; // actual byte
182
allBits = (allBits << 8) | (data[dataIndex++] & 0xff); // actual
183
// byte
184
allBits = allBits << 8; // 8 bits of zeroes
185
// Loop 4 times generating output bits (4 * 6 = 24)
186
for (int j = resultIndex + 3; j >= resultIndex; j--) {
187                     result[j] = (byte) digits[(allBits & 0x3f)]; // Bottom
188
// 6
189
// bits
190
allBits = allBits >>> 6;
191                 }
192                 // 1 pad tag
193
result[result.length - 1] = (byte) '=';
194                 break;
195         }
196         return result;
197     }
198 }
Popular Tags