KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > tagkit > Base64


1 //////////////////////license & copyright header/////////////////////////
2
// //
3
// Base64 - encode/decode data using the Base64 encoding scheme //
4
// //
5
// Copyright (c) 1998 by Kevin Kelley //
6
// //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later version. //
11
// //
12
// This library is distributed in the hope that it will be useful, //
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15
// GNU Lesser General Public License for more details. //
16
// //
17
// You should have received a copy of the GNU Lesser General Public //
18
// License along with this library; if not, write to the Free Software //
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA //
20
// 02111-1307, USA, or contact the author: //
21
// //
22
// Kevin Kelley <kelley@ruralnet.net> - 30718 Rd. 28, La Junta, CO, //
23
// 81050 USA. //
24
// //
25
////////////////////end license & copyright header///////////////////////
26

27 package soot.tagkit;
28
29 /**
30 * Provides encoding of raw bytes to base64-encoded characters, and
31 * decoding of base64 characters to raw bytes.
32 *
33 * @author Kevin Kelley (kelley@ruralnet.net)
34 * @version 1.3
35 * @date 06 August 1998
36 * @modified 14 February 2000
37 * @modified 22 September 2000
38 */

39 public class Base64 {
40
41 /**
42 * returns an array of base64-encoded characters to represent the
43 * passed data array.
44 *
45 * @param data the array of bytes to encode
46 * @return base64-coded character array.
47 */

48 static public char[] encode(byte[] data)
49 {
50     char[] out = new char[((data.length + 2) / 3) * 4];
51
52     //
53
// 3 bytes encode to 4 chars. Output is always an even
54
// multiple of 4 characters.
55
//
56
for (int i=0, index=0; i<data.length; i+=3, index+=4) {
57         boolean quad = false;
58         boolean trip = false;
59
60         int val = (0xFF & (int) data[i]);
61         val <<= 8;
62         if ((i+1) < data.length) {
63             val |= (0xFF & (int) data[i+1]);
64             trip = true;
65         }
66         val <<= 8;
67         if ((i+2) < data.length) {
68             val |= (0xFF & (int) data[i+2]);
69             quad = true;
70         }
71         out[index+3] = alphabet[(quad? (val & 0x3F): 64)];
72         val >>= 6;
73         out[index+2] = alphabet[(trip? (val & 0x3F): 64)];
74         val >>= 6;
75         out[index+1] = alphabet[val & 0x3F];
76         val >>= 6;
77         out[index+0] = alphabet[val & 0x3F];
78     }
79     return out;
80 }
81
82   /**
83    * Decodes a BASE-64 encoded stream to recover the original
84    * data. White space before and after will be trimmed away,
85    * but no other manipulation of the input will be performed.
86    *
87    * As of version 1.2 this method will properly handle input
88    * containing junk characters (newlines and the like) rather
89    * than throwing an error. It does this by pre-parsing the
90    * input and generating from that a count of VALID input
91    * characters.
92    **/

93 static public byte[] decode(char[] data)
94 {
95     // as our input could contain non-BASE64 data (newlines,
96
// whitespace of any sort, whatever) we must first adjust
97
// our count of USABLE data so that...
98
// (a) we don't misallocate the output array, and
99
// (b) think that we miscalculated our data length
100
// just because of extraneous throw-away junk
101

102     int tempLen = data.length;
103     for( int ix=0; ix<data.length; ix++ )
104     {
105         if( (data[ix] > 255) || codes[ data[ix] ] < 0 )
106             --tempLen; // ignore non-valid chars and padding
107
}
108     // calculate required length:
109
// -- 3 bytes for every 4 valid base64 chars
110
// -- plus 2 bytes if there are 3 extra base64 chars,
111
// or plus 1 byte if there are 2 extra.
112

113     int len = (tempLen / 4) * 3;
114     if ((tempLen % 4) == 3) len += 2;
115     if ((tempLen % 4) == 2) len += 1;
116
117     byte[] out = new byte[len];
118
119
120
121     int shift = 0; // # of excess bits stored in accum
122
int accum = 0; // excess bits
123
int index = 0;
124
125     // we now go through the entire array (NOT using the 'tempLen' value)
126
for (int ix=0; ix<data.length; ix++)
127     {
128         int value = (data[ix]>255)? -1: codes[ data[ix] ];
129
130         if ( value >= 0 ) // skip over non-code
131
{
132             accum <<= 6; // bits shift up by 6 each time thru
133
shift += 6; // loop, with new bits being put in
134
accum |= value; // at the bottom.
135
if ( shift >= 8 ) // whenever there are 8 or more shifted in,
136
{
137                 shift -= 8; // write them out (from the top, leaving any
138
out[index++] = // excess at the bottom for next iteration.
139
(byte) ((accum >> shift) & 0xff);
140             }
141         }
142         // we will also have skipped processing a padding null byte ('=') here;
143
// these are used ONLY for padding to an even length and do not legally
144
// occur as encoded data. for this reason we can ignore the fact that
145
// no index++ operation occurs in that special case: the out[] array is
146
// initialized to all-zero bytes to start with and that works to our
147
// advantage in this combination.
148
}
149
150     // if there is STILL something wrong we just have to throw up now!
151
if( index != out.length)
152     {
153         throw new Error JavaDoc("Miscalculated data length (wrote " + index + " instead of " + out.length + ")");
154     }
155
156     return out;
157 }
158
159
160 //
161
// code characters for values 0..63
162
//
163
static private char[] alphabet =
164     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
165         .toCharArray();
166
167 //
168
// lookup table for converting base64 characters to value in range 0..63
169
//
170
static private byte[] codes = new byte[256];
171 static {
172     for (int i=0; i<256; i++) codes[i] = -1;
173     for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A');
174     for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a');
175     for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0');
176     codes['+'] = 62;
177     codes['/'] = 63;
178 }
179
180 }
181
Popular Tags