PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/base64coder-2010-12-19/src/main/java/biz/source_code/base64Coder/Base64Coder.java

#
Java | 226 lines | 99 code | 18 blank | 109 comment | 33 complexity | c070311ea6e99f139289e8147f10ab70 MD5 | raw file
  1. // Copyright 2003-2010 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
  2. // www.source-code.biz, www.inventec.ch/chdh
  3. //
  4. // This module is multi-licensed and may be used under the terms
  5. // of any of the following licenses:
  6. //
  7. // EPL, Eclipse Public License, V1.0 or later, http://www.eclipse.org/legal
  8. // LGPL, GNU Lesser General Public License, V2.1 or later, http://www.gnu.org/licenses/lgpl.html
  9. // GPL, GNU General Public License, V2 or later, http://www.gnu.org/licenses/gpl.html
  10. // AL, Apache License, V2.0 or later, http://www.apache.org/licenses
  11. // BSD, BSD License, http://www.opensource.org/licenses/bsd-license.php
  12. //
  13. // Please contact the author if you need another license.
  14. // This module is provided "as is", without warranties of any kind.
  15. package biz.source_code.base64Coder;
  16. /**
  17. * A Base64 encoder/decoder.
  18. *
  19. * <p>
  20. * This class is used to encode and decode data in Base64 format as described in RFC 1521.
  21. *
  22. * <p>
  23. * Project home page: <a href="http://www.source-code.biz/base64coder/java/">www.source-code.biz/base64coder/java</a><br>
  24. * Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland<br>
  25. * Multi-licensed: EPL / LGPL / GPL / AL / BSD.
  26. */
  27. public class Base64Coder {
  28. // The line separator string of the operating system.
  29. private static final String systemLineSeparator = System.getProperty("line.separator");
  30. // Mapping table from 6-bit nibbles to Base64 characters.
  31. private static final char[] map1 = new char[64];
  32. static {
  33. int i=0;
  34. for (char c='A'; c<='Z'; c++) map1[i++] = c;
  35. for (char c='a'; c<='z'; c++) map1[i++] = c;
  36. for (char c='0'; c<='9'; c++) map1[i++] = c;
  37. map1[i++] = '+'; map1[i++] = '/'; }
  38. // Mapping table from Base64 characters to 6-bit nibbles.
  39. private static final byte[] map2 = new byte[128];
  40. static {
  41. for (int i=0; i<map2.length; i++) map2[i] = -1;
  42. for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
  43. /**
  44. * Encodes a string into Base64 format.
  45. * No blanks or line breaks are inserted.
  46. * @param s A String to be encoded.
  47. * @return A String containing the Base64 encoded data.
  48. */
  49. public static String encodeString (String s) {
  50. return new String(encode(s.getBytes())); }
  51. /**
  52. * Encodes a byte array into Base 64 format and breaks the output into lines of 76 characters.
  53. * This method is compatible with <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.
  54. * @param in An array containing the data bytes to be encoded.
  55. * @return A String containing the Base64 encoded data, broken into lines.
  56. */
  57. public static String encodeLines (byte[] in) {
  58. return encodeLines(in, 0, in.length, 76, systemLineSeparator); }
  59. /**
  60. * Encodes a byte array into Base 64 format and breaks the output into lines.
  61. * @param in An array containing the data bytes to be encoded.
  62. * @param iOff Offset of the first byte in <code>in</code> to be processed.
  63. * @param iLen Number of bytes to be processed in <code>in</code>, starting at <code>iOff</code>.
  64. * @param lineLen Line length for the output data. Should be a multiple of 4.
  65. * @param lineSeparator The line separator to be used to separate the output lines.
  66. * @return A String containing the Base64 encoded data, broken into lines.
  67. */
  68. public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String lineSeparator) {
  69. int blockLen = (lineLen*3) / 4;
  70. if (blockLen <= 0) throw new IllegalArgumentException();
  71. int lines = (iLen+blockLen-1) / blockLen;
  72. int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();
  73. StringBuilder buf = new StringBuilder(bufLen);
  74. int ip = 0;
  75. while (ip < iLen) {
  76. int l = Math.min(iLen-ip, blockLen);
  77. buf.append (encode(in, iOff+ip, l));
  78. buf.append (lineSeparator);
  79. ip += l; }
  80. return buf.toString(); }
  81. /**
  82. * Encodes a byte array into Base64 format.
  83. * No blanks or line breaks are inserted in the output.
  84. * @param in An array containing the data bytes to be encoded.
  85. * @return A character array containing the Base64 encoded data.
  86. */
  87. public static char[] encode (byte[] in) {
  88. return encode(in, 0, in.length); }
  89. /**
  90. * Encodes a byte array into Base64 format.
  91. * No blanks or line breaks are inserted in the output.
  92. * @param in An array containing the data bytes to be encoded.
  93. * @param iLen Number of bytes to process in <code>in</code>.
  94. * @return A character array containing the Base64 encoded data.
  95. */
  96. public static char[] encode (byte[] in, int iLen) {
  97. return encode(in, 0, iLen); }
  98. /**
  99. * Encodes a byte array into Base64 format.
  100. * No blanks or line breaks are inserted in the output.
  101. * @param in An array containing the data bytes to be encoded.
  102. * @param iOff Offset of the first byte in <code>in</code> to be processed.
  103. * @param iLen Number of bytes to process in <code>in</code>, starting at <code>iOff</code>.
  104. * @return A character array containing the Base64 encoded data.
  105. */
  106. public static char[] encode (byte[] in, int iOff, int iLen) {
  107. int oDataLen = (iLen*4+2)/3; // output length without padding
  108. int oLen = ((iLen+2)/3)*4; // output length including padding
  109. char[] out = new char[oLen];
  110. int ip = iOff;
  111. int iEnd = iOff + iLen;
  112. int op = 0;
  113. while (ip < iEnd) {
  114. int i0 = in[ip++] & 0xff;
  115. int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
  116. int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
  117. int o0 = i0 >>> 2;
  118. int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
  119. int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
  120. int o3 = i2 & 0x3F;
  121. out[op++] = map1[o0];
  122. out[op++] = map1[o1];
  123. out[op] = op < oDataLen ? map1[o2] : '='; op++;
  124. out[op] = op < oDataLen ? map1[o3] : '='; op++; }
  125. return out; }
  126. /**
  127. * Decodes a string from Base64 format.
  128. * No blanks or line breaks are allowed within the Base64 encoded input data.
  129. * @param s A Base64 String to be decoded.
  130. * @return A String containing the decoded data.
  131. * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
  132. */
  133. public static String decodeString (String s) {
  134. return new String(decode(s)); }
  135. /**
  136. * Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.
  137. * CR, LF, Tab and Space characters are ignored in the input data.
  138. * This method is compatible with <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.
  139. * @param s A Base64 String to be decoded.
  140. * @return An array containing the decoded data bytes.
  141. * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
  142. */
  143. public static byte[] decodeLines (String s) {
  144. char[] buf = new char[s.length()];
  145. int p = 0;
  146. for (int ip = 0; ip < s.length(); ip++) {
  147. char c = s.charAt(ip);
  148. if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
  149. buf[p++] = c; }
  150. return decode(buf, 0, p); }
  151. /**
  152. * Decodes a byte array from Base64 format.
  153. * No blanks or line breaks are allowed within the Base64 encoded input data.
  154. * @param s A Base64 String to be decoded.
  155. * @return An array containing the decoded data bytes.
  156. * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
  157. */
  158. public static byte[] decode (String s) {
  159. return decode(s.toCharArray()); }
  160. /**
  161. * Decodes a byte array from Base64 format.
  162. * No blanks or line breaks are allowed within the Base64 encoded input data.
  163. * @param in A character array containing the Base64 encoded data.
  164. * @return An array containing the decoded data bytes.
  165. * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
  166. */
  167. public static byte[] decode (char[] in) {
  168. return decode(in, 0, in.length); }
  169. /**
  170. * Decodes a byte array from Base64 format.
  171. * No blanks or line breaks are allowed within the Base64 encoded input data.
  172. * @param in A character array containing the Base64 encoded data.
  173. * @param iOff Offset of the first character in <code>in</code> to be processed.
  174. * @param iLen Number of characters to process in <code>in</code>, starting at <code>iOff</code>.
  175. * @return An array containing the decoded data bytes.
  176. * @throws IllegalArgumentException If the input is not valid Base64 encoded data.
  177. */
  178. public static byte[] decode (char[] in, int iOff, int iLen) {
  179. if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
  180. while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;
  181. int oLen = (iLen*3) / 4;
  182. byte[] out = new byte[oLen];
  183. int ip = iOff;
  184. int iEnd = iOff + iLen;
  185. int op = 0;
  186. while (ip < iEnd) {
  187. int i0 = in[ip++];
  188. int i1 = in[ip++];
  189. int i2 = ip < iEnd ? in[ip++] : 'A';
  190. int i3 = ip < iEnd ? in[ip++] : 'A';
  191. if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
  192. throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
  193. int b0 = map2[i0];
  194. int b1 = map2[i1];
  195. int b2 = map2[i2];
  196. int b3 = map2[i3];
  197. if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
  198. throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
  199. int o0 = ( b0 <<2) | (b1>>>4);
  200. int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
  201. int o2 = ((b2 & 3)<<6) | b3;
  202. out[op++] = (byte)o0;
  203. if (op<oLen) out[op++] = (byte)o1;
  204. if (op<oLen) out[op++] = (byte)o2; }
  205. return out; }
  206. // Dummy constructor.
  207. private Base64Coder() {}
  208. } // end class Base64Coder