Package com.fasterxml.jackson.core
Class Base64Variant
java.lang.Object
com.fasterxml.jackson.core.Base64Variant
- All Implemented Interfaces:
Serializable
Class used to define specific details of which
variant of Base64 encoding/decoding is to be used. Although there is
somewhat standard basic version (so-called "MIME Base64"), other variants
exists, see Base64 Wikipedia entry for details.
- Author:
- Tatu Saloranta
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Defines how the Base64Variant deals with Padding while reading -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Marker used to denote ascii characters that do not correspond to a 6-bit value (in this variant), and is not used as a padding character.static final int
Marker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).protected static final char
Placeholder used by "no padding" variant, to be used when a character value is needed. -
Constructor Summary
ConstructorDescriptionBase64Variant
(Base64Variant base, String name, boolean writePadding, char paddingChar, int maxLineLength) "Copy constructor" that can be used when the base alphabet is identical to one used by another variant, but other details (padding, maximum line length) differBase64Variant
(Base64Variant base, String name, int maxLineLength) "Copy constructor" that can be used when the base alphabet is identical to one used by another variant except for the maximum line length (and obviously, name).Base64Variant
(String name, String base64Alphabet, boolean writePadding, char paddingChar, int maxLineLength) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected void
protected void
_reportInvalidBase64
(char ch, int bindex, String msg) boolean
byte[]
Convenience method for decoding contents of a Base64-encoded String, using this variant's settings.void
decode
(String str, ByteArrayBuilder builder) Convenience method for decoding contents of a Base64-encoded String, using this variant's settings and appending decoded binary data using providedByteArrayBuilder
.int
decodeBase64Byte
(byte b) int
decodeBase64Char
(char c) int
decodeBase64Char
(int ch) encode
(byte[] input) Convenience method for converting given byte array as base64 encoded String using this variant's settings.encode
(byte[] input, boolean addQuotes) Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes.Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes.byte
encodeBase64BitsAsByte
(int value) char
encodeBase64BitsAsChar
(int value) int
encodeBase64Chunk
(int b24, byte[] buffer, int outPtr) Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 bytes (ascii), stored in given result buffer.int
encodeBase64Chunk
(int b24, char[] buffer, int outPtr) Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 characters, stored in given result buffer.void
encodeBase64Chunk
(StringBuilder sb, int b24) int
encodeBase64Partial
(int bits, int outputBytes, byte[] buffer, int outPtr) Method that outputs partial chunk (which only encodes one or two bytes of data).int
encodeBase64Partial
(int bits, int outputBytes, char[] buffer, int outPtr) Method that outputs partial chunk (which only encodes one or two bytes of data).void
encodeBase64Partial
(StringBuilder sb, int bits, int outputBytes) boolean
int
getName()
byte
char
int
hashCode()
Helper method that will construct a message to use in exceptions for cases where input ends prematurely in place where padding would be expected.protected Object
boolean
toString()
protected String
Helper method that will construct a message to use in exceptions for cases where input ends prematurely in place where padding is not expected.boolean
boolean
usesPaddingChar
(char c) boolean
usesPaddingChar
(int ch) withReadPadding
(Base64Variant.PaddingReadBehaviour readPadding) withWritePadding
(boolean writePadding)
-
Field Details
-
PADDING_CHAR_NONE
protected static final char PADDING_CHAR_NONEPlaceholder used by "no padding" variant, to be used when a character value is needed.- See Also:
-
BASE64_VALUE_INVALID
public static final int BASE64_VALUE_INVALIDMarker used to denote ascii characters that do not correspond to a 6-bit value (in this variant), and is not used as a padding character.- See Also:
-
BASE64_VALUE_PADDING
public static final int BASE64_VALUE_PADDINGMarker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).- See Also:
-
-
Constructor Details
-
Base64Variant
-
Base64Variant
"Copy constructor" that can be used when the base alphabet is identical to one used by another variant except for the maximum line length (and obviously, name).- Parameters:
base
- Variant to use for settings not specific by other parametersname
- Name of this variantmaxLineLength
- Maximum length (in characters) of lines to output before using linefeed
-
Base64Variant
public Base64Variant(Base64Variant base, String name, boolean writePadding, char paddingChar, int maxLineLength) "Copy constructor" that can be used when the base alphabet is identical to one used by another variant, but other details (padding, maximum line length) differ- Parameters:
base
- Variant to use for settings not specific by other parametersname
- Name of this variantwritePadding
- Whether variant will use padding when encodingpaddingChar
- Padding character used for encoding, excepted on reading, if anymaxLineLength
- Maximum length (in characters) of lines to output before using linefeed
-
-
Method Details
-
withPaddingAllowed
- Returns:
- Base64Variant which does not require padding on read
- Since:
- 2.12
-
withPaddingRequired
- Returns:
- Base64Variant which requires padding on read
- Since:
- 2.12
-
withPaddingForbidden
- Returns:
- Base64Variant which does not accept padding on read
- Since:
- 2.12
-
withReadPadding
- Parameters:
readPadding
- Padding read behavior desired- Returns:
- Instance with desired padding read behavior setting (this if already has setting; new instance otherwise)
- Since:
- 2.12
-
withWritePadding
- Parameters:
writePadding
- Determines if padding is output on write or not- Returns:
- Base64Variant which writes padding or not depending on writePadding
- Since:
- 2.12
-
readResolve
-
getName
-
usesPadding
public boolean usesPadding()- Returns:
- True if this Base64 encoding will write padding on output (note: before Jackson 2.12 also dictated whether padding was accepted on read)
-
requiresPaddingOnRead
public boolean requiresPaddingOnRead()- Returns:
True
if this variant requires padding on content decoded;false
if not.- Since:
- 2.12
-
acceptsPaddingOnRead
public boolean acceptsPaddingOnRead()- Returns:
True
if this variant accepts padding on content decoded;false
if not.- Since:
- 2.12
-
usesPaddingChar
public boolean usesPaddingChar(char c) -
usesPaddingChar
public boolean usesPaddingChar(int ch) -
paddingReadBehaviour
- Returns:
- Indicator on how this Base64 encoding will handle possible padding in content when reading.
- Since:
- 2.12
-
getPaddingChar
public char getPaddingChar() -
getPaddingByte
public byte getPaddingByte() -
getMaxLineLength
public int getMaxLineLength() -
decodeBase64Char
public int decodeBase64Char(char c) - Parameters:
c
- Character to decode- Returns:
- 6-bit decoded value, if valid character;
-
decodeBase64Char
public int decodeBase64Char(int ch) -
decodeBase64Byte
public int decodeBase64Byte(byte b) -
encodeBase64BitsAsChar
public char encodeBase64BitsAsChar(int value) -
encodeBase64Chunk
public int encodeBase64Chunk(int b24, char[] buffer, int outPtr) Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 characters, stored in given result buffer. Caller must ensure there is sufficient space for 4 encoded characters at specified position.- Parameters:
b24
- 3-byte value to encodebuffer
- Output buffer to append characters tooutPtr
- Starting position withinbuffer
to append encoded characters- Returns:
- Pointer in output buffer after appending 4 encoded characters
-
encodeBase64Chunk
-
encodeBase64Partial
public int encodeBase64Partial(int bits, int outputBytes, char[] buffer, int outPtr) Method that outputs partial chunk (which only encodes one or two bytes of data). Data given is still aligned same as if it as full data; that is, missing data is at the "right end" (LSB) of int.- Parameters:
bits
- 24-bit chunk containing 1 or 2 bytes to encodeoutputBytes
- Number of input bytes to encode (either 1 or 2)buffer
- Output buffer to append characters tooutPtr
- Starting position withinbuffer
to append encoded characters- Returns:
- Pointer in output buffer after appending encoded characters (2, 3 or 4)
-
encodeBase64Partial
-
encodeBase64BitsAsByte
public byte encodeBase64BitsAsByte(int value) -
encodeBase64Chunk
public int encodeBase64Chunk(int b24, byte[] buffer, int outPtr) Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 bytes (ascii), stored in given result buffer.- Parameters:
b24
- 3-byte value to encodebuffer
- Output buffer to append characters (as bytes) tooutPtr
- Starting position withinbuffer
to append encoded characters- Returns:
- Pointer in output buffer after appending 4 encoded characters
-
encodeBase64Partial
public int encodeBase64Partial(int bits, int outputBytes, byte[] buffer, int outPtr) Method that outputs partial chunk (which only encodes one or two bytes of data). Data given is still aligned same as if it as full data; that is, missing data is at the "right end" (LSB) of int.- Parameters:
bits
- 24-bit chunk containing 1 or 2 bytes to encodeoutputBytes
- Number of input bytes to encode (either 1 or 2)buffer
- Output buffer to append characters tooutPtr
- Starting position withinbuffer
to append encoded characters- Returns:
- Pointer in output buffer after appending encoded characters (2, 3 or 4)
-
encode
Convenience method for converting given byte array as base64 encoded String using this variant's settings. Resulting value is "raw", that is, not enclosed in double-quotes.- Parameters:
input
- Byte array to encode- Returns:
- Base64 encoded String of encoded
input
bytes, not surrounded by quotes
-
encode
Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes. Linefeeds added, if needed, are expressed as 2-character JSON (and Java source) escape sequence of backslash + `n`.- Parameters:
input
- Byte array to encodeaddQuotes
- Whether to surround resulting value in double quotes or not- Returns:
- Base64 encoded String of encoded
input
bytes, possibly surrounded by quotes (ifaddQuotes
enabled)
-
encode
Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes. Linefeed character to use is passed explicitly.- Parameters:
input
- Byte array to encodeaddQuotes
- Whether to surround resulting value in double quotes or notlinefeed
- Linefeed to use for encoded content- Returns:
- Base64 encoded String of encoded
input
bytes - Since:
- 2.10
-
decode
Convenience method for decoding contents of a Base64-encoded String, using this variant's settings.- Parameters:
input
- Base64-encoded input String to decode- Returns:
- Byte array of decoded contents
- Throws:
IllegalArgumentException
- if input is not valid base64 encoded data- Since:
- 2.3
-
decode
Convenience method for decoding contents of a Base64-encoded String, using this variant's settings and appending decoded binary data using providedByteArrayBuilder
.NOTE: builder will NOT be reset before decoding (nor cleared afterwards); assumption is that caller will ensure it is given in proper state, and used as appropriate afterwards.
- Parameters:
str
- Input to decodebuilder
- Builder used for assembling decoded content- Throws:
IllegalArgumentException
- if input is not valid base64 encoded data- Since:
- 2.3
-
toString
-
equals
-
hashCode
public int hashCode() -
_reportInvalidBase64
protected void _reportInvalidBase64(char ch, int bindex, String msg) throws IllegalArgumentException - Parameters:
ch
- Character to report onbindex
- Relative index within base64 character unit; between 0 and 3 (as unit has exactly 4 characters)msg
- Base message to use for exception- Throws:
IllegalArgumentException
-
_reportBase64EOF
- Throws:
IllegalArgumentException
-
_reportBase64UnexpectedPadding
- Throws:
IllegalArgumentException
-
unexpectedPaddingMessage
Helper method that will construct a message to use in exceptions for cases where input ends prematurely in place where padding is not expected.- Returns:
- Exception message for indicating "unexpected padding" case
- Since:
- 2.12
-
missingPaddingMessage
Helper method that will construct a message to use in exceptions for cases where input ends prematurely in place where padding would be expected.- Returns:
- Exception message for indicating "missing padding" case
- Since:
- 2.10
-