PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/client/android/src/org/xmlrpc/android/Base64.java

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