public abstract class Base64Utils
extends java.lang.Object
Adapts to either Java 8's Base64
class or Apache Commons Codec's
org.apache.commons.codec.binary.Base64
class. With neither Java 8 nor Commons
Codec present, encode(byte[])
/decode(byte[])
calls will throw an IllegalStateException.
However, as of Spring 4.2, encodeToString(byte[])
and decodeFromString(java.lang.String)
will
nevertheless work since they can delegate to the JAXB DatatypeConverter as a fallback.
However, this does not apply when using the ...UrlSafe... methods for RFC 4648 "URL and
Filename Safe Alphabet"; a delegate is required.
Note: Apache Commons Codec does not add padding (=
) when encoding with
the URL and Filename Safe Alphabet.
Base64
,
org.apache.commons.codec.binary.Base64
,
DatatypeConverter.printBase64Binary(byte[])
,
DatatypeConverter.parseBase64Binary(java.lang.String)
Modifier and Type | Class and Description |
---|---|
(package private) static interface |
Base64Utils.Base64Delegate |
(package private) static class |
Base64Utils.CommonsCodecBase64Delegate |
(package private) static class |
Base64Utils.JdkBase64Delegate |
Modifier and Type | Field and Description |
---|---|
private static java.nio.charset.Charset |
DEFAULT_CHARSET |
private static Base64Utils.Base64Delegate |
delegate |
Constructor and Description |
---|
Base64Utils() |
Modifier and Type | Method and Description |
---|---|
private static void |
assertDelegateAvailable()
Assert that Byte64 encoding between byte arrays is actually supported.
|
static byte[] |
decode(byte[] src)
Base64-decode the given byte array.
|
static byte[] |
decodeFromString(java.lang.String src)
Base64-decode the given byte array from an UTF-8 String.
|
static byte[] |
decodeFromUrlSafeString(java.lang.String src)
Base64-decode the given byte array from an UTF-8 String using the RFC 4868
"URL and Filename Safe Alphabet".
|
static byte[] |
decodeUrlSafe(byte[] src)
Base64-decode the given byte array using the RFC 4868
"URL and Filename Safe Alphabet".
|
static byte[] |
encode(byte[] src)
Base64-encode the given byte array.
|
static java.lang.String |
encodeToString(byte[] src)
Base64-encode the given byte array to a String.
|
static java.lang.String |
encodeToUrlSafeString(byte[] src)
Base64-encode the given byte array to a String using the RFC 4868
"URL and Filename Safe Alphabet".
|
static byte[] |
encodeUrlSafe(byte[] src)
Base64-encode the given byte array using the RFC 4868
"URL and Filename Safe Alphabet".
|
private static final java.nio.charset.Charset DEFAULT_CHARSET
private static final Base64Utils.Base64Delegate delegate
private static void assertDelegateAvailable()
java.lang.IllegalStateException
- if neither Java 8 nor Apache Commons Codec is presentpublic static byte[] encode(byte[] src)
src
- the original byte array (may be null
)null
if the input was null
)java.lang.IllegalStateException
- if Base64 encoding between byte arrays is not
supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtimepublic static byte[] decode(byte[] src)
src
- the encoded byte array (may be null
)null
if the input was null
)java.lang.IllegalStateException
- if Base64 encoding between byte arrays is not
supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtimepublic static byte[] encodeUrlSafe(byte[] src)
src
- the original byte array (may be null
)null
if the input was null
)java.lang.IllegalStateException
- if Base64 encoding between byte arrays is not
supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtimepublic static byte[] decodeUrlSafe(byte[] src)
src
- the encoded byte array (may be null
)null
if the input was null
)java.lang.IllegalStateException
- if Base64 encoding between byte arrays is not
supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtimepublic static java.lang.String encodeToString(byte[] src)
src
- the original byte array (may be null
)null
if the input was null
)public static byte[] decodeFromString(java.lang.String src)
src
- the encoded UTF-8 String (may be null
)null
if the input was null
)public static java.lang.String encodeToUrlSafeString(byte[] src)
src
- the original byte array (may be null
)null
if the input was null
)java.lang.IllegalStateException
- if Base64 encoding between byte arrays is not
supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtimepublic static byte[] decodeFromUrlSafeString(java.lang.String src)
src
- the encoded UTF-8 String (may be null
)null
if the input was null
)java.lang.IllegalStateException
- if Base64 encoding between byte arrays is not
supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime