8000 Replace Base64 from Apache Commons Codec with equivalent from Guava. … · monical1/google-http-java-client@ef9f914 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef9f914

Browse files
Replace Base64 from Apache Commons Codec with equivalent from Guava. (googleapis#374)
Replace Base64 from Apache Commons Codec with equivalent from Guava.
1 parent b69f50e commit ef9f914

File tree

2 files changed

+94
-27
lines changed

2 files changed

+94
-27
lines changed

google-http-client/src/main/java/com/google/api/client/util/Base64.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@
1414

1515
package com.google.api.client.util;
1616

17+
import com.google.common.io.BaseEncoding;
18+
1719
/**
18-
* Proxy for version 1.6 (or newer) of the Apache Commons Codec
19-
* {@link org.apache.commons.codec.binary.Base64} implementation.
20-
*
21-
* <p>
22-
* This is needed in order to support platforms like Android which already include an older version
23-
* of the Apache Commons Codec (Android includes version 1.3). To avoid a dependency library
24-
* conflict, this library includes a reduced private copy of version 1.6 (or newer) of the Apache
25-
* Commons Codec (using a tool like jarjar).
26-
* </p>
20+
* Proxy for base 64 encoding/decoding which matches the Base64 interface in Apache Commons (for
21+
* historical reasons).
2722
*
28-
* @since 1.8
2923
* @author Yaniv Inbar
24+
* @since 1.8
3025
*/
3126
public class Base64 {
3227

@@ -35,36 +30,41 @@ public class Base64 {
3530
*
3631
* @param binaryData binary data to encode or {@code null} for {@code null} result
3732
* @return byte[] containing Base64 characters in their UTF-8 representation or {@code null} for
38-
* {@code null} input
39-
* @see org.apache.commons.codec.binary.Base64#encodeBase64(byte[])
33+
* {@code null} input
4034
*/
4135
public static byte[] encodeBase64(byte[] binaryData) {
42-
return org.apache.commons.codec.binary.Base64.encodeBase64(binaryData);
36+
if (binaryData == null) {
37+
return null;
38+
}
39+
return BaseEncoding.base64().encode(binaryData).getBytes();
4340
}
4441

4542
/**
4643
* Encodes binary data using the base64 algorithm but does not chunk the output.
4744
*
4845
* @param binaryData binary data to encode or {@code null} for {@code null} result
4946
* @return String containing Base64 characters or {@code null} for {@code null} input
50-
* @see org.apache.commons.codec.binary.Base64#encodeBase64String(byte[])
5147
*/
5248
public static String encodeBase64String(byte[] binaryData) {
53-
return org.apache.commons.codec.binary.Base64.encodeBase64String(binaryData);
49+
if (binaryData == null) {
50+
return null;
51+
}
52+
return BaseEncoding.base64().encode(binaryData);
5453
}
5554

56-
5755
/**
5856
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the
5957
* output. The url-safe variation emits - and _ instead of + and / characters.
6058
*
6159
* @param binaryData binary data to encode or {@code null} for {@code null} result
6260
* @return byte[] containing Base64 characters in their UTF-8 representation or {@code null} for
63-
* {@code null} input
64-
* @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafe(byte[])
61+
* {@code null} input
6562
*/
6663
public static byte[] encodeBase64URLSafe(byte[] binaryData) {
67-
return org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(binaryData);
64+
if (binaryData == null) {
65+
return null;
66+
}
67+
return BaseEncoding.base64Url().omitPadding().encode(binaryData).getBytes();
6868
}
6969

7070
/**
@@ -73,34 +73,42 @@ public static byte[] encodeBase64URLSafe(byte[] binaryData) {
7373
*
7474
* @param binaryData binary data to encode or {@code null} for {@code null} result
7575
* @return String containing Base64 characters or {@code null} for {@code null} input
76-
* @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafeString(byte[])
7776
*/
7877
public static String encodeBase64URLSafeString(byte[] binaryData) {
79-
return org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(binaryData);
78+
if (binaryData == null) {
79+
return null;
80+
}
81+
return BaseEncoding.base64Url().omitPadding().encode(binaryData);
8082
}
8183

8284
/**
8385
* Decodes Base64 data into octets.
8486
*
8587
* @param base64Data Byte array containing Base64 data or {@code null} for {@code null} result
8688
* @return Array containing decoded data or {@code null} for {@code null} input
87-
* @see org.apache.commons.codec.binary.Base64#decodeBase64(byte[])
8889
*/
8990
public static byte[] decodeBase64(byte[] base64Data) {
90-
return org.apache.commons.codec.binary.Base64.decodeBase64(base64Data);
91+
if (base64Data == null) {
92+
return null;
93+
}
94+
return decodeBase64(new String(base64Data));
9195
}
9296

9397
/**
9498
* Decodes a Base64 String into octets.
9599
*
96100
* @param base64String String containing Base64 data or {@code null} for {@code null} result
97101
* @return Array containing decoded data or {@code null} for {@code null} input
98-
* @see org.apache.commons.codec.binary.Base64#decodeBase64(String)
99102
*/
100103
public static byte[] decodeBase64(String base64String) {
101-
return org.apache.commons.codec.binary.Base64.decodeBase64(base64String);
102-
}
104+
if (base64String == null) {
105+
return null;
106+
}
103107

104-
private Base64() {
108+
try {
109+
return BaseEncoding.base64().decode(base64String);
110+
} catch (IllegalArgumentException e) {
111+
return BaseEncoding.base64Url().omitPadding().decode(base64String);
112+
}
105113
}
106114
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.api.client.util;
15+
16+
import junit.framework.TestCase;
17+
18+
import java.util.Arrays;
19+
20+
/**
21+
* Tests {@link Base64}.
22+
*
23+
* @author mwhisenhunt@google.com (Matt Whisenhunt)
24+
*/
25+
public class Base64Test extends TestCase {
26+
27+
public void testEmptyValue() {
28+
byte[] emptyValue = new byte[0];
29+
30+
assertEquals(Base64.encodeBase64(emptyValue).length, 0);
31+
assertEquals(Base64.encodeBase64String(emptyValue).length(), 0);
32+
assertEquals(Base64.encodeBase64URLSafe(emptyValue).length, 0);
33+
assertEquals(Base64.encodeBase64URLSafeString(emptyValue).length(), 0);
34+
assertEquals(Base64.decodeBase64(emptyValue).length, 0);
35+
assertEquals(Base64.decodeBase64(emptyValue).length, 0);
36+
}
37+
38+
public void testNulls() {
39+
assertNull(Base64.encodeBase64(null));
40+
assertNull(Base64.encodeBase64String(null));
41+
assertNull(Base64.encodeBase64URLSafe(null));
42+
assertNull(Base64.encodeBase64URLSafeString(null));
43+
assertNull(Base64.decodeBase64((byte[]) null));
44+
assertNull(Base64.decodeBase64((String) null));
45+
}
46+
47+
public void testEncode() {
48+
assertEquals("Zm9vYmFy", new String(Base64.encodeBase64("foobar".getBytes())));
49+
assertEquals("Zm9vYmFy", Base64.encodeBase64String("foobar".getBytes()));
50+
51+
assertEquals("Zm9vYmFy", new String(Base64.encodeBase64URLSafe("foobar".getBytes())));
52+
assertEquals("Zm9vYmFy", Base64.encodeBase64URLSafeString("foobar".getBytes()));
53+
}
54+
55+
public void testDecode() {
56+
String value = new String(Base64.decodeBase64(Base64.encodeBase64("foobar".getBytes())));
57+
assertEquals("foobar", value);
58+
}
59+
}

0 commit comments

Comments
 (0)
0