Skip to content

Commit 059f12c

Browse files
author
Greg Curtis
committed
Remove dependency on Apache Commons
Add our own Base64 class for handling base64 encoding. This way we don't need to depend on all of Apache Commons.
1 parent 2073981 commit 059f12c

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ repositories {
1616

1717
dependencies {
1818
compile 'com.eclipsesource.minimal-json:minimal-json:0.9.1'
19-
compile 'commons-codec:commons-codec:1.10'
2019
testCompile 'junit:junit:4.11'
2120
testCompile 'org.hamcrest:hamcrest-library:1.3'
2221
testCompile 'com.github.tomakehurst:wiremock:1.52'
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

src/main/java/com/box/sdk/BoxAPIRequest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
import java.util.logging.Level;
1515
import java.util.logging.Logger;
1616

17-
import org.apache.commons.codec.binary.Base64;
18-
19-
2017
/**
2118
* Used to make HTTP requests to the Box API.
2219
*
@@ -355,8 +352,8 @@ private BoxAPIResponse trySend(ProgressListener listener) {
355352
if (this.api.getProxy() != null) {
356353
if (this.api.getProxyUsername() != null && this.api.getProxyPassword() != null) {
357354
String usernameAndPassword = this.api.getProxyUsername() + ":" + this.api.getProxyPassword();
358-
String encoded = new String(Base64.encodeBase64(usernameAndPassword.getBytes()));
359-
connection.addRequestProperty("Proxy-authorization", "Basic " + encoded);
355+
String encoded = new String(Base64.encode(usernameAndPassword.getBytes()));
356+
connection.addRequestProperty("Proxy-Authorization", "Basic " + encoded);
360357
}
361358
}
362359

0 commit comments

Comments
 (0)