14
14
15
15
package com .google .api .client .util ;
16
16
17
+ import com .google .common .io .BaseEncoding ;
18
+
17
19
/**
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).
27
22
*
28
- * @since 1.8
29
23
* @author Yaniv Inbar
24
+ * @since 1.8
30
25
*/
31
26
public class Base64 {
32
27
@@ -35,36 +30,41 @@ public class Base64 {
35
30
*
36
31
* @param binaryData binary data to encode or {@code null} for {@code null} result
37
32
* @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
40
34
*/
41
35
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 ();
43
40
}
44
41
45
42
/**
46
43
* Encodes binary data using the base64 algorithm but does not chunk the output.
47
44
*
48
45
* @param binaryData binary data to encode or {@code null} for {@code null} result
49
46
* @return String containing Base64 characters or {@code null} for {@code null} input
50
- * @see org.apache.commons.codec.binary.Base64#encodeBase64String(byte[])
51
47
*/
52
48
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 );
54
53
}
55
54
56
-
57
55
/**
58
56
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the
59
57
* output. The url-safe variation emits - and _ instead of + and / characters.
60
58
*
61
59
* @param binaryData binary data to encode or {@code null} for {@code null} result
62
60
* @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
65
62
*/
66
63
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 ();
68
68
}
69
69
70
70
/**
@@ -73,34 +73,42 @@ public static byte[] encodeBase64URLSafe(byte[] binaryData) {
73
73
*
74
74
* @param binaryData binary data to encode or {@code null} for {@code null} result
75
75
* @return String containing Base64 characters or {@code null} for {@code null} input
76
- * @see org.apache.commons.codec.binary.Base64#encodeBase64URLSafeString(byte[])
77
76
*/
78
77
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 );
80
82
}
81
83
82
84
/**
83
85
* Decodes Base64 data into octets.
84
86
*
85
87
* @param base64Data Byte array containing Base64 data or {@code null} for {@code null} result
86
88
* @return Array containing decoded data or {@code null} for {@code null} input
87
- * @see org.apache.commons.codec.binary.Base64#decodeBase64(byte[])
88
89
*/
89
90
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 ));
91
95
}
92
96
93
97
/**
94
98
* Decodes a Base64 String into octets.
95
99
*
96
100
* @param base64String String containing Base64 data or {@code null} for {@code null} result
97
101
* @return Array containing decoded data or {@code null} for {@code null} input
98
- * @see org.apache.commons.codec.binary.Base64#decodeBase64(String)
99
102
*/
100
103
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
+ }
103
107
104
- private Base64 () {
108
+ try {
109
+ return BaseEncoding .base64 ().decode (base64String );
110
+ } catch (IllegalArgumentException e ) {
111
+ return BaseEncoding .base64Url ().omitPadding ().decode (base64String );
112
+ }
105
113
}
106
114
}
0 commit comments