KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > slamd > example > Base64Encoder


1 /*
2  * Sun Public License
3  *
4  * The contents of this file are subject to the Sun Public License Version
5  * 1.0 (the "License"). You may not use this file except in compliance with
6  * the License. A copy of the License is available at http://www.sun.com/
7  *
8  * The Original Code is the SLAMD Distributed Load Generation Engine.
9  * The Initial Developer of the Original Code is Neil A. Wilson.
10  * Portions created by Neil A. Wilson are Copyright (C) 2004.
11  * Some preexisting portions Copyright (C) 2002-2004 Sun Microsystems, Inc.
12  * All Rights Reserved.
13  *
14  * Contributor(s): Neil A. Wilson
15  */

16 package com.sun.slamd.example;
17
18
19
20 import java.nio.*;
21
22
23
24 /**
25  * This class provides methods for encoding and decoding data using the base64
26  * specification as defined in RFC 1521, section 5.2. Although a base64 encoder
27  * and decoder are available in the sun.misc package, they should not be used
28  * as classes in the sun.misc package are not guaranteed to exist or work, and
29  * any program that uses them cannot be considered pure Java.
30  *
31  *
32  * @author Neil A. Wilson
33  */

34 public class Base64Encoder
35 {
36   /**
37    * The array of characters that may be used in base64-encoded values.
38    */

39   public static final char[] BASE64_ALPHABET =
40        ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
41         "0123456789+/").toCharArray();
42
43
44
45   /**
46    * Base64-encodes the data in the provided byte array.
47    *
48    * @param byteArray The byte array containing the data to encode.
49    *
50    * @return The base64-encoded value.
51    */

52   public static String JavaDoc encode(byte[] byteArray)
53   {
54     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(4 * byteArray.length / 3);
55
56     int i, j;
57     int iterations = byteArray.length / 3;
58     for (i=0,j=0; j < iterations; j++, i += 3)
59     {
60       int value = ((byteArray[i] & 0x000000FF) << 16) |
61                   ((byteArray[i+1] & 0x000000FF) << 8) |
62                    (byteArray[i+2] & 0x000000FF);
63
64       buffer.append(BASE64_ALPHABET[(value >>> 18) & 0x0000003F]);
65       buffer.append(BASE64_ALPHABET[(value >>> 12) & 0x0000003F]);
66       buffer.append(BASE64_ALPHABET[(value >>> 6) & 0x0000003F]);
67       buffer.append(BASE64_ALPHABET[value & 0x0000003F]);
68     }
69
70     switch (byteArray.length % 3)
71     {
72       case 1:
73         buffer.append(BASE64_ALPHABET[(byteArray[i] >>> 2) & 0x0000003F]);
74         buffer.append(BASE64_ALPHABET[(byteArray[i] << 4) & 0x0000003F]);
75         buffer.append("==");
76         break;
77       case 2:
78         int value = ((byteArray[i] & 0x000000FF) << 8) |
79                      (byteArray[i+1] & 0x000000FF);
80
81         buffer.append(BASE64_ALPHABET[(value >>> 10) & 0x0000003F]);
82         buffer.append(BASE64_ALPHABET[(value >>> 4) & 0x0000003F]);
83         buffer.append(BASE64_ALPHABET[(value << 2) & 0x0000003F]);
84         buffer.append("=");
85         break;
86     }
87
88     return buffer.toString();
89   }
90
91
92
93   /**
94    * Base64-decodes the provided string into a byte array.
95    *
96    * @param encodedValue The string containing the base64-encoded data.
97    *
98    * @return The byte array containing the decoded data.
99    */

100   public static byte[] decode(String JavaDoc encodedValue)
101   {
102     // The encoded value must have a length that is a multiple of four bytes.
103
int length = encodedValue.length();
104     if ((length % 4) != 0)
105     {
106       return null;
107     }
108
109     char[] encodedChars = encodedValue.toCharArray();
110
111     ByteBuffer buffer = ByteBuffer.allocate(length);
112
113     for (int i=0; i < length; i += 4)
114     {
115       boolean append = true;
116       int value = 0;
117       for (int j=0; j < 4; j++)
118       {
119         switch (encodedChars[i+j])
120         {
121           case 'A':
122             value <<= 6;
123             break;
124           case 'B':
125             value = (value << 6) | 0x00000001;
126             break;
127           case 'C':
128             value = (value << 6) | 0x00000002;
129             break;
130           case 'D':
131             value = (value << 6) | 0x00000003;
132             break;
133           case 'E':
134             value = (value << 6) | 0x00000004;
135             break;
136           case 'F':
137             value = (value << 6) | 0x00000005;
138             break;
139           case 'G':
140             value = (value << 6) | 0x00000006;
141             break;
142           case 'H':
143             value = (value << 6) | 0x00000007;
144             break;
145           case 'I':
146             value = (value << 6) | 0x00000008;
147             break;
148           case 'J':
149             value = (value << 6) | 0x00000009;
150             break;
151           case 'K':
152             value = (value << 6) | 0x0000000A;
153             break;
154           case 'L':
155             value = (value << 6) | 0x0000000B;
156             break;
157           case 'M':
158             value = (value << 6) | 0x0000000C;
159             break;
160           case 'N':
161             value = (value << 6) | 0x0000000D;
162             break;
163           case 'O':
164             value = (value << 6) | 0x0000000E;
165             break;
166           case 'P':
167             value = (value << 6) | 0x0000000F;
168             break;
169           case 'Q':
170             value = (value << 6) | 0x00000010;
171             break;
172           case 'R':
173             value = (value << 6) | 0x00000011;
174             break;
175           case 'S':
176             value = (value << 6) | 0x00000012;
177             break;
178           case 'T':
179             value = (value << 6) | 0x00000013;
180             break;
181           case 'U':
182             value = (value << 6) | 0x00000014;
183             break;
184           case 'V':
185             value = (value << 6) | 0x00000015;
186             break;
187           case 'W':
188             value = (value << 6) | 0x00000016;
189             break;
190           case 'X':
191             value = (value << 6) | 0x00000017;
192             break;
193           case 'Y':
194             value = (value << 6) | 0x00000018;
195             break;
196           case 'Z':
197             value = (value << 6) | 0x00000019;
198             break;
199           case 'a':
200             value = (value << 6) | 0x0000001A;
201             break;
202           case 'b':
203             value = (value << 6) | 0x0000001B;
204             break;
205           case 'c':
206             value = (value << 6) | 0x0000001C;
207             break;
208           case 'd':
209             value = (value << 6) | 0x0000001D;
210             break;
211           case 'e':
212             value = (value << 6) | 0x0000001E;
213             break;
214           case 'f':
215             value = (value << 6) | 0x0000001F;
216             break;
217           case 'g':
218             value = (value << 6) | 0x00000020;
219             break;
220           case 'h':
221             value = (value << 6) | 0x00000021;
222             break;
223           case 'i':
224             value = (value << 6) | 0x00000022;
225             break;
226           case 'j':
227             value = (value << 6) | 0x00000023;
228             break;
229           case 'k':
230             value = (value << 6) | 0x00000024;
231             break;
232           case 'l':
233             value = (value << 6) | 0x00000025;
234             break;
235           case 'm':
236             value = (value << 6) | 0x00000026;
237             break;
238           case 'n':
239             value = (value << 6) | 0x00000027;
240             break;
241           case 'o':
242             value = (value << 6) | 0x00000028;
243             break;
244           case 'p':
245             value = (value << 6) | 0x00000029;
246             break;
247           case 'q':
248             value = (value << 6) | 0x0000002A;
249             break;
250           case 'r':
251             value = (value << 6) | 0x0000002B;
252             break;
253           case 's':
254             value = (value << 6) | 0x0000002C;
255             break;
256           case 't':
257             value = (value << 6) | 0x0000002D;
258             break;
259           case 'u':
260             value = (value << 6) | 0x0000002E;
261             break;
262           case 'v':
263             value = (value << 6) | 0x0000002F;
264             break;
265           case 'w':
266             value = (value << 6) | 0x00000030;
267             break;
268           case 'x':
269             value = (value << 6) | 0x00000031;
270             break;
271           case 'y':
272             value = (value << 6) | 0x00000032;
273             break;
274           case 'z':
275             value = (value << 6) | 0x00000033;
276             break;
277           case '0':
278             value = (value << 6) | 0x00000034;
279             break;
280           case '1':
281             value = (value << 6) | 0x00000035;
282             break;
283           case '2':
284             value = (value << 6) | 0x00000036;
285             break;
286           case '3':
287             value = (value << 6) | 0x00000037;
288             break;
289           case '4':
290             value = (value << 6) | 0x00000038;
291             break;
292           case '5':
293             value = (value << 6) | 0x00000039;
294             break;
295           case '6':
296             value = (value << 6) | 0x0000003A;
297             break;
298           case '7':
299             value = (value << 6) | 0x0000003B;
300             break;
301           case '8':
302             value = (value << 6) | 0x0000003C;
303             break;
304           case '9':
305             value = (value << 6) | 0x0000003D;
306             break;
307           case '+':
308             value = (value << 6) | 0x0000003E;
309             break;
310           case '/':
311             value = (value << 6) | 0x0000003F;
312             break;
313           case '=':
314             append = false;
315             switch (j)
316             {
317               case 2:
318                 buffer.put((byte) ((value >>> 4) & 0x000000FF));
319                 break;
320               case 3:
321                 buffer.put((byte) ((value >>> 10) & 0x000000FF));
322                 buffer.put((byte) ((value >>> 2) & 0x000000FF));
323                 break;
324             }
325             break;
326           default:
327             return null;
328         }
329
330         if (! append)
331         {
332           break;
333         }
334       }
335
336       if (append)
337       {
338         buffer.put((byte) ((value >>> 16) & 0x000000FF));
339         buffer.put((byte) ((value >>> 8) & 0x000000FF));
340         buffer.put((byte) (value & 0x000000FF));
341       }
342       else
343       {
344         break;
345       }
346     }
347
348     buffer.flip();
349     byte[] returnArray = new byte[buffer.limit()];
350     buffer.get(returnArray);
351     return returnArray;
352   }
353 }
354
355
Popular Tags