KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > Base64


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.internal.preferences;
12
13 /**
14  * Base64 is a helper class for converting byte arrays to and
15  * from base 64 encoded Strings.
16  *
17  */

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

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

124     static int decodeDigit(byte data) {
125         char charData = (char) data;
126         if (charData <= 'Z' && charData >= 'A') {
127             return charData - 'A';
128         }
129         if (charData <= 'z' && charData >= 'a') {
130             return charData - 'a' + 26;
131         }
132         if (charData <= '9' && charData >= '0') {
133             return charData - '0' + 52;
134         }
135         switch (charData) {
136             case '+' :
137                 return 62;
138             case '/' :
139                 return 63;
140             default :
141                 throw new IllegalArgumentException JavaDoc("Invalid char to decode: " + data); //$NON-NLS-1$
142
}
143     }
144
145     /**
146      * This method encodes the byte array into a char array in base 64 according
147      * to the specification given by the RFC 1521 (5.2).
148      *
149      * @param data the encoded char array
150      * @return the byte array that needs to be encoded
151      */

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