1 | package de.ugoe.cs.swe.memos.misc; |
---|
2 | |
---|
3 | //Copyright 2003-2009 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland |
---|
4 | //www.source-code.biz, www.inventec.ch/chdh |
---|
5 | // |
---|
6 | //This module is multi-licensed and may be used under the terms |
---|
7 | //of any of the following licenses: |
---|
8 | // |
---|
9 | //EPL, Eclipse Public License, http://www.eclipse.org/legal |
---|
10 | //LGPL, GNU Lesser General Public License, http://www.gnu.org/licenses/lgpl.html |
---|
11 | //AL, Apache License, http://www.apache.org/licenses |
---|
12 | //BSD, BSD License, http://www.opensource.org/licenses/bsd-license.php |
---|
13 | // |
---|
14 | //Please contact the author if you need another license. |
---|
15 | //This module is provided "as is", without warranties of any kind. |
---|
16 | |
---|
17 | /** |
---|
18 | * A Base64 Encoder/Decoder. |
---|
19 | * |
---|
20 | * <p> |
---|
21 | * This class is used to encode and decode data in Base64 format as described in |
---|
22 | * RFC 1521. |
---|
23 | * |
---|
24 | * <p> |
---|
25 | * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br> |
---|
26 | * Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland<br> |
---|
27 | * Multi-licensed: EPL/LGPL/AL/BSD. |
---|
28 | * |
---|
29 | * <p> |
---|
30 | * Version history:<br> |
---|
31 | * 2003-07-22 Christian d'Heureuse (chdh): Module created.<br> |
---|
32 | * 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br> |
---|
33 | * 2006-11-21 chdh:<br> |
---|
34 | * Method encode(String) renamed to encodeString(String).<br> |
---|
35 | * Method decode(String) renamed to decodeString(String).<br> |
---|
36 | * New method encode(byte[],int) added.<br> |
---|
37 | * New method decode(String) added.<br> |
---|
38 | * 2009-07-16: Additional licenses (EPL/AL) added.<br> |
---|
39 | * 2009-09-16: Additional license (BSD) added.<br> |
---|
40 | */ |
---|
41 | |
---|
42 | public class Base64Coder { |
---|
43 | |
---|
44 | // Mapping table from 6-bit nibbles to Base64 characters. |
---|
45 | private static char[] map1 = new char[64]; |
---|
46 | static { |
---|
47 | int i = 0; |
---|
48 | for (char c = 'A'; c <= 'Z'; c++) |
---|
49 | map1[i++] = c; |
---|
50 | for (char c = 'a'; c <= 'z'; c++) |
---|
51 | map1[i++] = c; |
---|
52 | for (char c = '0'; c <= '9'; c++) |
---|
53 | map1[i++] = c; |
---|
54 | map1[i++] = '+'; |
---|
55 | map1[i++] = '/'; |
---|
56 | } |
---|
57 | |
---|
58 | // Mapping table from Base64 characters to 6-bit nibbles. |
---|
59 | private static byte[] map2 = new byte[128]; |
---|
60 | static { |
---|
61 | for (int i = 0; i < map2.length; i++) |
---|
62 | map2[i] = -1; |
---|
63 | for (int i = 0; i < 64; i++) |
---|
64 | map2[map1[i]] = (byte) i; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Encodes a string into Base64 format. No blanks or line breaks are |
---|
69 | * inserted. |
---|
70 | * |
---|
71 | * @param s |
---|
72 | * a String to be encoded. |
---|
73 | * @return A String with the Base64 encoded data. |
---|
74 | */ |
---|
75 | public static String encodeString(String s) { |
---|
76 | return new String(encode(s.getBytes())); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Encodes a byte array into Base64 format. No blanks or line breaks are |
---|
81 | * inserted. |
---|
82 | * |
---|
83 | * @param in |
---|
84 | * an array containing the data bytes to be encoded. |
---|
85 | * @return A character array with the Base64 encoded data. |
---|
86 | */ |
---|
87 | public static char[] encode(byte[] in) { |
---|
88 | return encode(in, in.length); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Encodes a byte array into Base64 format. No blanks or line breaks are |
---|
93 | * inserted. |
---|
94 | * |
---|
95 | * @param in |
---|
96 | * an array containing the data bytes to be encoded. |
---|
97 | * @param iLen |
---|
98 | * number of bytes to process in <code>in</code>. |
---|
99 | * @return A character array with the Base64 encoded data. |
---|
100 | */ |
---|
101 | public static char[] encode(byte[] in, int iLen) { |
---|
102 | int oDataLen = (iLen * 4 + 2) / 3; // output length without padding |
---|
103 | int oLen = ((iLen + 2) / 3) * 4; // output length including padding |
---|
104 | char[] out = new char[oLen]; |
---|
105 | int ip = 0; |
---|
106 | int op = 0; |
---|
107 | while (ip < iLen) { |
---|
108 | int i0 = in[ip++] & 0xff; |
---|
109 | int i1 = ip < iLen ? in[ip++] & 0xff : 0; |
---|
110 | int i2 = ip < iLen ? in[ip++] & 0xff : 0; |
---|
111 | int o0 = i0 >>> 2; |
---|
112 | int o1 = ((i0 & 3) << 4) | (i1 >>> 4); |
---|
113 | int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6); |
---|
114 | int o3 = i2 & 0x3F; |
---|
115 | out[op++] = map1[o0]; |
---|
116 | out[op++] = map1[o1]; |
---|
117 | out[op] = op < oDataLen ? map1[o2] : '='; |
---|
118 | op++; |
---|
119 | out[op] = op < oDataLen ? map1[o3] : '='; |
---|
120 | op++; |
---|
121 | } |
---|
122 | return out; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Decodes a string from Base64 format. |
---|
127 | * |
---|
128 | * @param s |
---|
129 | * a Base64 String to be decoded. |
---|
130 | * @return A String containing the decoded data. |
---|
131 | * @throws IllegalArgumentException |
---|
132 | * if the input is not valid Base64 encoded data. |
---|
133 | */ |
---|
134 | public static String decodeString(String s) { |
---|
135 | return new String(decode(s)); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Decodes a byte array from Base64 format. |
---|
140 | * |
---|
141 | * @param s |
---|
142 | * a Base64 String to be decoded. |
---|
143 | * @return An array containing the decoded data bytes. |
---|
144 | * @throws IllegalArgumentException |
---|
145 | * if the input is not valid Base64 encoded data. |
---|
146 | */ |
---|
147 | public static byte[] decode(String s) { |
---|
148 | return decode(s.toCharArray()); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Decodes a byte array from Base64 format. No blanks or line breaks are |
---|
153 | * allowed within the Base64 encoded data. |
---|
154 | * |
---|
155 | * @param in |
---|
156 | * a character array containing the Base64 encoded data. |
---|
157 | * @return An array containing the decoded data bytes. |
---|
158 | * @throws IllegalArgumentException |
---|
159 | * if the input is not valid Base64 encoded data. |
---|
160 | */ |
---|
161 | public static byte[] decode(char[] in) { |
---|
162 | int iLen = in.length; |
---|
163 | if (iLen % 4 != 0) |
---|
164 | throw new IllegalArgumentException( |
---|
165 | "Length of Base64 encoded input string is not a multiple of 4."); |
---|
166 | while (iLen > 0 && in[iLen - 1] == '=') |
---|
167 | iLen--; |
---|
168 | int oLen = (iLen * 3) / 4; |
---|
169 | byte[] out = new byte[oLen]; |
---|
170 | int ip = 0; |
---|
171 | int op = 0; |
---|
172 | while (ip < iLen) { |
---|
173 | int i0 = in[ip++]; |
---|
174 | int i1 = in[ip++]; |
---|
175 | int i2 = ip < iLen ? in[ip++] : 'A'; |
---|
176 | int i3 = ip < iLen ? in[ip++] : 'A'; |
---|
177 | if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127) |
---|
178 | throw new IllegalArgumentException( |
---|
179 | "Illegal character in Base64 encoded data."); |
---|
180 | int b0 = map2[i0]; |
---|
181 | int b1 = map2[i1]; |
---|
182 | int b2 = map2[i2]; |
---|
183 | int b3 = map2[i3]; |
---|
184 | if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0) |
---|
185 | throw new IllegalArgumentException( |
---|
186 | "Illegal character in Base64 encoded data."); |
---|
187 | int o0 = (b0 << 2) | (b1 >>> 4); |
---|
188 | int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2); |
---|
189 | int o2 = ((b2 & 3) << 6) | b3; |
---|
190 | out[op++] = (byte) o0; |
---|
191 | if (op < oLen) |
---|
192 | out[op++] = (byte) o1; |
---|
193 | if (op < oLen) |
---|
194 | out[op++] = (byte) o2; |
---|
195 | } |
---|
196 | return out; |
---|
197 | } |
---|
198 | |
---|
199 | // Dummy constructor. |
---|
200 | private Base64Coder() { |
---|
201 | } |
---|
202 | |
---|
203 | } // end class Base64Coder |
---|