KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > Base64Utils


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security;
8
9 /** Base64 encoding/decoding utilities
10  *
11  * @author Scott.Stark@jboss.org
12  * @version $Revison:$
13  */

14 public class Base64Utils
15 {
16    private static final char[] base64Table =
17    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./".toCharArray();
18    public static final String JavaDoc BASE64_ENCODING = "BASE64";
19    public static final String JavaDoc BASE16_ENCODING = "HEX";
20
21    // These functions assume that the byte array has MSB at 0, LSB at end.
22
// Reverse the byte array (not the String) if this is not the case.
23
// All base64 strings are in natural order, least significant digit last.
24
public static String JavaDoc tob64(byte[] buffer)
25    {
26       boolean notleading = false;
27       int len = buffer.length, pos = len % 3, c;
28       byte b0 = 0, b1 = 0, b2 = 0;
29       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
30
31       switch(pos)
32       {
33          case 1:
34             b2 = buffer[0];
35             break;
36          case 2:
37             b1 = buffer[0];
38             b2 = buffer[1];
39             break;
40       }
41       do
42       {
43          c = (b0 & 0xfc) >>> 2;
44          if(notleading || c != 0)
45          {
46             sb.append(base64Table[c]);
47             notleading = true;
48          }
49          c = ((b0 & 3) << 4) | ((b1 & 0xf0) >>> 4);
50          if(notleading || c != 0)
51          {
52             sb.append(base64Table[c]);
53             notleading = true;
54          }
55          c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >>> 6);
56          if(notleading || c != 0)
57          {
58             sb.append(base64Table[c]);
59             notleading = true;
60          }
61          c = b2 & 0x3f;
62          if(notleading || c != 0)
63          {
64             sb.append(base64Table[c]);
65             notleading = true;
66          }
67          if(pos >= len)
68             break;
69          else
70          {
71             try
72             {
73                b0 = buffer[pos++];
74                b1 = buffer[pos++];
75                b2 = buffer[pos++];
76             }
77             catch(ArrayIndexOutOfBoundsException JavaDoc e)
78             {
79                break;
80             }
81          }
82       } while(true);
83
84       if(notleading)
85          return sb.toString();
86       else
87          return "0";
88    }
89
90    public static byte[] fromb64(String JavaDoc str) throws NumberFormatException JavaDoc
91    {
92       int len = str.length();
93       if(len == 0)
94          throw new NumberFormatException JavaDoc("Empty Base64 string");
95
96       byte[] a = new byte[len + 1];
97       char c;
98       int i, j;
99
100       for(i = 0; i < len; ++i)
101       {
102          c = str.charAt(i);
103          try
104          {
105             for(j = 0; c != base64Table[j]; ++j)
106                ;
107          } catch(Exception JavaDoc e)
108          {
109             throw new NumberFormatException JavaDoc("Illegal Base64 character");
110          }
111          a[i] = (byte) j;
112       }
113
114       i = len - 1;
115       j = len;
116       try
117       {
118          while(true)
119          {
120             a[j] = a[i];
121             if(--i < 0)
122                break;
123             a[j] |= (a[i] & 3) << 6;
124             --j;
125             a[j] = (byte) ((a[i] & 0x3c) >>> 2);
126             if(--i < 0)
127                break;
128             a[j] |= (a[i] & 0xf) << 4;
129             --j;
130             a[j] = (byte) ((a[i] & 0x30) >>> 4);
131             if(--i < 0)
132                break;
133             a[j] |= (a[i] << 2);
134
135             // Nasty, evil bug in Microsloth's Java interpreter under
136
// Netscape: The following three lines of code are supposed
137
// to be equivalent, but under the Windows NT VM (Netscape3.0)
138
// using either of the two commented statements would cause
139
// the zero to be placed in a[j] *before* decrementing j.
140
// Weeeeird.
141
a[j-1] = 0; --j;
142             // a[--j] = 0;
143
// --j; a[j] = 0;
144

145             if(--i < 0)
146                break;
147          }
148       }
149       catch(Exception JavaDoc e)
150       {
151
152       }
153
154       try
155       {
156          while(a[j] == 0)
157             ++j;
158       }
159       catch(Exception JavaDoc e)
160       {
161          return new byte[1];
162       }
163
164       byte[] result = new byte[len - j + 1];
165       System.arraycopy(a, j, result, 0, len - j + 1);
166       return result;
167    }
168
169 }
170
Popular Tags