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