KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > util > Base64


1 package net.matuschek.util;
2
3 /***************************************************************
4
5 Copyright (c) 1998, 1999 Nate Sammons <nate@protomatter.com>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20
21 Contact support@protomatter.com with your questions, comments,
22 gripes, praise, etc...
23
24 ***************************************************************/

25
26
27 /***************************************************************
28   - moved to the net.matuschek.util tree by Daniel Matuschek
29   - replaced deprecated getBytes() method in method decode
30   - added String encode(String) method to encode a String to
31     base64
32  ***************************************************************/

33
34 /**
35  * Base64 encoder/decoder. Does not stream, so be careful with
36  * using large amounts of data
37  *
38  * @author Nate Sammons
39  * @author Daniel Matuschek
40  * @version $Id: Base64.java,v 1.4 2001/04/17 10:09:27 matuschd Exp $
41  */

42 public class Base64
43 {
44
45   private Base64()
46   {
47     super();
48   }
49
50   /**
51    * Encode some data and return a String.
52    */

53   public final static String JavaDoc encode(byte[] d)
54   {
55     if (d == null) return null;
56     byte data[] = new byte[d.length+2];
57     System.arraycopy(d, 0, data, 0, d.length);
58     byte dest[] = new byte[(data.length/3)*4];
59
60     // 3-byte to 4-byte conversion
61
for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4)
62     {
63       dest[didx] = (byte) ((data[sidx] >>> 2) & 077);
64       dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 |
65                   (data[sidx] << 4) & 077);
66       dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 |
67                   (data[sidx+1] << 2) & 077);
68       dest[didx+3] = (byte) (data[sidx+2] & 077);
69     }
70
71     // 0-63 to ascii printable conversion
72
for (int idx = 0; idx <dest.length; idx++)
73     {
74       if (dest[idx] < 26) dest[idx] = (byte)(dest[idx] + 'A');
75       else if (dest[idx] < 52) dest[idx] = (byte)(dest[idx] + 'a' - 26);
76       else if (dest[idx] < 62) dest[idx] = (byte)(dest[idx] + '0' - 52);
77       else if (dest[idx] < 63) dest[idx] = (byte)'+';
78       else dest[idx] = (byte)'/';
79     }
80
81     // add padding
82
for (int idx = dest.length-1; idx > (d.length*4)/3; idx--)
83     {
84       dest[idx] = (byte)'=';
85     }
86     return new String JavaDoc(dest);
87   }
88
89   /**
90    * Encode a String using Base64 using the default platform encoding
91    **/

92   public final static String JavaDoc encode(String JavaDoc s) {
93     return encode(s.getBytes());
94   }
95
96   /**
97    * Decode data and return bytes.
98    */

99   public final static byte[] decode(String JavaDoc str)
100   {
101     if (str == null) return null;
102     byte data[] = str.getBytes();
103     return decode(data);
104   }
105
106   /**
107    * Decode data and return bytes. Assumes that the data passed
108    * in is ASCII text.
109    */

110   public final static byte[] decode(byte[] data)
111   {
112     int tail = data.length;
113     while (data[tail-1] == '=') tail--;
114     byte dest[] = new byte[tail - data.length/4];
115
116     // ascii printable to 0-63 conversion
117
for (int idx = 0; idx <data.length; idx++)
118     {
119       if (data[idx] == '=') data[idx] = 0;
120       else if (data[idx] == '/') data[idx] = 63;
121       else if (data[idx] == '+') data[idx] = 62;
122       else if (data[idx] >= '0' && data[idx] <= '9')
123         data[idx] = (byte)(data[idx] - ('0' - 52));
124       else if (data[idx] >= 'a' && data[idx] <= 'z')
125         data[idx] = (byte)(data[idx] - ('a' - 26));
126       else if (data[idx] >= 'A' && data[idx] <= 'Z')
127         data[idx] = (byte)(data[idx] - 'A');
128     }
129
130     // 4-byte to 3-byte conversion
131
int sidx, didx;
132     for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3)
133     {
134       dest[didx] = (byte) ( ((data[sidx] << 2) & 255) |
135               ((data[sidx+1] >>> 4) & 3) );
136       dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) |
137               ((data[sidx+2] >>> 2) & 017) );
138       dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) |
139               (data[sidx+3] & 077) );
140     }
141     if (didx < dest.length)
142     {
143       dest[didx] = (byte) ( ((data[sidx] << 2) & 255) |
144               ((data[sidx+1] >>> 4) & 3) );
145     }
146     if (++didx < dest.length)
147     {
148       dest[didx] = (byte) ( ((data[sidx+1] << 4) & 255) |
149               ((data[sidx+2] >>> 2) & 017) );
150     }
151     return dest;
152   }
153
154   /**
155    * A simple test that encodes and decodes the first commandline argument.
156    */

157   public static final void main(String JavaDoc[] args)
158   {
159     if (args.length != 1)
160     {
161       System.out.println("Usage: Base64 string");
162       System.exit(0);
163     }
164     try
165     {
166       String JavaDoc e = Base64.encode(args[0].getBytes());
167       String JavaDoc d = new String JavaDoc(Base64.decode(e));
168       System.out.println("Input = '" + args[0] + "'");
169       System.out.println("Encoded = '" + e + "'");
170       System.out.println("Decoded = '" + d + "'");
171     }
172     catch (Exception JavaDoc x)
173     {
174       x.printStackTrace();
175     }
176   }
177 }
178
Popular Tags