|
1 | 1 | package com.pusher.client.util; |
2 | 2 |
|
3 | | -// copied from: https://stackoverflow.com/a/4265472/501940 |
| 3 | +import static java.util.Arrays.fill; |
| 4 | + |
| 5 | +// copied from: https://stackoverflow.com/a/4265472/501940 and improved (naming, char validation) |
4 | 6 | public class Base64 { |
5 | 7 |
|
6 | | - private final static char[] ALPHABET = |
| 8 | + private final static char[] CHAR_INDEX_TABLE = |
7 | 9 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); |
8 | 10 |
|
9 | | - private static int[] toInt = new int[128]; |
| 11 | + private static int[] charToIndexSparseMappingArray = new int[128]; |
10 | 12 |
|
11 | 13 | static { |
12 | | - for (int i = 0; i < ALPHABET.length; i++) { |
13 | | - toInt[ALPHABET[i]] = i; |
| 14 | + fill(charToIndexSparseMappingArray, -1); |
| 15 | + for (int i = 0; i < CHAR_INDEX_TABLE.length; i++) { |
| 16 | + charToIndexSparseMappingArray[CHAR_INDEX_TABLE[i]] = i; |
14 | 17 | } |
15 | 18 | } |
16 | 19 |
|
| 20 | + private static int toInt(char character) { |
| 21 | + int retVal = charToIndexSparseMappingArray[character]; |
| 22 | + if (retVal == -1) throw new IllegalArgumentException("invalid char: " + character); |
| 23 | + return retVal; |
| 24 | + } |
| 25 | + |
17 | 26 | public static byte[] decode(String base64String) { |
18 | | - int delta = base64String.endsWith("==") ? 2 : base64String.endsWith("=") ? 1 : 0; |
19 | | - byte[] buffer = new byte[base64String.length() * 3 / 4 - delta]; |
| 27 | + int paddingSize = base64String.endsWith("==") ? 2 : base64String.endsWith("=") ? 1 : 0; |
| 28 | + byte[] retVal = new byte[base64String.length() * 3 / 4 - paddingSize]; |
20 | 29 | int mask = 0xFF; |
21 | 30 | int index = 0; |
22 | 31 | for (int i = 0; i < base64String.length(); i += 4) { |
23 | | - int c0 = toInt[base64String.charAt(i)]; |
24 | | - int c1 = toInt[base64String.charAt(i + 1)]; |
25 | | - buffer[index++] = (byte) (((c0 << 2) | (c1 >> 4)) & mask); |
26 | | - if (index >= buffer.length) { |
27 | | - return buffer; |
| 32 | + int c0 = toInt(base64String.charAt(i)); |
| 33 | + int c1 = toInt(base64String.charAt(i + 1)); |
| 34 | + retVal[index++] = (byte) (((c0 << 2) | (c1 >> 4)) & mask); |
| 35 | + if (index >= retVal.length) { |
| 36 | + return retVal; |
28 | 37 | } |
29 | | - int c2 = toInt[base64String.charAt(i + 2)]; |
30 | | - buffer[index++] = (byte) (((c1 << 4) | (c2 >> 2)) & mask); |
31 | | - if (index >= buffer.length) { |
32 | | - return buffer; |
| 38 | + int c2 = toInt(base64String.charAt(i + 2)); |
| 39 | + retVal[index++] = (byte) (((c1 << 4) | (c2 >> 2)) & mask); |
| 40 | + if (index >= retVal.length) { |
| 41 | + return retVal; |
33 | 42 | } |
34 | | - int c3 = toInt[base64String.charAt(i + 3)]; |
35 | | - buffer[index++] = (byte) (((c2 << 6) | c3) & mask); |
| 43 | + int c3 = toInt(base64String.charAt(i + 3)); |
| 44 | + retVal[index++] = (byte) (((c2 << 6) | c3) & mask); |
36 | 45 | } |
37 | | - return buffer; |
| 46 | + return retVal; |
38 | 47 | } |
39 | 48 | } |
0 commit comments