Class Base64Support
Base64.
This helper class specifically addresses that waste of the Apache Codec encode/decode static methods creating new
instances of the Base64 for every operation. It also provides the helper method to produce both chunked and
unchunked encoded content as strings.
Most importantly this insulates calling code so we can swap in a different Base64 implementation, as commons-codec and many if not most others have a very inconvenient stance on whitespace, which is absolutely necessary to ignore, so changes to the implementation are likely to continue.
As of 9.3, a new system property has been introduced (SANITIZE_WHITESPACE_PROPERTY_NAME) to enable
compatibility with newer commons-codec versions being used under this library at the cost of a whitespace removal scan.
We do not support use of the library with that version, but the property provides a workaround for that case without
incurring additional overhead for the project's own use. A future major update may obviate the need for that workaround.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanChunk the encoded data into 76-character lines broken by CRLF characters.private static final org.apache.commons.codec.binary.Base64Encoder used to produce chunked output.static final booleanTriggers whitespace sanitization of input when decoding, per JSSH-84.static final StringSystem property name to enable whitespace sanitization on decode for commons-codec 1.23 compatibility.static final booleanDo not chunk encoded data.private static final org.apache.commons.codec.binary.Base64Encoder used to produce unchunked output. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Decodes (un)chunked Base64 encoded data.static byte[]decodeURLSafe(String data) Decodes (un)chunked Base64URL encoded data.static Stringencode(byte[] data, boolean chunked) Base64 encodes the given binary data.static StringencodeURLSafe(byte[] data) Base64URL encodes the given binary data.
-
Field Details
-
SANITIZE_WHITESPACE_PROPERTY_NAME
System property name to enable whitespace sanitization on decode for commons-codec 1.23 compatibility.- See Also:
-
CHUNKED
public static final boolean CHUNKEDChunk the encoded data into 76-character lines broken by CRLF characters.- See Also:
-
UNCHUNKED
public static final boolean UNCHUNKEDDo not chunk encoded data.- See Also:
-
sanitize
public static final boolean sanitizeTriggers whitespace sanitization of input when decoding, per JSSH-84. -
CHUNKED_ENCODER
@Nonnull private static final org.apache.commons.codec.binary.Base64 CHUNKED_ENCODEREncoder used to produce chunked output. -
UNCHUNKED_ENCODER
@Nonnull private static final org.apache.commons.codec.binary.Base64 UNCHUNKED_ENCODEREncoder used to produce unchunked output.
-
-
Constructor Details
-
Base64Support
private Base64Support()Constructor.
-
-
Method Details
-
encode
@Nonnull public static String encode(@Nonnull byte[] data, boolean chunked) throws EncodingException Base64 encodes the given binary data.- Parameters:
data- data to encodechunked- whether the encoded data should be chunked or not- Returns:
- the base64 encoded data
- Throws:
EncodingException- when anyExceptionis thrown from the underlying encoder, or the output is null.
-
decode
Decodes (un)chunked Base64 encoded data.- Parameters:
data- Base64 encoded data- Returns:
- the decoded data
- Throws:
DecodingException- when anyExceptionis thrown from the underlying decoder, or the output is null.
-
encodeURLSafe
Base64URL encodes the given binary data.This is compliant with RFC 4648, Section 5: "Base 64 Encoding with URL and Filename Safe Alphabet".
- Parameters:
data- data to encode- Returns:
- the base64url encoded data
- Throws:
EncodingException- if the input data can not be encoded as a base64 string.
-
decodeURLSafe
Decodes (un)chunked Base64URL encoded data.This is compliant with RFC 4648, Section 5: "Base 64 Encoding with URL and Filename Safe Alphabet".
- Parameters:
data- Base64URL encoded data- Returns:
- the decoded data
- Throws:
DecodingException- if unable to decode the input data.
-