| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.util.Arrays; |
| 36 | | | |
| 37 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 38 | | | |
| 39 | | | |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | @author |
| 53 | | | |
| 54 | | (1) | |
| 55 | | | |
| 56 | | | public final class Base64Util |
| 57 | | | { |
| 58 | | | private static final String ENCODED_PARAMETER = "encoded"; |
| 59 | | | private static final int LOWER_SIX_BITS = 0x3f; |
| 60 | | | private static final int BASELENGTH = 255; |
| 61 | | | private static final int BITS_PER_BASE64_CHAR = 6; |
| 62 | | | private static final int FOURBYTE = 4; |
| 63 | | | private static final int BYTES_PER_BASE64_CHUNK = 3; |
| 64 | | | private static final int TWENTYFOURBITGROUP = 3 * Constants.BITS_PER_BYTE; |
| 65 | | | private static final char PAD = '='; |
| 66 | 100 | | private static final char[] LOOKUP_BASE64_ALPHABET |
| 67 | | | = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 68 | | | .toCharArray(); |
| 69 | 100 | | private static final byte[] BASE64_ALPHABET = new byte[BASELENGTH]; |
| 70 | | | |
| 71 | | | static |
| 72 | | | { |
| 73 | 100 | | Arrays.fill(BASE64_ALPHABET, (byte) -1); |
| 74 | 100 | | for (int i = 0; i < LOOKUP_BASE64_ALPHABET.length; i++) |
| 75 | | | { |
| 76 | 100 | | BASE64_ALPHABET[LOOKUP_BASE64_ALPHABET[i]] = (byte) i; |
| 77 | | | } |
| 78 | 100 | | } |
| 79 | | | |
| 80 | | | private Base64Util () |
| 81 | 0 | | { |
| 82 | | | |
| 83 | 0 | | } |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | @param |
| 89 | | | @return |
| 90 | | | |
| 91 | | | public static char[] encodeToChars (byte[] binaryData) |
| 92 | | | { |
| 93 | | | final char[] result; |
| 94 | 100 | | if (binaryData == null) |
| 95 | | | { |
| 96 | 0 | | result = null; |
| 97 | | | } |
| 98 | 100 | | else if (binaryData.length == 0) |
| 99 | | | { |
| 100 | 0 | | result = new char[0]; |
| 101 | | | } |
| 102 | | | else |
| 103 | | | { |
| 104 | 100 | | final int dataBits = binaryData.length * Constants.BITS_PER_BYTE; |
| 105 | 100 | | final int remainingBits = dataBits % TWENTYFOURBITGROUP; |
| 106 | 100 | | final int numberTriplets = dataBits / TWENTYFOURBITGROUP; |
| 107 | 100 | | final int numberQuartet = remainingBits != 0 ? numberTriplets + 1 |
| 108 | | | : numberTriplets; |
| 109 | | | |
| 110 | 100 | | final char [] encodedData = new char[numberQuartet * FOURBYTE]; |
| 111 | 100 | | int encodedIndex = 0; |
| 112 | 100 | | int dataIndex = 0; |
| 113 | 100 | | for (int i = 0; i < numberTriplets; i++) |
| 114 | | | { |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | |
| 120 | | | |
| 121 | | | |
| 122 | 100 | | final int x |
| 123 | | | = (binaryData[dataIndex++] & Constants.BYTE_MASK) |
| 124 | | | << (2 * Constants.BITS_PER_BYTE) |
| 125 | | | | (binaryData[dataIndex++] & Constants.BYTE_MASK) |
| 126 | | | << Constants.BITS_PER_BYTE |
| 127 | | | | (binaryData[dataIndex++] & Constants.BYTE_MASK); |
| 128 | | | |
| 129 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 130 | | | [(x >>> (3 * BITS_PER_BASE64_CHAR)) & LOWER_SIX_BITS]; |
| 131 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 132 | | | [(x >>> (2 * BITS_PER_BASE64_CHAR)) & LOWER_SIX_BITS]; |
| 133 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 134 | | | [(x >>> BITS_PER_BASE64_CHAR) & LOWER_SIX_BITS]; |
| 135 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 136 | | | [x & LOWER_SIX_BITS]; |
| 137 | | | } |
| 138 | | | |
| 139 | 100 | | if (remainingBits == 2 * Constants.BITS_PER_BYTE) |
| 140 | | | { |
| 141 | | | |
| 142 | | | |
| 143 | | | |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | 100 | | final int x |
| 149 | | | = (binaryData[dataIndex++] & Constants.BYTE_MASK) |
| 150 | | | << Constants.BITS_PER_BYTE |
| 151 | | | | (binaryData[dataIndex++] & Constants.BYTE_MASK); |
| 152 | | | |
| 153 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 154 | | | [x >>> 10 & LOWER_SIX_BITS]; |
| 155 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 156 | | | [x >>> 4 & LOWER_SIX_BITS]; |
| 157 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 158 | | | [x << 2 & LOWER_SIX_BITS]; |
| 159 | 100 | | encodedData[encodedIndex++] = PAD; |
| 160 | 100 | | } |
| 161 | | | |
| 162 | 100 | | else if (remainingBits == Constants.BITS_PER_BYTE) |
| 163 | | | { |
| 164 | | | |
| 165 | | | |
| 166 | | | |
| 167 | | | |
| 168 | | | |
| 169 | | | |
| 170 | | | |
| 171 | 100 | | final int x |
| 172 | | | = (binaryData[dataIndex++] & Constants.BYTE_MASK); |
| 173 | | | |
| 174 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 175 | | | [(x >>> 2) & LOWER_SIX_BITS]; |
| 176 | 100 | | encodedData[encodedIndex++] = LOOKUP_BASE64_ALPHABET |
| 177 | | | [(x << 4) & LOWER_SIX_BITS]; |
| 178 | 100 | | encodedData[encodedIndex++] = PAD; |
| 179 | 100 | | encodedData[encodedIndex++] = PAD; |
| 180 | | | } |
| 181 | 100 | | result = encodedData; |
| 182 | | | } |
| 183 | 100 | | return result; |
| 184 | | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | @param |
| 190 | | | @return |
| 191 | | | |
| 192 | | | public static String encode (byte[] binaryData) |
| 193 | | | { |
| 194 | 100 | | return new String(encodeToChars(binaryData)); |
| 195 | | | } |
| 196 | | | |
| 197 | | | |
| 198 | | | |
| 199 | | | |
| 200 | | | <tt></tt> |
| 201 | | | |
| 202 | | | @param |
| 203 | | | |
| 204 | | | @param |
| 205 | | | |
| 206 | | | public static void appendEncoded (StringBuffer sb, byte[] binaryData) |
| 207 | | | { |
| 208 | 0 | | sb.append(encodeToChars(binaryData)); |
| 209 | 0 | | } |
| 210 | | | |
| 211 | | | |
| 212 | | | |
| 213 | | | |
| 214 | | | @param |
| 215 | | | @return |
| 216 | | | @throws |
| 217 | | | |
| 218 | | | |
| 219 | | | public static byte[] decode (String encoded) |
| 220 | | | throws ArgumentMalformedException |
| 221 | | | { |
| 222 | 100 | | Assert.notNull(encoded, ENCODED_PARAMETER); |
| 223 | | | final byte[] result; |
| 224 | | | |
| 225 | 100 | | if (encoded.length() % FOURBYTE != 0) |
| 226 | | | { |
| 227 | 100 | (2) | throw new ArgumentMalformedException(ENCODED_PARAMETER, encoded, |
| 228 | | | "Base64 length must be a multiple of " + FOURBYTE); |
| 229 | | | } |
| 230 | 100 | | final char[] base64Data = encoded.toCharArray(); |
| 231 | 100 | | final int numberQuadruple = base64Data.length / FOURBYTE; |
| 232 | | | |
| 233 | 100 | | if (numberQuadruple == 0) |
| 234 | | | { |
| 235 | 100 | | throw new ArgumentMalformedException(ENCODED_PARAMETER, encoded, |
| 236 | | | "Base64 length " + base64Data.length + " must be at least " |
| 237 | | | + FOURBYTE + " bytes"); |
| 238 | | | } |
| 239 | | | |
| 240 | 100 | | byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; |
| 241 | 100 | | int encodedIndex = 0; |
| 242 | 100 | | int dataIndex = 0; |
| 243 | 100 | | final byte[] decodedData |
| 244 | | | = new byte[(numberQuadruple) * BYTES_PER_BASE64_CHUNK]; |
| 245 | 100 | | final int pureBase64Chunks = numberQuadruple - 1; |
| 246 | | | |
| 247 | 100 | | for (int i = 0; i < pureBase64Chunks; i++) |
| 248 | | | { |
| 249 | 100 | | b1 = base64AlphabetLookup(base64Data[dataIndex++]); |
| 250 | 100 | | b2 = base64AlphabetLookup(base64Data[dataIndex++]); |
| 251 | 100 | | b3 = base64AlphabetLookup(base64Data[dataIndex++]); |
| 252 | 100 | | b4 = base64AlphabetLookup(base64Data[dataIndex++]); |
| 253 | | | |
| 254 | | | |
| 255 | | | |
| 256 | | | |
| 257 | | | |
| 258 | | | |
| 259 | 100 | | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| 260 | 100 | | decodedData[encodedIndex++] = (byte) (b2 << 4 | b3 >> 2); |
| 261 | 100 | | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); |
| 262 | | | } |
| 263 | | | |
| 264 | | | |
| 265 | 100 | | b1 = base64AlphabetLookup(base64Data[dataIndex++]); |
| 266 | 100 | | b2 = base64AlphabetLookup(base64Data[dataIndex++]); |
| 267 | 100 | | final char beforeLastChar = base64Data[dataIndex++]; |
| 268 | 100 | | final char lastChar = base64Data[dataIndex++]; |
| 269 | | | |
| 270 | 100 | (3)(4) | if (isData((beforeLastChar)) && isData((lastChar))) |
| 271 | | | { |
| 272 | | | |
| 273 | | | |
| 274 | | | |
| 275 | | | |
| 276 | | | |
| 277 | | | |
| 278 | 100 | | b3 = BASE64_ALPHABET[beforeLastChar]; |
| 279 | 100 | | b4 = BASE64_ALPHABET[lastChar]; |
| 280 | 100 | | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| 281 | 100 | | decodedData[encodedIndex++] = (byte) (b2 << 4 | b3 >> 2); |
| 282 | 100 | | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); |
| 283 | 100 | | result = decodedData; |
| 284 | | | } |
| 285 | | | else |
| 286 | | | { |
| 287 | 100 | | final int decodedDataLength = encodedIndex; |
| 288 | | | |
| 289 | 100 | | if (isPad(beforeLastChar) && isPad(lastChar)) |
| 290 | | | { |
| 291 | | | |
| 292 | 100 | | assertLastFourBitsZero(encoded, b2); |
| 293 | 100 | | final byte[] tmp = new byte[decodedDataLength + 1]; |
| 294 | 100 | | System.arraycopy(decodedData, 0, tmp, 0, decodedDataLength); |
| 295 | 100 | | tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); |
| 296 | 100 | | result = tmp; |
| 297 | 100 | | } |
| 298 | 100 | | else if (isData(beforeLastChar) && isPad(lastChar)) |
| 299 | | | { |
| 300 | | | |
| 301 | 100 | | b3 = BASE64_ALPHABET[beforeLastChar]; |
| 302 | 100 | | assertLastTwoBitsZero(encoded, b3); |
| 303 | 100 | | final byte[] tmp = new byte[decodedDataLength + 2]; |
| 304 | 100 | | System.arraycopy(decodedData, 0, tmp, 0, decodedDataLength); |
| 305 | 100 | | tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); |
| 306 | 100 | | tmp[encodedIndex] = (byte) (b2 << 4 | b3 >> 2); |
| 307 | 100 | | result = tmp; |
| 308 | 100 | | } |
| 309 | | | else |
| 310 | | | { |
| 311 | | | |
| 312 | | | |
| 313 | 100 | (5) | throw new ArgumentMalformedException(ENCODED_PARAMETER, encoded, |
| 314 | | | "At least one of the last 2 characters '" |
| 315 | | | + new StringBuffer().append(beforeLastChar).append(lastChar) |
| 316 | | | + "' are not a valid Base64 [padding] character"); |
| 317 | | | } |
| 318 | | | } |
| 319 | 100 | | return result; |
| 320 | | | } |
| 321 | | | |
| 322 | | | |
| 323 | | | private static void assertLastFourBitsZero (String encoded, byte b) |
| 324 | | | { |
| 325 | 100 | | if ((b & 0xf) != 0) |
| 326 | | | { |
| 327 | 100 | | throw new ArgumentMalformedException(ENCODED_PARAMETER, encoded, |
| 328 | | | "Last 4 bits should be zero of the last " |
| 329 | | | + "non-padding character '" |
| 330 | | | + Integer.toHexString(b) + "'"); |
| 331 | | | } |
| 332 | 100 | | } |
| 333 | | | |
| 334 | | | private static void assertLastTwoBitsZero (String encoded, byte b) |
| 335 | | | { |
| 336 | 100 | | if ((b & 0x3) != 0) |
| 337 | | | { |
| 338 | 0 | | throw new ArgumentMalformedException(ENCODED_PARAMETER, encoded, |
| 339 | | | "Last 2 bits should be zero of the last " |
| 340 | | | + "non-padding character '" |
| 341 | | | + Integer.toHexString(b) + "'"); |
| 342 | | | } |
| 343 | 100 | | } |
| 344 | | | |
| 345 | | | private static byte base64AlphabetLookup (char octect) |
| 346 | | | { |
| 347 | 100 | | if (!isData(octect)) |
| 348 | | | { |
| 349 | 100 | | throw new ArgumentMalformedException("octect", |
| 350 | | | Character.toString(octect), |
| 351 | | | "Illegal Base64 character '" + octect + "'"); |
| 352 | | | } |
| 353 | 100 | | return BASE64_ALPHABET[octect]; |
| 354 | | | } |
| 355 | | | |
| 356 | | | private static boolean isPad (char octect) |
| 357 | | | { |
| 358 | 100 | | return (octect == PAD); |
| 359 | | | } |
| 360 | | | |
| 361 | | | private static boolean isData (char octect) |
| 362 | | | { |
| 363 | 100 | | return (BASE64_ALPHABET[octect] != -1); |
| 364 | | | } |
| 365 | | | } |