23 | * This class is used to encode and decode data in Base64 format as described in RFC 1521. 24 | * 25 | *
26 | * Project home page: www.source-code.biz/base64coder/java
27 | * Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
28 | * Multi-licensed: EPL / LGPL / GPL / AL / BSD / MIT.
29 | */
30 | public class Base64Coder {
31 |
32 | // The line separator string of the operating system.
33 | private static final String systemLineSeparator = System.getProperty("line.separator");
34 |
35 | // Mapping table from 6-bit nibbles to Base64 characters.
36 | private static final char[] map1 = new char[64];
37 | static {
38 | int i=0;
39 | for (char c='A'; c<='Z'; c++) map1[i++] = c;
40 | for (char c='a'; c<='z'; c++) map1[i++] = c;
41 | for (char c='0'; c<='9'; c++) map1[i++] = c;
42 | map1[i++] = '+'; map1[i++] = '/'; }
43 |
44 | // Mapping table from Base64 characters to 6-bit nibbles.
45 | private static final byte[] map2 = new byte[128];
46 | static {
47 | for (int i=0; iin
to be processed.
72 | * @param iLen Number of bytes to be processed in in
, starting at iOff
.
73 | * @param lineLen Line length for the output data. Should be a multiple of 4.
74 | * @param lineSeparator The line separator to be used to separate the output lines.
75 | * @return A String containing the Base64 encoded data, broken into lines.
76 | */
77 | public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String lineSeparator) {
78 | int blockLen = (lineLen*3) / 4;
79 | if (blockLen <= 0) throw new IllegalArgumentException();
80 | int lines = (iLen+blockLen-1) / blockLen;
81 | int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();
82 | StringBuilder buf = new StringBuilder(bufLen);
83 | int ip = 0;
84 | while (ip < iLen) {
85 | int l = Math.min(iLen-ip, blockLen);
86 | buf.append (encode(in, iOff+ip, l));
87 | buf.append (lineSeparator);
88 | ip += l; }
89 | return buf.toString(); }
90 |
91 | /**
92 | * Encodes a byte array into Base64 format.
93 | * No blanks or line breaks are inserted in the output.
94 | * @param in An array containing the data bytes to be encoded.
95 | * @return A character array containing the Base64 encoded data.
96 | */
97 | public static char[] encode (byte[] in) {
98 | return encode(in, 0, in.length); }
99 |
100 | /**
101 | * Encodes a byte array into Base64 format.
102 | * No blanks or line breaks are inserted in the output.
103 | * @param in An array containing the data bytes to be encoded.
104 | * @param iLen Number of bytes to process in in
.
105 | * @return A character array containing the Base64 encoded data.
106 | */
107 | public static char[] encode (byte[] in, int iLen) {
108 | return encode(in, 0, iLen); }
109 |
110 | /**
111 | * Encodes a byte array into Base64 format.
112 | * No blanks or line breaks are inserted in the output.
113 | * @param in An array containing the data bytes to be encoded.
114 | * @param iOff Offset of the first byte in in
to be processed.
115 | * @param iLen Number of bytes to process in in
, starting at iOff
.
116 | * @return A character array containing the Base64 encoded data.
117 | */
118 | public static char[] encode (byte[] in, int iOff, int iLen) {
119 | int oDataLen = (iLen*4+2)/3; // output length without padding
120 | int oLen = ((iLen+2)/3)*4; // output length including padding
121 | char[] out = new char[oLen];
122 | int ip = iOff;
123 | int iEnd = iOff + iLen;
124 | int op = 0;
125 | while (ip < iEnd) {
126 | int i0 = in[ip++] & 0xff;
127 | int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
128 | int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
129 | int o0 = i0 >>> 2;
130 | int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
131 | int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
132 | int o3 = i2 & 0x3F;
133 | out[op++] = map1[o0];
134 | out[op++] = map1[o1];
135 | out[op] = op < oDataLen ? map1[o2] : '='; op++;
136 | out[op] = op < oDataLen ? map1[o3] : '='; op++; }
137 | return out; }
138 |
139 | /**
140 | * Decodes a string from Base64 format.
141 | * No blanks or line breaks are allowed within the Base64 encoded input data.
142 | * @param s A Base64 String to be decoded.
143 | * @return A String containing the decoded data.
144 | * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
145 | */
146 | public static String decodeString (String s) {
147 | return new String(decode(s)); }
148 |
149 | /**
150 | * Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.
151 | * CR, LF, Tab and Space characters are ignored in the input data.
152 | * This method is compatible with sun.misc.BASE64Decoder.decodeBuffer(String)
.
153 | * @param s A Base64 String to be decoded.
154 | * @return An array containing the decoded data bytes.
155 | * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
156 | */
157 | public static byte[] decodeLines (String s) {
158 | char[] buf = new char[s.length()];
159 | int p = 0;
160 | for (int ip = 0; ip < s.length(); ip++) {
161 | char c = s.charAt(ip);
162 | if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
163 | buf[p++] = c; }
164 | return decode(buf, 0, p); }
165 |
166 | /**
167 | * Decodes a byte array from Base64 format.
168 | * No blanks or line breaks are allowed within the Base64 encoded input data.
169 | * @param s A Base64 String to be decoded.
170 | * @return An array containing the decoded data bytes.
171 | * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
172 | */
173 | public static byte[] decode (String s) {
174 | return decode(s.toCharArray()); }
175 |
176 | /**
177 | * Decodes a byte array from Base64 format.
178 | * No blanks or line breaks are allowed within the Base64 encoded input data.
179 | * @param in A character array containing the Base64 encoded data.
180 | * @return An array containing the decoded data bytes.
181 | * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
182 | */
183 | public static byte[] decode (char[] in) {
184 | return decode(in, 0, in.length); }
185 |
186 | /**
187 | * Decodes a byte array from Base64 format.
188 | * No blanks or line breaks are allowed within the Base64 encoded input data.
189 | * @param in A character array containing the Base64 encoded data.
190 | * @param iOff Offset of the first character in in
to be processed.
191 | * @param iLen Number of characters to process in in
, starting at iOff
.
192 | * @return An array containing the decoded data bytes.
193 | * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
194 | */
195 | public static byte[] decode (char[] in, int iOff, int iLen) {
196 | if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
197 | while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;
198 | int oLen = (iLen*3) / 4;
199 | byte[] out = new byte[oLen];
200 | int ip = iOff;
201 | int iEnd = iOff + iLen;
202 | int op = 0;
203 | while (ip < iEnd) {
204 | int i0 = in[ip++];
205 | int i1 = in[ip++];
206 | int i2 = ip < iEnd ? in[ip++] : 'A';
207 | int i3 = ip < iEnd ? in[ip++] : 'A';
208 | if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
209 | throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
210 | int b0 = map2[i0];
211 | int b1 = map2[i1];
212 | int b2 = map2[i2];
213 | int b3 = map2[i3];
214 | if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
215 | throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
216 | int o0 = ( b0 <<2) | (b1>>>4);
217 | int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
218 | int o2 = ((b2 & 3)<<6) | b3;
219 | out[op++] = (byte)o0;
220 | if (op