001: package clime.messadmin.utils;
002:
003: import java.util.Arrays;
004:
005: /** A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance
006: * with RFC 2045.<br><br>
007: * On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster
008: * on small arrays (10 - 1000 bytes) and 2-3 times as fast on larger arrays (10000 - 1000000 bytes)
009: * compared to <code>sun.misc.Encoder()/Decoder()</code>.<br><br>
010: *
011: * On byte arrays the encoder is about 20% faster than Jakarta Commons Base64 Codec for encode and
012: * about 50% faster for decoding large arrays. This implementation is about twice as fast on very small
013: * arrays (< 30 bytes). If source/destination is a <code>String</code> this
014: * version is about three times as fast due to the fact that the Commons Codec result has to be recoded
015: * to a <code>String</code> from <code>byte[]</code>, which is very expensive.<br><br>
016: *
017: * This encode/decode algorithm doesn't create any temporary arrays as many other codecs do, it only
018: * allocates the resulting array. This produces less garbage and it is possible to handle arrays twice
019: * as large as algorithms that create a temporary array. (E.g. Jakarta Commons Codec). It is unknown
020: * whether Sun's <code>sun.misc.Encoder()/Decoder()</code> produce temporary arrays but since performance
021: * is quite low it probably does.<br><br>
022: *
023: * The encoder produces the same output as the Sun one except that the Sun's encoder appends
024: * a trailing line separator if the last character isn't a pad. Unclear why but it only adds to the
025: * length and is probably a side effect. Both are in conformance with RFC 2045 though.<br>
026: * Commons codec seem to always att a trailing line separator.<br><br>
027: *
028: * <b>Note!</b>
029: * The encode/decode method pairs (types) come in three versions with the <b>exact</b> same algorithm and
030: * thus a lot of code redundancy. This is to not create any temporary arrays for transcoding to/from different
031: * format types. The methods not used can simply be commented out.<br><br>
032: *
033: * There is also a "fast" version of all decode methods that works the same way as the normal ones, but
034: * har a few demands on the decoded input. Normally though, these fast verions should be used if the source if
035: * the input is known and it hasn't bee tampered with.<br><br>
036: *
037: * If you find the code useful or you find a bug, please send me a note at base64 @ miginfocom . com.
038: *
039: * Licence (BSD):
040: * ==============
041: *
042: * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (base64 @ miginfocom . com)
043: * All rights reserved.
044: *
045: * Redistribution and use in source and binary forms, with or without modification,
046: * are permitted provided that the following conditions are met:
047: * Redistributions of source code must retain the above copyright notice, this list
048: * of conditions and the following disclaimer.
049: * Redistributions in binary form must reproduce the above copyright notice, this
050: * list of conditions and the following disclaimer in the documentation and/or other
051: * materials provided with the distribution.
052: * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
053: * used to endorse or promote products derived from this software without specific
054: * prior written permission.
055: *
056: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
057: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
058: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
059: * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
060: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
061: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
062: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
063: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
064: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
065: * OF SUCH DAMAGE.
066: *
067: * @version 2.2
068: * @author Mikael Grev
069: * Date: 2004-aug-02
070: * Time: 11:31:11
071: */
072:
073: public class Base64 {
074: private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
075: .toCharArray();
076: private static final int[] IA = new int[256];
077: static {
078: Arrays.fill(IA, -1);
079: for (int i = 0, iS = CA.length; i < iS; ++i) {
080: IA[CA[i]] = i;
081: }
082: IA['='] = 0;
083: }
084:
085: // ****************************************************************************************
086: // * char[] version
087: // ****************************************************************************************
088:
089: /** Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045.
090: * @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
091: * @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
092: * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
093: * little faster.
094: * @return A BASE64 encoded array. Never <code>null</code>.
095: */
096: public final static char[] encodeToChar(byte[] sArr, boolean lineSep) {
097: // Check special case
098: int sLen = sArr != null ? sArr.length : 0;
099: if (sLen == 0) {
100: return new char[0];
101: }
102: //assert sArr != null;
103:
104: int eLen = (sLen / 3) * 3; // Length of even 24-bits.
105: int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
106: int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
107: char[] dArr = new char[dLen];
108:
109: // Encode even 24-bits
110: for (int s = 0, d = 0, cc = 0; s < eLen;) {
111: // Copy next three bytes into lower 24 bits of int, paying attension to sign.
112: int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8
113: | (sArr[s++] & 0xff);
114:
115: // Encode the int into four chars
116: dArr[d++] = CA[(i >>> 18) & 0x3f];
117: dArr[d++] = CA[(i >>> 12) & 0x3f];
118: dArr[d++] = CA[(i >>> 6) & 0x3f];
119: dArr[d++] = CA[i & 0x3f];
120:
121: // Add optional line separator
122: if (lineSep && ++cc == 19 && d < dLen - 2) {
123: dArr[d++] = '\r';
124: dArr[d++] = '\n';
125: cc = 0;
126: }
127: }
128:
129: // Pad and encode last bits if source isn't even 24 bits.
130: int left = sLen - eLen; // 0 - 2.
131: if (left > 0) {
132: // Prepare the int
133: int i = ((sArr[eLen] & 0xff) << 10)
134: | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
135:
136: // Set last four chars
137: dArr[dLen - 4] = CA[i >> 12];
138: dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
139: dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
140: dArr[dLen - 1] = '=';
141: }
142: return dArr;
143: }
144:
145: /** Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with
146: * and without line separators.
147: * @param sArr The source array. <code>null</code> or length 0 will return an empty array.
148: * @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
149: * (including '=') isn't divideable by 4. (I.e. definitely corrupted).
150: */
151: public final static byte[] decode(char[] sArr) {
152: // Check special case
153: int sLen = sArr != null ? sArr.length : 0;
154: if (sLen == 0) {
155: return new byte[0];
156: }
157: //assert sArr != null;
158:
159: // Count illegal characters (including '\r', '\n') to know what size the returned array will be,
160: // so we don't have to reallocate & copy it later.
161: int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
162: for (int i = 0; i < sLen; ++i) {
163: if (IA[sArr[i]] < 0) {
164: ++sepCnt;
165: }
166: }
167:
168: // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
169: if ((sLen - sepCnt) % 4 != 0) {
170: return null;
171: }
172:
173: int pad = 0;
174: for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0;) {
175: if (sArr[i] == '=') {
176: ++pad;
177: }
178: }
179:
180: int len = ((sLen - sepCnt) * 6 >> 3) - pad;
181:
182: byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
183:
184: for (int s = 0, d = 0; d < len;) {
185: // Assemble three bytes into an int from four "valid" characters.
186: int i = 0;
187: for (int j = 0; j < 4; ++j) { // j only increased if a valid char was found.
188: int c = IA[sArr[s++]];
189: if (c >= 0) {
190: i |= c << (18 - j * 6);
191: } else {
192: --j;
193: }
194: }
195: // Add the bytes
196: dArr[d++] = (byte) (i >> 16);
197: if (d < len) {
198: dArr[d++] = (byte) (i >> 8);
199: if (d < len) {
200: dArr[d++] = (byte) i;
201: }
202: }
203: }
204: return dArr;
205: }
206:
207: /* * Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as
208: * fast as {@link #decode(char[])}. The preconditions are:<br>
209: * + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
210: * + Line separator must be "\r\n", as specified in RFC 2045
211: * + The array must not contain illegal characters within the encoded string<br>
212: * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
213: * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
214: * @return The decoded array of bytes. May be of length 0.
215: * /
216: public final static byte[] decodeFast(char[] sArr)
217: {
218: // Check special case
219: int sLen = sArr.length;
220: if (sLen == 0) {
221: return new byte[0];
222: }
223:
224: int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
225:
226: // Trim illegal chars from start
227: while (sIx < eIx && IA[sArr[sIx]] < 0) {
228: ++sIx;
229: }
230:
231: // Trim illegal chars from end
232: while (eIx > 0 && IA[sArr[eIx]] < 0) {
233: --eIx;
234: }
235:
236: // get the padding count (=) (0, 1 or 2)
237: int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.
238: int cCnt = eIx - sIx + 1; // Content count including possible separators
239: int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
240:
241: int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
242: byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
243:
244: // Decode all but the last 0 - 2 bytes.
245: int d = 0;
246: for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
247: // Assemble three bytes into an int from four "valid" characters.
248: int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
249:
250: // Add the bytes
251: dArr[d++] = (byte) (i >> 16);
252: dArr[d++] = (byte) (i >> 8);
253: dArr[d++] = (byte) i;
254:
255: // If line separator, jump over it.
256: if (sepCnt > 0 && ++cc == 19) {
257: sIx += 2;
258: cc = 0;
259: }
260: }
261:
262: if (d < len) {
263: // Decode last 1-3 bytes (incl '=') into 1-3 bytes
264: int i = 0;
265: for (int j = 0; sIx <= eIx - pad; ++j) {
266: i |= IA[sArr[sIx++]] << (18 - j * 6);
267: }
268:
269: for (int r = 16; d < len; r -= 8) {
270: dArr[d++] = (byte) (i >> r);
271: }
272: }
273:
274: return dArr;
275: }*/
276:
277: // ****************************************************************************************
278: // * byte[] version
279: // ****************************************************************************************
280: /** Encodes a raw byte array into a BASE64 <code>byte[]</code> representation i accordance with RFC 2045.
281: * @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
282: * @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
283: * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
284: * little faster.
285: * @return A BASE64 encoded array. Never <code>null</code>.
286: */
287: public final static byte[] encodeToByte(byte[] sArr, boolean lineSep) {
288: // Check special case
289: int sLen = sArr != null ? sArr.length : 0;
290: if (sLen == 0) {
291: return new byte[0];
292: }
293: //assert sArr != null;
294:
295: int eLen = (sLen / 3) * 3; // Length of even 24-bits.
296: int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
297: int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
298: byte[] dArr = new byte[dLen];
299:
300: // Encode even 24-bits
301: for (int s = 0, d = 0, cc = 0; s < eLen;) {
302: // Copy next three bytes into lower 24 bits of int, paying attension to sign.
303: int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8
304: | (sArr[s++] & 0xff);
305:
306: // Encode the int into four chars
307: dArr[d++] = (byte) CA[(i >>> 18) & 0x3f];
308: dArr[d++] = (byte) CA[(i >>> 12) & 0x3f];
309: dArr[d++] = (byte) CA[(i >>> 6) & 0x3f];
310: dArr[d++] = (byte) CA[i & 0x3f];
311:
312: // Add optional line separator
313: if (lineSep && ++cc == 19 && d < dLen - 2) {
314: dArr[d++] = '\r';
315: dArr[d++] = '\n';
316: cc = 0;
317: }
318: }
319:
320: // Pad and encode last bits if source isn't an even 24 bits.
321: int left = sLen - eLen; // 0 - 2.
322: if (left > 0) {
323: // Prepare the int
324: int i = ((sArr[eLen] & 0xff) << 10)
325: | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
326:
327: // Set last four chars
328: dArr[dLen - 4] = (byte) CA[i >> 12];
329: dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f];
330: dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f]
331: : (byte) '=';
332: dArr[dLen - 1] = '=';
333: }
334: return dArr;
335: }
336:
337: /** Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with
338: * and without line separators.
339: * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
340: * @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
341: * (including '=') isn't divideable by 4. (I.e. definitely corrupted).
342: */
343: public final static byte[] decode(byte[] sArr) {
344: // Check special case
345: int sLen = sArr.length;
346:
347: // Count illegal characters (including '\r', '\n') to know what size the returned array will be,
348: // so we don't have to reallocate & copy it later.
349: int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
350: for (int i = 0; i < sLen; ++i) {
351: if (IA[sArr[i] & 0xff] < 0) {
352: ++sepCnt;
353: }
354: }
355:
356: // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
357: if ((sLen - sepCnt) % 4 != 0) {
358: return null;
359: }
360:
361: int pad = 0;
362: for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0;) {
363: if (sArr[i] == '=') {
364: ++pad;
365: }
366: }
367:
368: int len = ((sLen - sepCnt) * 6 >> 3) - pad;
369:
370: byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
371:
372: for (int s = 0, d = 0; d < len;) {
373: // Assemble three bytes into an int from four "valid" characters.
374: int i = 0;
375: for (int j = 0; j < 4; ++j) { // j only increased if a valid char was found.
376: int c = IA[sArr[s++] & 0xff];
377: if (c >= 0) {
378: i |= c << (18 - j * 6);
379: } else {
380: --j;
381: }
382: }
383:
384: // Add the bytes
385: dArr[d++] = (byte) (i >> 16);
386: if (d < len) {
387: dArr[d++] = (byte) (i >> 8);
388: if (d < len) {
389: dArr[d++] = (byte) i;
390: }
391: }
392: }
393:
394: return dArr;
395: }
396:
397: /* * Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as
398: * fast as {@link #decode(byte[])}. The preconditions are:<br>
399: * + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
400: * + Line separator must be "\r\n", as specified in RFC 2045
401: * + The array must not contain illegal characters within the encoded string<br>
402: * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
403: * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.
404: * @return The decoded array of bytes. May be of length 0.
405: * /
406: public final static byte[] decodeFast(byte[] sArr)
407: {
408: // Check special case
409: int sLen = sArr.length;
410: if (sLen == 0) {
411: return new byte[0];
412: }
413:
414: int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
415:
416: // Trim illegal chars from start
417: while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0) {
418: ++sIx;
419: }
420:
421: // Trim illegal chars from end
422: while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0) {
423: --eIx;
424: }
425:
426: // get the padding count (=) (0, 1 or 2)
427: int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.
428: int cCnt = eIx - sIx + 1; // Content count including possible separators
429: int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;
430:
431: int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
432: byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
433:
434: // Decode all but the last 0 - 2 bytes.
435: int d = 0;
436: for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
437: // Assemble three bytes into an int from four "valid" characters.
438: int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]];
439:
440: // Add the bytes
441: dArr[d++] = (byte) (i >> 16);
442: dArr[d++] = (byte) (i >> 8);
443: dArr[d++] = (byte) i;
444:
445: // If line separator, jump over it.
446: if (sepCnt > 0 && ++cc == 19) {
447: sIx += 2;
448: cc = 0;
449: }
450: }
451:
452: if (d < len) {
453: // Decode last 1-3 bytes (incl '=') into 1-3 bytes
454: int i = 0;
455: for (int j = 0; sIx <= eIx - pad; ++j) {
456: i |= IA[sArr[sIx++]] << (18 - j * 6);
457: }
458:
459: for (int r = 16; d < len; r -= 8) {
460: dArr[d++] = (byte) (i >> r);
461: }
462: }
463:
464: return dArr;
465: }*/
466:
467: // ****************************************************************************************
468: // * String version
469: // ****************************************************************************************
470: /** Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045.
471: * @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
472: * @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
473: * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
474: * little faster.
475: * @return A BASE64 encoded array. Never <code>null</code>.
476: */
477: public final static String encodeToString(byte[] sArr,
478: boolean lineSep) {
479: // Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.
480: return new String(encodeToChar(sArr, lineSep));
481: }
482:
483: /** Decodes a BASE64 encoded <code>String</code>. All illegal characters will be ignored and can handle both strings with
484: * and without line separators.<br>
485: * <b>Note!</b> It can be up to about 2x the speed to call <code>decode(str.toCharArray())</code> instead. That
486: * will create a temporary array though. This version will use <code>str.charAt(i)</code> to iterate the string.
487: * @param str The source string. <code>null</code> or length 0 will return an empty array.
488: * @return The decoded array of bytes. May be of length 0. Will be <code>null</code> if the legal characters
489: * (including '=') isn't divideable by 4. (I.e. definitely corrupted).
490: */
491: public final static byte[] decode(String str) {
492: // Check special case
493: int sLen = str != null ? str.length() : 0;
494: if (sLen == 0) {
495: return new byte[0];
496: }
497: //assert str != null;
498:
499: // Count illegal characters (including '\r', '\n') to know what size the returned array will be,
500: // so we don't have to reallocate & copy it later.
501: int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...)
502: for (int i = 0; i < sLen; ++i) {
503: if (IA[str.charAt(i)] < 0) {
504: ++sepCnt;
505: }
506: }
507:
508: // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045.
509: if ((sLen - sepCnt) % 4 != 0) {
510: return null;
511: }
512:
513: // Count '=' at end
514: int pad = 0;
515: for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0;) {
516: if (str.charAt(i) == '=') {
517: ++pad;
518: }
519: }
520:
521: int len = ((sLen - sepCnt) * 6 >> 3) - pad;
522:
523: byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
524:
525: for (int s = 0, d = 0; d < len;) {
526: // Assemble three bytes into an int from four "valid" characters.
527: int i = 0;
528: for (int j = 0; j < 4; ++j) { // j only increased if a valid char was found.
529: int c = IA[str.charAt(s++)];
530: if (c >= 0) {
531: i |= c << (18 - j * 6);
532: } else {
533: --j;
534: }
535: }
536: // Add the bytes
537: dArr[d++] = (byte) (i >> 16);
538: if (d < len) {
539: dArr[d++] = (byte) (i >> 8);
540: if (d < len) {
541: dArr[d++] = (byte) i;
542: }
543: }
544: }
545: return dArr;
546: }
547:
548: /* * Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as
549: * fast as {@link #decode(String)}. The preconditions are:<br>
550: * + The array must have a line length of 76 chars OR no line separators at all (one line).<br>
551: * + Line separator must be "\r\n", as specified in RFC 2045
552: * + The array must not contain illegal characters within the encoded string<br>
553: * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>
554: * @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.
555: * @return The decoded array of bytes. May be of length 0.
556: * /
557: public final static byte[] decodeFast(String s)
558: {
559: // Check special case
560: int sLen = s.length();
561: if (sLen == 0) {
562: return new byte[0];
563: }
564:
565: int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.
566:
567: // Trim illegal chars from start
568: while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0) {
569: ++sIx;
570: }
571:
572: // Trim illegal chars from end
573: while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0) {
574: --eIx;
575: }
576:
577: // get the padding count (=) (0, 1 or 2)
578: int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.
579: int cCnt = eIx - sIx + 1; // Content count including possible separators
580: int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;
581:
582: int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes
583: byte[] dArr = new byte[len]; // Preallocate byte[] of exact length
584:
585: // Decode all but the last 0 - 2 bytes.
586: int d = 0;
587: for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {
588: // Assemble three bytes into an int from four "valid" characters.
589: int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];
590:
591: // Add the bytes
592: dArr[d++] = (byte) (i >> 16);
593: dArr[d++] = (byte) (i >> 8);
594: dArr[d++] = (byte) i;
595:
596: // If line separator, jump over it.
597: if (sepCnt > 0 && ++cc == 19) {
598: sIx += 2;
599: cc = 0;
600: }
601: }
602:
603: if (d < len) {
604: // Decode last 1-3 bytes (incl '=') into 1-3 bytes
605: int i = 0;
606: for (int j = 0; sIx <= eIx - pad; ++j) {
607: i |= IA[s.charAt(sIx++)] << (18 - j * 6);
608: }
609:
610: for (int r = 16; d < len; r -= 8) {
611: dArr[d++] = (byte) (i >> r);
612: }
613: }
614:
615: return dArr;
616: }*/
617: }
|