Imported from dev1.link2tek.net CommEngine.git
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

146 行
5.1 KiB

  1. package altk.common.engine.util;
  2. /**
  3. * A Base64 Encoder/Decoder.
  4. *
  5. * <p>
  6. * This class is used to encode and decode data in Base64 format as described in RFC 1521.
  7. *
  8. * <p>
  9. * This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br>
  10. * It is provided "as is" without warranty of any kind.<br>
  11. * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br>
  12. * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
  13. *
  14. * <p>
  15. * Version history:<br>
  16. * 2003-07-22 Christian d'Heureuse (chdh): Module created.<br>
  17. * 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br>
  18. * 2006-11-21 chdh:<br>
  19. * &nbsp; Method encode(String) renamed to encodeString(String).<br>
  20. * &nbsp; Method decode(String) renamed to decodeString(String).<br>
  21. * &nbsp; New method encode(byte[],int) added.<br>
  22. * &nbsp; New method decode(String) added.<br>
  23. */
  24. public class Base64Coder {
  25. // Mapping table from 6-bit nibbles to Base64 characters.
  26. private static char[] map1 = new char[64];
  27. static {
  28. int i=0;
  29. for (char c='A'; c<='Z'; c++) map1[i++] = c;
  30. for (char c='a'; c<='z'; c++) map1[i++] = c;
  31. for (char c='0'; c<='9'; c++) map1[i++] = c;
  32. map1[i++] = '+'; map1[i++] = '/'; }
  33. // Mapping table from Base64 characters to 6-bit nibbles.
  34. private static byte[] map2 = new byte[128];
  35. static {
  36. for (int i=0; i<map2.length; i++) map2[i] = -1;
  37. for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
  38. /**
  39. * Encodes a string into Base64 format.
  40. * No blanks or line breaks are inserted.
  41. * @param s a String to be encoded.
  42. * @return A String with the Base64 encoded data.
  43. */
  44. public static String encodeString (String s) {
  45. return new String(encode(s.getBytes())); }
  46. /**
  47. * Encodes a byte array into Base64 format.
  48. * No blanks or line breaks are inserted.
  49. * @param in an array containing the data bytes to be encoded.
  50. * @return A character array with the Base64 encoded data.
  51. */
  52. public static char[] encode (byte[] in) {
  53. return encode(in,in.length); }
  54. /**
  55. * Encodes a byte array into Base64 format.
  56. * No blanks or line breaks are inserted.
  57. * @param in an array containing the data bytes to be encoded.
  58. * @param iLen number of bytes to process in <code>in</code>.
  59. * @return A character array with the Base64 encoded data.
  60. */
  61. public static char[] encode (byte[] in, int iLen) {
  62. int oDataLen = (iLen*4+2)/3; // output length without padding
  63. int oLen = ((iLen+2)/3)*4; // output length including padding
  64. char[] out = new char[oLen];
  65. int ip = 0;
  66. int op = 0;
  67. while (ip < iLen) {
  68. int i0 = in[ip++] & 0xff;
  69. int i1 = ip < iLen ? in[ip++] & 0xff : 0;
  70. int i2 = ip < iLen ? in[ip++] & 0xff : 0;
  71. int o0 = i0 >>> 2;
  72. int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
  73. int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
  74. int o3 = i2 & 0x3F;
  75. out[op++] = map1[o0];
  76. out[op++] = map1[o1];
  77. out[op] = op < oDataLen ? map1[o2] : '='; op++;
  78. out[op] = op < oDataLen ? map1[o3] : '='; op++; }
  79. return out; }
  80. /**
  81. * Decodes a string from Base64 format.
  82. * @param s a Base64 String to be decoded.
  83. * @return A String containing the decoded data.
  84. * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
  85. */
  86. public static String decodeString (String s) {
  87. return new String(decode(s)); }
  88. /**
  89. * Decodes a byte array from Base64 format.
  90. * @param s a Base64 String to be decoded.
  91. * @return An array containing the decoded data bytes.
  92. * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
  93. */
  94. public static byte[] decode (String s) {
  95. return decode(s.toCharArray()); }
  96. /**
  97. * Decodes a byte array from Base64 format.
  98. * No blanks or line breaks are allowed within the Base64 encoded data.
  99. * @param in a character array containing the Base64 encoded data.
  100. * @return An array containing the decoded data bytes.
  101. * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
  102. */
  103. public static byte[] decode (char[] in) {
  104. int iLen = in.length;
  105. if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
  106. while (iLen > 0 && in[iLen-1] == '=') iLen--;
  107. int oLen = (iLen*3) / 4;
  108. byte[] out = new byte[oLen];
  109. int ip = 0;
  110. int op = 0;
  111. while (ip < iLen) {
  112. int i0 = in[ip++];
  113. int i1 = in[ip++];
  114. int i2 = ip < iLen ? in[ip++] : 'A';
  115. int i3 = ip < iLen ? in[ip++] : 'A';
  116. if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
  117. throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
  118. int b0 = map2[i0];
  119. int b1 = map2[i1];
  120. int b2 = map2[i2];
  121. int b3 = map2[i3];
  122. if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
  123. throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
  124. int o0 = ( b0 <<2) | (b1>>>4);
  125. int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
  126. int o2 = ((b2 & 3)<<6) | b3;
  127. out[op++] = (byte)o0;
  128. if (op<oLen) out[op++] = (byte)o1;
  129. if (op<oLen) out[op++] = (byte)o2; }
  130. return out; }
  131. // Dummy constructor.
  132. private Base64Coder() {}
  133. } // end class Base64Coder