- * This is needed in order to support platforms like Android which already include an older version - * of the Apache Commons Codec (Android includes version 1.3). To avoid a dependency library - * conflict, this library includes a reduced private copy of version 1.6 (or newer) of the Apache - * Commons Codec (using a tool like jarjar). - *
+ * Proxy for handling Base64 encoding/decoding. * * @since 1.8 * @author Yaniv Inbar @@ -36,10 +31,9 @@ public class Base64 { * @param binaryData binary data to encode or {@code null} for {@code null} result * @return byte[] containing Base64 characters in their UTF-8 representation or {@code null} for * {@code null} input - * @see org.apache.commons.codec.binary.Base64#encodeBase64(byte[]) */ public static byte[] encodeBase64(byte[] binaryData) { - return org.apache.commons.codec.binary.Base64.encodeBase64(binaryData); + return StringUtils.getBytesUtf8(encodeBase64String(binaryData)); } /** @@ -47,10 +41,9 @@ public static byte[] encodeBase64(byte[] binaryData) { * * @param binaryData binary data to encode or {@code null} for {@code null} result * @return String containing Base64 characters or {@code null} for {@code null} input - * @see org.apache.commons.codec.binary.Base64#encodeBase64String(byte[]) */ public static String encodeBase64String(byte[] binaryData) { - return org.apache.commons.codec.binary.Base64.encodeBase64String(binaryData); + return BaseEncoding.base64().encode(binaryData); } @@ -61,10 +54,9 @@ public static String encodeBase64String(byte[] binaryData) { * @param binaryData binary data to encode or {@code null} for {@code null} result * @return byte[] containing Base64 characters in their UTF-8 representation or {@code null} for * {@code null} input - * @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafe(byte[]) */ public static byte[] encodeBase64URLSafe(byte[] binaryData) { - return org.apache.commons.codec.binary.Base64.encodeBase64URLSafe(binaryData); + return StringUtils.getBytesUtf8(encodeBase64URLSafeString(binaryData)); } /** @@ -73,32 +65,38 @@ public static byte[] encodeBase64URLSafe(byte[] binaryData) { * * @param binaryData binary data to encode or {@code null} for {@code null} result * @return String containing Base64 characters or {@code null} for {@code null} input - * @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafeString(byte[]) */ public static String encodeBase64URLSafeString(byte[] binaryData) { - return org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(binaryData); + return BaseEncoding.base64Url().omitPadding().encode(binaryData); } /** - * Decodes Base64 data into octets. + * Decodes Base64 data into octets. Note that this method handles both URL-safe and + * non-URL-safe base 64 encoded inputs. * * @param base64Data Byte array containing Base64 data or {@code null} for {@code null} result * @return Array containing decoded data or {@code null} for {@code null} input - * @see org.apache.commons.codec.binary.Base64#decodeBase64(byte[]) */ public static byte[] decodeBase64(byte[] base64Data) { - return org.apache.commons.codec.binary.Base64.decodeBase64(base64Data); + return decodeBase64(StringUtils.newStringUtf8(base64Data)); } /** - * Decodes a Base64 String into octets. + * Decodes a Base64 String into octets. Note that this method handles both URL-safe and + * non-URL-safe base 64 encoded strings. * * @param base64String String containing Base64 data or {@code null} for {@code null} result * @return Array containing decoded data or {@code null} for {@code null} input - * @see org.apache.commons.codec.binary.Base64#decodeBase64(String) */ public static byte[] decodeBase64(String base64String) { - return org.apache.commons.codec.binary.Base64.decodeBase64(base64String); + try { + return BaseEncoding.base64().decode(base64String); + } catch (IllegalArgumentException e) { + if (e.getCause() instanceof DecodingException) { + return BaseEncoding.base64Url().decode(base64String); + } + throw e; + } } private Base64() { diff --git a/google-http-client/src/main/java/com/google/api/client/util/StringUtils.java b/google-http-client/src/main/java/com/google/api/client/util/StringUtils.java index 8d0c92532..80b1273cc 100644 --- a/google-http-client/src/main/java/com/google/api/client/util/StringUtils.java +++ b/google-http-client/src/main/java/com/google/api/client/util/StringUtils.java @@ -15,19 +15,12 @@ package com.google.api.client.util; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * Utilities for strings. * - *- * Some of these methods are a proxy for version 1.6 (or newer) of the Apache Commons Codec - * {@link StringUtils} implementation. This is needed in order to support platforms like Android - * which already include an older version of the Apache Commons Codec (Android includes version - * 1.3). To avoid a dependency library conflict, this library includes a reduced private copy of - * version 1.6 (or newer) of the Apache Commons Codec (using a tool like jarjar). - *
- * * @since 1.8 * @author Yaniv Inbar */ @@ -48,13 +41,15 @@ public class StringUtils { * @return encoded bytes, ornull if the input string was null
* @throws IllegalStateException Thrown when the charset is missing, which should be never
* according the the Java specification.
- * @see Standard charsets
- * @see org.apache.commons.codec.binary.StringUtils#getBytesUtf8(String)
* @since 1.8
*/
public static byte[] getBytesUtf8(String string) {
- return org.apache.commons.codec.binary.StringUtils.getBytesUtf8(string);
+ if (string == null) {
+ return null;
+ }
+ return string.getBytes(StandardCharsets.UTF_8);
}
/**
@@ -66,11 +61,13 @@ public static byte[] getBytesUtf8(String string) {
* charset, or null if the input byte array was null.
* @throws IllegalStateException Thrown when a {@link UnsupportedEncodingException} is caught,
* which should never happen since the charset is required.
- * @see org.apache.commons.codec.binary.StringUtils#newStringUtf8(byte[])
* @since 1.8
*/
public static String newStringUtf8(byte[] bytes) {
- return org.apache.commons.codec.binary.StringUtils.newStringUtf8(bytes);
+ if (bytes == null) {
+ return null;
+ }
+ return new String(bytes, StandardCharsets.UTF_8);
}
private StringUtils() {
diff --git a/google-http-client/src/test/java/com/google/api/client/util/StringUtilsTest.java b/google-http-client/src/test/java/com/google/api/client/util/StringUtilsTest.java
index 0f7887197..3606f3ba3 100644
--- a/google-http-client/src/test/java/com/google/api/client/util/StringUtilsTest.java
+++ b/google-http-client/src/test/java/com/google/api/client/util/StringUtilsTest.java
@@ -40,7 +40,15 @@ public void testToBytesUtf8() {
Assert.assertArrayEquals(SAMPLE_UTF8, StringUtils.getBytesUtf8(SAMPLE));
}
+ public void testToBytesUtf8Null() {
+ assertNull(StringUtils.getBytesUtf8(null));
+ }
+
public void testFromBytesUtf8() {
assertEquals(SAMPLE, StringUtils.newStringUtf8(SAMPLE_UTF8));
}
+
+ public void testFromBytesUtf8Null() {
+ assertNull(StringUtils.newStringUtf8(null));
+ }
}
diff --git a/pom.xml b/pom.xml
index 84c905f42..546342f52 100644
--- a/pom.xml
+++ b/pom.xml
@@ -150,11 +150,6 @@