Base64.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.ydtech.utils;
  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 Array containing binaryData
  54. * @return Encoded Base64 array
  55. */
  56. public static String encode(byte[] binaryData) {
  57. if (binaryData == null) {
  58. return null;
  59. }
  60. int lengthDataBits = binaryData.length * EIGHTBIT;
  61. if (lengthDataBits == 0) {
  62. return "";
  63. }
  64. int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
  65. int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
  66. int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;
  67. char encodedData[] = null;
  68. encodedData = new char[numberQuartet * 4];
  69. byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
  70. int encodedIndex = 0;
  71. int dataIndex = 0;
  72. if (fDebug) {
  73. }
  74. for (int i = 0; i < numberTriplets; i++) {
  75. b1 = binaryData[dataIndex++];
  76. b2 = binaryData[dataIndex++];
  77. b3 = binaryData[dataIndex++];
  78. if (fDebug) { }
  79. l = (byte) (b2 & 0x0f);
  80. k = (byte) (b1 & 0x03);
  81. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  82. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
  83. byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
  84. if (fDebug) { }
  85. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  86. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  87. encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
  88. encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
  89. }
  90. // form integral number of 6-bit groups
  91. if (fewerThan24bits == EIGHTBIT) {
  92. b1 = binaryData[dataIndex];
  93. k = (byte) (b1 & 0x03);
  94. if (fDebug) { }
  95. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  96. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  97. encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
  98. encodedData[encodedIndex++] = PAD;
  99. encodedData[encodedIndex++] = PAD;
  100. } else if (fewerThan24bits == SIXTEENBIT) {
  101. b1 = binaryData[dataIndex];
  102. b2 = binaryData[dataIndex + 1];
  103. l = (byte) (b2 & 0x0f);
  104. k = (byte) (b1 & 0x03);
  105. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  106. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
  107. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  108. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  109. encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
  110. encodedData[encodedIndex++] = PAD;
  111. }
  112. return new String(encodedData);
  113. }
  114. /**
  115. * Decodes Base64 data into octects
  116. *
  117. * @param encoded string containing Base64 data
  118. * @return Array containind decoded data.
  119. */
  120. public static byte[] decode(String encoded) {
  121. if (encoded == null) {
  122. return null;
  123. }
  124. char[] base64Data = encoded.toCharArray();
  125. // remove white spaces
  126. int len = removeWhiteSpace(base64Data);
  127. if (len % FOURBYTE != 0) {
  128. return null;//should be divisible by four
  129. }
  130. int numberQuadruple = (len / FOURBYTE);
  131. if (numberQuadruple == 0) {
  132. return new byte[0];
  133. }
  134. byte decodedData[] = null;
  135. byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  136. char d1 = 0, d2 = 0, d3 = 0, d4 = 0;
  137. int i = 0;
  138. int encodedIndex = 0;
  139. int dataIndex = 0;
  140. decodedData = new byte[(numberQuadruple) * 3];
  141. for (; i < numberQuadruple - 1; i++) {
  142. if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))
  143. || !isData((d3 = base64Data[dataIndex++]))
  144. || !isData((d4 = base64Data[dataIndex++]))) {
  145. return null;
  146. }//if found "no data" just return null
  147. b1 = base64Alphabet[d1];
  148. b2 = base64Alphabet[d2];
  149. b3 = base64Alphabet[d3];
  150. b4 = base64Alphabet[d4];
  151. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  152. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  153. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  154. }
  155. if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) {
  156. return null;//if found "no data" just return null
  157. }
  158. b1 = base64Alphabet[d1];
  159. b2 = base64Alphabet[d2];
  160. d3 = base64Data[dataIndex++];
  161. d4 = base64Data[dataIndex++];
  162. if (!isData((d3)) || !isData((d4))) {//Check if they are PAD characters
  163. if (isPad(d3) && isPad(d4)) {
  164. if ((b2 & 0xf) != 0)//last 4 bits should be zero
  165. {
  166. return null;
  167. }
  168. byte[] tmp = new byte[i * 3 + 1];
  169. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  170. tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
  171. return tmp;
  172. } else if (!isPad(d3) && isPad(d4)) {
  173. b3 = base64Alphabet[d3];
  174. if ((b3 & 0x3) != 0)//last 2 bits should be zero
  175. {
  176. return null;
  177. }
  178. byte[] tmp = new byte[i * 3 + 2];
  179. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  180. tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  181. tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  182. return tmp;
  183. } else {
  184. return null;
  185. }
  186. } else { //No PAD e.g 3cQl
  187. b3 = base64Alphabet[d3];
  188. b4 = base64Alphabet[d4];
  189. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  190. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  191. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  192. }
  193. return decodedData;
  194. }
  195. /**
  196. * remove WhiteSpace from MIME containing encoded Base64 data.
  197. *
  198. * @param data the byte array of base64 data (with WS)
  199. * @return the new length
  200. */
  201. private static int removeWhiteSpace(char[] data) {
  202. if (data == null) {
  203. return 0;
  204. }
  205. // count characters that's not whitespace
  206. int newSize = 0;
  207. int len = data.length;
  208. for (int i = 0; i < len; i++) {
  209. if (!isWhiteSpace(data[i])) {
  210. data[newSize++] = data[i];
  211. }
  212. }
  213. return newSize;
  214. }
  215. }