6d4fb2bc1b88942628dd0e16fcec085b03662c55.svn-base 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package xin.glue.cargocnHttpClient;
  2. public final class Base64 {
  3. static private final int BASELENGTH = 128;
  4. static private final int LOOKUPLENGTH = 64;
  5. static private final int TWENTYFOURBITGROUP = 24;
  6. static private final int EIGHTBIT = 8;
  7. static private final int SIXTEENBIT = 16;
  8. static private final int FOURBYTE = 4;
  9. static private final int SIGN = -128;
  10. static private final char PAD = '=';
  11. static private final boolean fDebug = false;
  12. static final private byte[] base64Alphabet = new byte[BASELENGTH];
  13. static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
  14. static {
  15. for (int i = 0; i < BASELENGTH; ++i) {
  16. base64Alphabet[i] = -1;
  17. }
  18. for (int i = 'Z'; i >= 'A'; i--) {
  19. base64Alphabet[i] = (byte) (i - 'A');
  20. }
  21. for (int i = 'z'; i >= 'a'; i--) {
  22. base64Alphabet[i] = (byte) (i - 'a' + 26);
  23. }
  24. for (int i = '9'; i >= '0'; i--) {
  25. base64Alphabet[i] = (byte) (i - '0' + 52);
  26. }
  27. base64Alphabet['+'] = 62;
  28. base64Alphabet['/'] = 63;
  29. for (int i = 0; i <= 25; i++) {
  30. lookUpBase64Alphabet[i] = (char) ('A' + i);
  31. }
  32. for (int i = 26, j = 0; i <= 51; i++, j++) {
  33. lookUpBase64Alphabet[i] = (char) ('a' + j);
  34. }
  35. for (int i = 52, j = 0; i <= 61; i++, j++) {
  36. lookUpBase64Alphabet[i] = (char) ('0' + j);
  37. }
  38. lookUpBase64Alphabet[62] = (char) '+';
  39. lookUpBase64Alphabet[63] = (char) '/';
  40. }
  41. private static boolean isWhiteSpace(char octect) {
  42. return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
  43. }
  44. private static boolean isPad(char octect) {
  45. return (octect == PAD);
  46. }
  47. private static boolean isData(char octect) {
  48. return (octect < BASELENGTH && base64Alphabet[octect] != -1);
  49. }
  50. /**
  51. * Encodes hex octects into Base64
  52. *
  53. * @param binaryData
  54. * Array containing binaryData
  55. * @return Encoded Base64 array
  56. */
  57. public static String encode(byte[] binaryData) {
  58. if (binaryData == null) {
  59. return null;
  60. }
  61. int lengthDataBits = binaryData.length * EIGHTBIT;
  62. if (lengthDataBits == 0) {
  63. return "";
  64. }
  65. int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
  66. int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
  67. int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1
  68. : numberTriplets;
  69. char encodedData[] = null;
  70. encodedData = new char[numberQuartet * 4];
  71. byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
  72. int encodedIndex = 0;
  73. int dataIndex = 0;
  74. if (fDebug) {
  75. System.out.println("number of triplets = " + numberTriplets);
  76. }
  77. for (int i = 0; i < numberTriplets; i++) {
  78. b1 = binaryData[dataIndex++];
  79. b2 = binaryData[dataIndex++];
  80. b3 = binaryData[dataIndex++];
  81. if (fDebug) {
  82. System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3);
  83. }
  84. l = (byte) (b2 & 0x0f);
  85. k = (byte) (b1 & 0x03);
  86. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
  87. : (byte) ((b1) >> 2 ^ 0xc0);
  88. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
  89. : (byte) ((b2) >> 4 ^ 0xf0);
  90. byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
  91. : (byte) ((b3) >> 6 ^ 0xfc);
  92. if (fDebug) {
  93. System.out.println("val2 = " + val2);
  94. System.out.println("k4 = " + (k << 4));
  95. System.out.println("vak = " + (val2 | (k << 4)));
  96. }
  97. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  98. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  99. encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
  100. encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
  101. }
  102. // form integral number of 6-bit groups
  103. if (fewerThan24bits == EIGHTBIT) {
  104. b1 = binaryData[dataIndex];
  105. k = (byte) (b1 & 0x03);
  106. if (fDebug) {
  107. System.out.println("b1=" + b1);
  108. System.out.println("b1<<2 = " + (b1 >> 2));
  109. }
  110. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
  111. : (byte) ((b1) >> 2 ^ 0xc0);
  112. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  113. encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
  114. encodedData[encodedIndex++] = PAD;
  115. encodedData[encodedIndex++] = PAD;
  116. } else if (fewerThan24bits == SIXTEENBIT) {
  117. b1 = binaryData[dataIndex];
  118. b2 = binaryData[dataIndex + 1];
  119. l = (byte) (b2 & 0x0f);
  120. k = (byte) (b1 & 0x03);
  121. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
  122. : (byte) ((b1) >> 2 ^ 0xc0);
  123. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
  124. : (byte) ((b2) >> 4 ^ 0xf0);
  125. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  126. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  127. encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
  128. encodedData[encodedIndex++] = PAD;
  129. }
  130. return new String(encodedData);
  131. }
  132. /**
  133. * Decodes Base64 data into octects
  134. *
  135. * @param encoded
  136. * string containing Base64 data
  137. * @return Array containind decoded data.
  138. */
  139. public static byte[] decode(String encoded) {
  140. if (encoded == null) {
  141. return null;
  142. }
  143. char[] base64Data = encoded.toCharArray();
  144. // remove white spaces
  145. int len = removeWhiteSpace(base64Data);
  146. if (len % FOURBYTE != 0) {
  147. return null;// should be divisible by four
  148. }
  149. int numberQuadruple = (len / FOURBYTE);
  150. if (numberQuadruple == 0) {
  151. return new byte[0];
  152. }
  153. byte decodedData[] = null;
  154. byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  155. char d1 = 0, d2 = 0, d3 = 0, d4 = 0;
  156. int i = 0;
  157. int encodedIndex = 0;
  158. int dataIndex = 0;
  159. decodedData = new byte[(numberQuadruple) * 3];
  160. for (; i < numberQuadruple - 1; i++) {
  161. if (!isData((d1 = base64Data[dataIndex++]))
  162. || !isData((d2 = base64Data[dataIndex++]))
  163. || !isData((d3 = base64Data[dataIndex++]))
  164. || !isData((d4 = base64Data[dataIndex++]))) {
  165. return null;
  166. }// if found "no data" just return null
  167. b1 = base64Alphabet[d1];
  168. b2 = base64Alphabet[d2];
  169. b3 = base64Alphabet[d3];
  170. b4 = base64Alphabet[d4];
  171. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  172. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  173. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  174. }
  175. if (!isData((d1 = base64Data[dataIndex++]))
  176. || !isData((d2 = base64Data[dataIndex++]))) {
  177. return null;// if found "no data" just return null
  178. }
  179. b1 = base64Alphabet[d1];
  180. b2 = base64Alphabet[d2];
  181. d3 = base64Data[dataIndex++];
  182. d4 = base64Data[dataIndex++];
  183. if (!isData((d3)) || !isData((d4))) {// Check if they are PAD characters
  184. if (isPad(d3) && isPad(d4)) {
  185. if ((b2 & 0xf) != 0)// last 4 bits should be zero
  186. {
  187. return null;
  188. }
  189. byte[] tmp = new byte[i * 3 + 1];
  190. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  191. tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
  192. return tmp;
  193. } else if (!isPad(d3) && isPad(d4)) {
  194. b3 = base64Alphabet[d3];
  195. if ((b3 & 0x3) != 0)// last 2 bits should be zero
  196. {
  197. return null;
  198. }
  199. byte[] tmp = new byte[i * 3 + 2];
  200. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  201. tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  202. tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  203. return tmp;
  204. } else {
  205. return null;
  206. }
  207. } else { // No PAD e.g 3cQl
  208. b3 = base64Alphabet[d3];
  209. b4 = base64Alphabet[d4];
  210. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  211. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  212. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  213. }
  214. return decodedData;
  215. }
  216. /**
  217. * remove WhiteSpace from MIME containing encoded Base64 data.
  218. *
  219. * @param data
  220. * the byte array of base64 data (with WS)
  221. * @return the new length
  222. */
  223. private static int removeWhiteSpace(char[] data) {
  224. if (data == null) {
  225. return 0;
  226. }
  227. // count characters that's not whitespace
  228. int newSize = 0;
  229. int len = data.length;
  230. for (int i = 0; i < len; i++) {
  231. if (!isWhiteSpace(data[i])) {
  232. data[newSize++] = data[i];
  233. }
  234. }
  235. return newSize;
  236. }
  237. }