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