2 * A Base64 Encoder/Decoder.
5 * This class is used to encode and decode data in Base64 format as described in RFC 1521.
8 * This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br>
9 * It is provided "as is" without warranty of any kind.<br>
10 * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br>
11 * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
14 * Version history:<br>
15 * 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
16 * 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
17 * 2006-11-21 chdh:<br>
18 * Method encode(String) renamed to encodeString(String).<br>
19 * Method decode(String) renamed to decodeString(String).<br>
20 * New method encode(byte[],int) added.<br>
21 * New method decode(String) added.<br>
25 public class Base64Coder
{
27 // Mapping table from 6-bit nibbles to Base64 characters.
28 private static char[] map1
= new char[64];
31 for (char c
='A'; c
<='Z'; c
++) map1
[i
++] = c
;
32 for (char c
='a'; c
<='z'; c
++) map1
[i
++] = c
;
33 for (char c
='0'; c
<='9'; c
++) map1
[i
++] = c
;
34 map1
[i
++] = '+'; map1
[i
++] = '/'; }
36 // Mapping table from Base64 characters to 6-bit nibbles.
37 private static byte[] map2
= new byte[128];
39 for (int i
=0; i
<map2
.length
; i
++) map2
[i
] = -1;
40 for (int i
=0; i
<64; i
++) map2
[map1
[i
]] = (byte)i
; }
43 * Encodes a string into Base64 format.
44 * No blanks or line breaks are inserted.
45 * @param s a String to be encoded.
46 * @return A String with the Base64 encoded data.
48 public static String
encodeString (String s
) {
49 return new String(encode(s
.getBytes())); }
52 * Encodes a byte array into Base64 format.
53 * No blanks or line breaks are inserted.
54 * @param in an array containing the data bytes to be encoded.
55 * @return A character array with the Base64 encoded data.
57 public static char[] encode (byte[] in
) {
58 return encode(in
,in
.length
); }
61 * Encodes a byte array into Base64 format.
62 * No blanks or line breaks are inserted.
63 * @param in an array containing the data bytes to be encoded.
64 * @param iLen number of bytes to process in <code>in</code>.
65 * @return A character array with the Base64 encoded data.
67 public static char[] encode (byte[] in
, int iLen
) {
68 int oDataLen
= (iLen
*4+2)/3; // output length without padding
69 int oLen
= ((iLen
+2)/3)*4; // output length including padding
70 char[] out
= new char[oLen
];
74 int i0
= in
[ip
++] & 0xff;
75 int i1
= ip
< iLen ? in
[ip
++] & 0xff : 0;
76 int i2
= ip
< iLen ? in
[ip
++] & 0xff : 0;
78 int o1
= ((i0
& 3) << 4) | (i1
>>> 4);
79 int o2
= ((i1
& 0xf) << 2) | (i2
>>> 6);
83 out
[op
] = op
< oDataLen ? map1
[o2
] : '='; op
++;
84 out
[op
] = op
< oDataLen ? map1
[o3
] : '='; op
++; }
88 * Decodes a string from Base64 format.
89 * @param s a Base64 String to be decoded.
90 * @return A String containing the decoded data.
91 * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
93 public static String
decodeString (String s
) {
94 if(s
.equals("nil")) return("");
95 return new String(decode(s
)); }
98 * Decodes a byte array from Base64 format.
99 * @param s a Base64 String to be decoded.
100 * @return An array containing the decoded data bytes.
101 * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
103 public static byte[] decode (String s
) {
104 return decode(s
.toCharArray()); }
107 * Decodes a byte array from Base64 format.
108 * No blanks or line breaks are allowed within the Base64 encoded data.
109 * @param in a character array containing the Base64 encoded data.
110 * @return An array containing the decoded data bytes.
111 * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
113 public static byte[] decode (char[] in
) {
114 int iLen
= in
.length
;
115 if (iLen
%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
116 while (iLen
> 0 && in
[iLen
-1] == '=') iLen
--;
117 int oLen
= (iLen
*3) / 4;
118 byte[] out
= new byte[oLen
];
124 int i2
= ip
< iLen ? in
[ip
++] : 'A';
125 int i3
= ip
< iLen ? in
[ip
++] : 'A';
126 if (i0
> 127 || i1
> 127 || i2
> 127 || i3
> 127)
127 throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
132 if (b0
< 0 || b1
< 0 || b2
< 0 || b3
< 0)
133 throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
134 int o0
= ( b0
<<2) | (b1
>>>4);
135 int o1
= ((b1
& 0xf)<<4) | (b2
>>>2);
136 int o2
= ((b2
& 3)<<6) | b3
;
137 out
[op
++] = (byte)o0
;
138 if (op
<oLen
) out
[op
++] = (byte)o1
;
139 if (op
<oLen
) out
[op
++] = (byte)o2
; }
142 // Dummy constructor.
143 private Base64Coder() {}
145 } // end class Base64Coder