24
24
* Project home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
25
25
* Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland<br>
26
26
* Multi-licensed: EPL/LGPL/AL/BSD.
27
- *
28
27
*/
29
-
30
28
public class Base64Coder {
31
29
30
+ // The line separator string of the operating system.
31
+ private static final String systemLineSeparator = System .getProperty ("line.separator" );
32
+
32
33
// Mapping table from 6-bit nibbles to Base64 characters.
33
34
private static char [] map1 = new char [64 ];
34
35
static {
@@ -48,37 +49,81 @@ public class Base64Coder {
48
49
* Encodes a string into Base64 format.
49
50
* No blanks or line breaks are inserted.
50
51
* @param s a String to be encoded.
51
- * @return A String with the Base64 encoded data.
52
+ * @return a String with the Base64 encoded data.
52
53
*/
53
54
public static String encodeString (String s ) {
54
55
return new String (encode (s .getBytes ())); }
55
56
57
+ /**
58
+ * Encodes a byte array into Base 64 format and splits the output into lines of 76 characters.
59
+ * This method is compatible with <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.
60
+ * @param in an array containing the data bytes to be encoded.
61
+ * @return a String with the Base64 encoded data, split into lines.
62
+ */
63
+ public static String encodeLines (byte [] in ) {
64
+ return encodeLines (in , 0 , in .length , 76 , systemLineSeparator ); }
65
+
66
+ /**
67
+ * Encodes a byte array into Base 64 format and splits the output into lines.
68
+ * @param in an array containing the data bytes to be encoded.
69
+ * #param iOff offset of the first byte in <code>in</code> to be processed.
70
+ * #param iLen number of bytes to be processed in <code>in</code> starting at <code>iOff</code>.
71
+ * @param lineLen line length for the output data. Should be a multiple of 4.
72
+ * @param lineSeparator the line separator to be used to split the output lines.
73
+ * @return a String with the Base64 encoded data, split into lines.
74
+ */
75
+ public static String encodeLines (byte [] in , int iOff , int iLen , int lineLen , String lineSeparator ) {
76
+ int blockLen = (lineLen *3 ) / 4 ;
77
+ if (blockLen <= 0 ) throw new IllegalArgumentException ();
78
+ int lines = (iLen +blockLen -1 ) / blockLen ;
79
+ int bufLen = ((iLen +2 )/3 )*4 + lines *lineSeparator .length ();
80
+ StringBuilder buf = new StringBuilder (bufLen );
81
+ int ip = 0 ;
82
+ while (ip < iLen ) {
83
+ int l = Math .min (iLen -ip , blockLen );
84
+ buf .append (encode (in , iOff +ip , l ));
85
+ buf .append (lineSeparator );
86
+ ip += l ; }
87
+ return buf .toString (); }
88
+
56
89
/**
57
90
* Encodes a byte array into Base64 format.
58
91
* No blanks or line breaks are inserted.
59
92
* @param in an array containing the data bytes to be encoded.
60
- * @return A character array with the Base64 encoded data.
93
+ * @return a character array with the Base64 encoded data.
61
94
*/
62
95
public static char [] encode (byte [] in ) {
63
- return encode (in , in .length ); }
96
+ return encode (in , 0 , in .length ); }
64
97
65
98
/**
66
99
* Encodes a byte array into Base64 format.
67
100
* No blanks or line breaks are inserted.
68
- * @param in an array containing the data bytes to be encoded.
69
- * @param iLen number of bytes to process in <code>in</code>.
70
- * @return A character array with the Base64 encoded data.
101
+ * @param in an array containing the data bytes to be encoded.
102
+ * @param iLen number of bytes to process in <code>in</code>.
103
+ * @return a character array with the Base64 encoded data.
71
104
*/
72
105
public static char [] encode (byte [] in , int iLen ) {
106
+ return encode (in , 0 , iLen ); }
107
+
108
+ /**
109
+ * Encodes a byte array into Base64 format.
110
+ * No blanks or line breaks are inserted.
111
+ * @param in an array containing the data bytes to be encoded.
112
+ * @param iOff offset of the first byte in <code>in</code> to be processed.
113
+ * @param iLen number of bytes to process in <code>in</code> starting at <code>iOff</code>.
114
+ * @return a character array with the Base64 encoded data.
115
+ */
116
+ public static char [] encode (byte [] in , int iOff , int iLen ) {
73
117
int oDataLen = (iLen *4 +2 )/3 ; // output length without padding
74
118
int oLen = ((iLen +2 )/3 )*4 ; // output length including padding
75
119
char [] out = new char [oLen ];
76
- int ip = 0 ;
120
+ int ip = iOff ;
121
+ int iEnd = iOff + iLen ;
77
122
int op = 0 ;
78
- while (ip < iLen ) {
123
+ while (ip < iEnd ) {
79
124
int i0 = in [ip ++] & 0xff ;
80
- int i1 = ip < iLen ? in [ip ++] & 0xff : 0 ;
81
- int i2 = ip < iLen ? in [ip ++] & 0xff : 0 ;
125
+ int i1 = ip < iEnd ? in [ip ++] & 0xff : 0 ;
126
+ int i2 = ip < iEnd ? in [ip ++] & 0xff : 0 ;
82
127
int o0 = i0 >>> 2 ;
83
128
int o1 = ((i0 & 3 ) << 4 ) | (i1 >>> 4 );
84
129
int o2 = ((i1 & 0xf ) << 2 ) | (i2 >>> 6 );
@@ -91,17 +136,36 @@ public static char[] encode (byte[] in, int iLen) {
91
136
92
137
/**
93
138
* Decodes a string from Base64 format.
139
+ * No blanks or line breaks are allowed within the Base64 encoded data.
94
140
* @param s a Base64 String to be decoded.
95
- * @return A String containing the decoded data.
141
+ * @return a String containing the decoded data.
96
142
* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
97
143
*/
98
144
public static String decodeString (String s ) {
99
145
return new String (decode (s )); }
100
146
147
+ /**
148
+ * Decodes a byte array from Base64 format and ignores line separators and blanks.
149
+ * CR, LF, Tab and Space characters are ignored in the input data.
150
+ * This method is compatible with <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.
151
+ * @param s a Base64 String to be decoded.
152
+ * @return an array containing the decoded data bytes.
153
+ * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
154
+ */
155
+ public static byte [] decodeLines (String s ) {
156
+ char [] buf = new char [s .length ()];
157
+ int p = 0 ;
158
+ for (int ip = 0 ; ip < s .length (); ip ++) {
159
+ char c = s .charAt (ip );
160
+ if (c != ' ' && c != '\r' && c != '\n' && c != '\t' )
161
+ buf [p ++] = c ; }
162
+ return decode (buf , 0 , p ); }
163
+
101
164
/**
102
165
* Decodes a byte array from Base64 format.
166
+ * No blanks or line breaks are allowed within the Base64 encoded data.
103
167
* @param s a Base64 String to be decoded.
104
- * @return An array containing the decoded data bytes.
168
+ * @return an array containing the decoded data bytes.
105
169
* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
106
170
*/
107
171
public static byte [] decode (String s ) {
@@ -111,22 +175,34 @@ public static byte[] decode (String s) {
111
175
* Decodes a byte array from Base64 format.
112
176
* No blanks or line breaks are allowed within the Base64 encoded data.
113
177
* @param in a character array containing the Base64 encoded data.
114
- * @return An array containing the decoded data bytes.
178
+ * @return an array containing the decoded data bytes.
115
179
* @throws IllegalArgumentException if the input is not valid Base64 encoded data.
116
180
*/
117
181
public static byte [] decode (char [] in ) {
118
- int iLen = in .length ;
182
+ return decode (in , 0 , in .length ); }
183
+
184
+ /**
185
+ * Decodes a byte array from Base64 format.
186
+ * No blanks or line breaks are allowed within the Base64 encoded data.
187
+ * @param in a character array containing the Base64 encoded data.
188
+ * @param iOff offset of the first character in <code>in</code> to be processed.
189
+ * @param iLen number of characters to process in <code>in</code> starting at <code>iOff</code>.
190
+ * @return an array containing the decoded data bytes.
191
+ * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
192
+ */
193
+ public static byte [] decode (char [] in , int iOff , int iLen ) {
119
194
if (iLen %4 != 0 ) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4." );
120
- while (iLen > 0 && in [iLen -1 ] == '=' ) iLen --;
195
+ while (iLen > 0 && in [iOff + iLen -1 ] == '=' ) iLen --;
121
196
int oLen = (iLen *3 ) / 4 ;
122
197
byte [] out = new byte [oLen ];
123
- int ip = 0 ;
198
+ int ip = iOff ;
199
+ int iEnd = iOff + iLen ;
124
200
int op = 0 ;
125
- while (ip < iLen ) {
201
+ while (ip < iEnd ) {
126
202
int i0 = in [ip ++];
127
203
int i1 = in [ip ++];
128
- int i2 = ip < iLen ? in [ip ++] : 'A' ;
129
- int i3 = ip < iLen ? in [ip ++] : 'A' ;
204
+ int i2 = ip < iEnd ? in [ip ++] : 'A' ;
205
+ int i3 = ip < iEnd ? in [ip ++] : 'A' ;
130
206
if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127 )
131
207
throw new IllegalArgumentException ("Illegal character in Base64 encoded data." );
132
208
int b0 = map2 [i0 ];
0 commit comments