|
| 1 | +package com.box.sdk; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | + |
| 5 | +/** |
| 6 | + * Contains a method for performing base64 encoding. |
| 7 | + * |
| 8 | + * <p>This class is included so that we don't need to add a dependency on Apache Commons for base64 encoding.</p> |
| 9 | + * |
| 10 | + * <p>The code in this class was mostly taken from https://gist.github.com/EmilHernvall/953733#file-base64-java</p> |
| 11 | + */ |
| 12 | +final class Base64 { |
| 13 | + |
| 14 | + private Base64() { } |
| 15 | + |
| 16 | + static String encode(byte[] data) { |
| 17 | + char[] tbl = { |
| 18 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 19 | + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 20 | + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 21 | + 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; |
| 22 | + |
| 23 | + StringBuilder buffer = new StringBuilder(); |
| 24 | + int pad = 0; |
| 25 | + for (int i = 0; i < data.length; i += 3) { |
| 26 | + |
| 27 | + int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF; |
| 28 | + if (i + 1 < data.length) { |
| 29 | + b |= (data[i + 1] & 0xFF) << 8; |
| 30 | + } else { |
| 31 | + pad++; |
| 32 | + } |
| 33 | + if (i + 2 < data.length) { |
| 34 | + b |= (data[i + 2] & 0xFF); |
| 35 | + } else { |
| 36 | + pad++; |
| 37 | + } |
| 38 | + |
| 39 | + for (int j = 0; j < 4 - pad; j++) { |
| 40 | + int c = (b & 0xFC0000) >> 18; |
| 41 | + buffer.append(tbl[c]); |
| 42 | + b <<= 6; |
| 43 | + } |
| 44 | + } |
| 45 | + for (int j = 0; j < pad; j++) { |
| 46 | + buffer.append("="); |
| 47 | + } |
| 48 | + |
| 49 | + return buffer.toString(); |
| 50 | + } |
| 51 | + |
| 52 | + public static byte[] decode(String data) { |
| 53 | + int[] tbl = { |
| 54 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 55 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 56 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, |
| 57 | + 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, |
| 58 | + 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 59 | + 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, |
| 60 | + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 61 | + 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 62 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 63 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 64 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 65 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 66 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 67 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 68 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; |
| 69 | + byte[] bytes = data.getBytes(); |
| 70 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 71 | + for (int i = 0; i < bytes.length;) { |
| 72 | + int b = 0; |
| 73 | + if (tbl[bytes[i]] != -1) { |
| 74 | + b = (tbl[bytes[i]] & 0xFF) << 18; |
| 75 | + } else { |
| 76 | + i++; |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + int num = 0; |
| 81 | + if (i + 1 < bytes.length && tbl[bytes[i + 1]] != -1) { |
| 82 | + b = b | ((tbl[bytes[i + 1]] & 0xFF) << 12); |
| 83 | + num++; |
| 84 | + } |
| 85 | + if (i + 2 < bytes.length && tbl[bytes[i + 2]] != -1) { |
| 86 | + b = b | ((tbl[bytes[i + 2]] & 0xFF) << 6); |
| 87 | + num++; |
| 88 | + } |
| 89 | + if (i + 3 < bytes.length && tbl[bytes[i + 3]] != -1) { |
| 90 | + b = b | (tbl[bytes[i + 3]] & 0xFF); |
| 91 | + num++; |
| 92 | + } |
| 93 | + |
| 94 | + while (num > 0) { |
| 95 | + int c = (b & 0xFF0000) >> 16; |
| 96 | + buffer.write((char) c); |
| 97 | + b <<= 8; |
| 98 | + num--; |
| 99 | + } |
| 100 | + i += 4; |
| 101 | + } |
| 102 | + return buffer.toByteArray(); |
| 103 | + } |
| 104 | +} |
0 commit comments