Top Description Inners Fields Constructors Methods
com.fasterxml.jackson.core

public final Class Base64Variant

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.util.Arrays, com.fasterxml.jackson.core.util.ByteArrayBuilder

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
Base64Variant.PaddingReadBehaviour

Defines how the Base64Variant deals with Padding while reading

Field Summary

Modifier and TypeField and Description
private final transient int[]
_asciiToBase64

Decoding table used for base 64 decoding.

private final transient byte[]
_base64ToAsciiB

Alternative encoding table used for base 64 decoding when output is done as ascii bytes.

private final transient char[]
_base64ToAsciiC

Encoding table used for base 64 decoding when output is done as characters.

private final int
_maxLineLength

Maximum number of encoded base64 characters to output during encoding before adding a linefeed, if line length is to be limited (java.lang.Integer#MAX_VALUE if not limited).

pack-priv final String
_name

Symbolic name of variant; used for diagnostics/debugging.

private final char
_paddingChar

Character used for padding, if any (PADDING_CHAR_NONE if not).

private final Base64Variant.PaddingReadBehaviour
_paddingReadBehaviour

Whether padding characters should be required or not while decoding

private final boolean
_writePadding

Whether this variant uses padding when writing out content or not.

public static final int
BASE64_VALUE_INVALID

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.

public static final int
BASE64_VALUE_PADDING

Marker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).

private static final int
protected static final char
PADDING_CHAR_NONE

Placeholder used by "no padding" variant, to be used when a character value is needed.

private static final long

Constructor Summary

AccessConstructor and Description
public
Base64Variant(String name, String base64Alphabet, boolean writePadding, char paddingChar, int maxLineLength)

public
Base64Variant(Base64Variant
Variant to use for settings not specific by other parameters
base
,
String
Name of this variant
name
,
int
Maximum length (in characters) of lines to output before using linefeed
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).

public
Base64Variant(Base64Variant
Variant to use for settings not specific by other parameters
base
,
String
Name of this variant
name
,
boolean
Whether variant will use padding when encoding
writePadding
,
char
Padding character used for encoding, excepted on reading, if any
paddingChar
,
int
Maximum length (in characters) of lines to output before using linefeed
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

private
Base64Variant(Base64Variant base, String name, boolean writePadding, char paddingChar, Base64Variant.PaddingReadBehaviour paddingReadBehaviour, int maxLineLength)

private

Method Summary

Modifier and TypeMethod and Description
protected void
protected void
protected void
_reportInvalidBase64(char
Character to report on
ch
,
int
Relative index within base64 character unit; between 0 and 3 (as unit has exactly 4 characters)
bindex
,
String
Base message to use for exception
msg
)

public boolean

Returns:

True if this variant accepts padding on content decoded; false if not.
acceptsPaddingOnRead
()

public byte[]

Returns:

Byte array of decoded contents
decode
(String
Base64-encoded input String to decode
input
)

Convenience method for decoding contents of a Base64-encoded String, using this variant's settings.

public void
decode(String
Input to decode
str
,
ByteArrayBuilder
Builder used for assembling decoded content
builder
)

Convenience method for decoding contents of a Base64-encoded String, using this variant's settings and appending decoded binary data using provided ByteArrayBuilder.

public int
public int

Returns:

6-bit decoded value, if valid character;
decodeBase64Char
(char
Character to decode
c
)

public int
public String

Returns:

Base64 encoded String of encoded input bytes, not surrounded by quotes
encode
(byte[]
Byte array to encode
input
)

Convenience method for converting given byte array as base64 encoded String using this variant's settings.

public String

Returns:

Base64 encoded String of encoded input bytes, possibly surrounded by quotes (if addQuotes enabled)
encode
(byte[]
Byte array to encode
input
,
boolean
Whether to surround resulting value in double quotes or not
addQuotes
)

Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes.

public String

Returns:

Base64 encoded String of encoded input bytes
encode
(byte[]
Byte array to encode
input
,
boolean
Whether to surround resulting value in double quotes or not
addQuotes
,
String
Linefeed to use for encoded content
linefeed
)

Convenience method for converting given byte array as base64 encoded String using this variant's settings, optionally enclosed in double-quotes.

public byte
public char
public int

Returns:

Pointer in output buffer after appending 4 encoded characters
encodeBase64Chunk
(int
3-byte value to encode
b24
,
char[]
Output buffer to append characters to
buffer
,
int
Starting position within buffer to append encoded characters
outPtr
)

Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 characters, stored in given result buffer.

public void
public int

Returns:

Pointer in output buffer after appending 4 encoded characters
encodeBase64Chunk
(int
3-byte value to encode
b24
,
byte[]
Output buffer to append characters (as bytes) to
buffer
,
int
Starting position within buffer to append encoded characters
outPtr
)

Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 bytes (ascii), stored in given result buffer.

public int

Returns:

Pointer in output buffer after appending encoded characters (2, 3 or 4)
encodeBase64Partial
(int
24-bit chunk containing 1 or 2 bytes to encode
bits
,
int
Number of input bytes to encode (either 1 or 2)
outputBytes
,
char[]
Output buffer to append characters to
buffer
,
int
Starting position within buffer to append encoded characters
outPtr
)

Method that outputs partial chunk (which only encodes one or two bytes of data).

public void
encodeBase64Partial(StringBuilder sb, int bits, int outputBytes)

public int

Returns:

Pointer in output buffer after appending encoded characters (2, 3 or 4)
encodeBase64Partial
(int
24-bit chunk containing 1 or 2 bytes to encode
bits
,
int
Number of input bytes to encode (either 1 or 2)
outputBytes
,
byte[]
Output buffer to append characters to
buffer
,
int
Starting position within buffer to append encoded characters
outPtr
)

Method that outputs partial chunk (which only encodes one or two bytes of data).

public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
public String
public byte
public char
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String

Returns:

Exception message for indicating "missing padding" case
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.

public Base64Variant.PaddingReadBehaviour

Returns:

Indicator on how this Base64 encoding will handle possible padding in content when reading.
paddingReadBehaviour
()

protected Object
public boolean

Returns:

True if this variant requires padding on content decoded; false if not.
requiresPaddingOnRead
()

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

protected String

Returns:

Exception message for indicating "unexpected padding" case
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.

public boolean

Returns:

True if this Base64 encoding will write padding on output (note: before Jackson 2.12 also dictated whether padding was accepted on read)
usesPadding
()

public boolean
public boolean
public Base64Variant

Returns:

Base64Variant which does not require padding on read
withPaddingAllowed
()

public Base64Variant

Returns:

Base64Variant which does not accept padding on read
withPaddingForbidden
()

public Base64Variant

Returns:

Base64Variant which requires padding on read
withPaddingRequired
()

public Base64Variant

Returns:

Instance with desired padding read behavior setting (this if already has setting; new instance otherwise)
withReadPadding
(Base64Variant.PaddingReadBehaviour
Padding read behavior desired
readPadding
)

public Base64Variant

Returns:

Base64Variant which writes padding or not depending on writePadding
withWritePadding
(boolean
Determines if padding is output on write or not
writePadding
)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

_asciiToBase64back to summary
private final transient int[] _asciiToBase64

Decoding table used for base 64 decoding.

_base64ToAsciiBback to summary
private final transient byte[] _base64ToAsciiB

Alternative encoding table used for base 64 decoding when output is done as ascii bytes.

_base64ToAsciiCback to summary
private final transient char[] _base64ToAsciiC

Encoding table used for base 64 decoding when output is done as characters.

_maxLineLengthback to summary
private final int _maxLineLength

Maximum number of encoded base64 characters to output during encoding before adding a linefeed, if line length is to be limited (java.lang.Integer#MAX_VALUE if not limited).

Note

for some output modes (when writing attributes) linefeeds may need to be avoided, and this value ignored.

_nameback to summary
pack-priv final String _name

Symbolic name of variant; used for diagnostics/debugging.

Note that this is the only non-transient field; used when reading back from serialized state.

Also: must not be private, accessed from `BaseVariants`

_paddingCharback to summary
private final char _paddingChar

Character used for padding, if any (PADDING_CHAR_NONE if not).

_paddingReadBehaviourback to summary
private final Base64Variant.PaddingReadBehaviour _paddingReadBehaviour

Whether padding characters should be required or not while decoding

Since
2.12
_writePaddingback to summary
private final boolean _writePadding

Whether this variant uses padding when writing out content or not.

Since
2.12
BASE64_VALUE_INVALIDback to summary
public static final int BASE64_VALUE_INVALID

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.

BASE64_VALUE_PADDINGback to summary
public static final int BASE64_VALUE_PADDING

Marker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).

INT_SPACEback to summary
private static final int INT_SPACE
PADDING_CHAR_NONEback to summary
protected static final char PADDING_CHAR_NONE

Placeholder used by "no padding" variant, to be used when a character value is needed.

serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

Base64Variantback to summary
public Base64Variant(String name, String base64Alphabet, boolean writePadding, char paddingChar, int maxLineLength)
Base64Variantback to summary
public Base64Variant(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).

Parameters
base:Base64Variant

Variant to use for settings not specific by other parameters

name:String

Name of this variant

maxLineLength:int

Maximum length (in characters) of lines to output before using linefeed

Base64Variantback to summary
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:Base64Variant

Variant to use for settings not specific by other parameters

name:String

Name of this variant

writePadding:boolean

Whether variant will use padding when encoding

paddingChar:char

Padding character used for encoding, excepted on reading, if any

maxLineLength:int

Maximum length (in characters) of lines to output before using linefeed

Base64Variantback to summary
private Base64Variant(Base64Variant base, String name, boolean writePadding, char paddingChar, Base64Variant.PaddingReadBehaviour paddingReadBehaviour, int maxLineLength)
Base64Variantback to summary
private Base64Variant(Base64Variant base, Base64Variant.PaddingReadBehaviour paddingReadBehaviour)

Method Detail

_reportBase64EOFback to summary
protected void _reportBase64EOF() throws IllegalArgumentException
_reportBase64UnexpectedPaddingback to summary
protected void _reportBase64UnexpectedPadding() throws IllegalArgumentException
_reportInvalidBase64back to summary
protected void _reportInvalidBase64(char ch, int bindex, String msg) throws IllegalArgumentException
Parameters
ch:char

Character to report on

bindex:int

Relative index within base64 character unit; between 0 and 3 (as unit has exactly 4 characters)

msg:String

Base message to use for exception

acceptsPaddingOnReadback to summary
public boolean acceptsPaddingOnRead()
Returns:boolean

True if this variant accepts padding on content decoded; false if not.

Since
2.12
decodeback to summary
public byte[] decode(String input) throws IllegalArgumentException

Convenience method for decoding contents of a Base64-encoded String, using this variant's settings.

Parameters
input:String

Base64-encoded input String to decode

Returns:byte[]

Byte array of decoded contents

Annotations
@SuppressWarnings:resource
Exceptions
IllegalArgumentException:
if input is not valid base64 encoded data
Since
2.3
decodeback to summary
public void decode(String str, ByteArrayBuilder builder) throws IllegalArgumentException

Convenience method for decoding contents of a Base64-encoded String, using this variant's settings and appending decoded binary data using provided ByteArrayBuilder.

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:String

Input to decode

builder:ByteArrayBuilder

Builder used for assembling decoded content

Exceptions
IllegalArgumentException:
if input is not valid base64 encoded data
Since
2.3
decodeBase64Byteback to summary
public int decodeBase64Byte(byte b)
decodeBase64Charback to summary
public int decodeBase64Char(char c)
Parameters
c:char

Character to decode

Returns:int

6-bit decoded value, if valid character;

decodeBase64Charback to summary
public int decodeBase64Char(int ch)
encodeback to summary
public String encode(byte[] input)

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[]

Byte array to encode

Returns:String

Base64 encoded String of encoded input bytes, not surrounded by quotes

encodeback to summary
public String 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. Linefeeds added, if needed, are expressed as 2-character JSON (and Java source) escape sequence of backslash + `n`.

Parameters
input:byte[]

Byte array to encode

addQuotes:boolean

Whether to surround resulting value in double quotes or not

Returns:String

Base64 encoded String of encoded input bytes, possibly surrounded by quotes (if addQuotes enabled)

encodeback to summary
public String encode(byte[] input, boolean addQuotes, String linefeed)

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[]

Byte array to encode

addQuotes:boolean

Whether to surround resulting value in double quotes or not

linefeed:String

Linefeed to use for encoded content

Returns:String

Base64 encoded String of encoded input bytes

Since
2.10
encodeBase64BitsAsByteback to summary
public byte encodeBase64BitsAsByte(int value)
encodeBase64BitsAsCharback to summary
public char encodeBase64BitsAsChar(int value)
encodeBase64Chunkback to summary
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:int

3-byte value to encode

buffer:char[]

Output buffer to append characters to

outPtr:int

Starting position within buffer to append encoded characters

Returns:int

Pointer in output buffer after appending 4 encoded characters

encodeBase64Chunkback to summary
public void encodeBase64Chunk(StringBuilder sb, int b24)
encodeBase64Chunkback to summary
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:int

3-byte value to encode

buffer:byte[]

Output buffer to append characters (as bytes) to

outPtr:int

Starting position within buffer to append encoded characters

Returns:int

Pointer in output buffer after appending 4 encoded characters

encodeBase64Partialback to summary
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:int

24-bit chunk containing 1 or 2 bytes to encode

outputBytes:int

Number of input bytes to encode (either 1 or 2)

buffer:char[]

Output buffer to append characters to

outPtr:int

Starting position within buffer to append encoded characters

Returns:int

Pointer in output buffer after appending encoded characters (2, 3 or 4)

encodeBase64Partialback to summary
public void encodeBase64Partial(StringBuilder sb, int bits, int outputBytes)
encodeBase64Partialback to summary
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:int

24-bit chunk containing 1 or 2 bytes to encode

outputBytes:int

Number of input bytes to encode (either 1 or 2)

buffer:byte[]

Output buffer to append characters to

outPtr:int

Starting position within buffer to append encoded characters

Returns:int

Pointer in output buffer after appending encoded characters (2, 3 or 4)

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getMaxLineLengthback to summary
public int getMaxLineLength()
getNameback to summary
public String getName()
getPaddingByteback to summary
public byte getPaddingByte()
getPaddingCharback to summary
public char getPaddingChar()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
missingPaddingMessageback to summary
public String 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:String

Exception message for indicating "missing padding" case

Since
2.10
paddingReadBehaviourback to summary
public Base64Variant.PaddingReadBehaviour paddingReadBehaviour()
Returns:Base64Variant.PaddingReadBehaviour

Indicator on how this Base64 encoding will handle possible padding in content when reading.

Since
2.12
readResolveback to summary
protected Object readResolve()
requiresPaddingOnReadback to summary
public boolean requiresPaddingOnRead()
Returns:boolean

True if this variant requires padding on content decoded; false if not.

Since
2.12
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
unexpectedPaddingMessageback to summary
protected String 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:String

Exception message for indicating "unexpected padding" case

Since
2.12
usesPaddingback to summary
public boolean usesPadding()
Returns:boolean

True if this Base64 encoding will write padding on output (note: before Jackson 2.12 also dictated whether padding was accepted on read)

usesPaddingCharback to summary
public boolean usesPaddingChar(char c)
usesPaddingCharback to summary
public boolean usesPaddingChar(int ch)
withPaddingAllowedback to summary
public Base64Variant withPaddingAllowed()
Returns:Base64Variant

Base64Variant which does not require padding on read

Since
2.12
withPaddingForbiddenback to summary
public Base64Variant withPaddingForbidden()
Returns:Base64Variant

Base64Variant which does not accept padding on read

Since
2.12
withPaddingRequiredback to summary
public Base64Variant withPaddingRequired()
Returns:Base64Variant

Base64Variant which requires padding on read

Since
2.12
withReadPaddingback to summary
public Base64Variant withReadPadding(Base64Variant.PaddingReadBehaviour readPadding)
Parameters
readPadding:Base64Variant.PaddingReadBehaviour

Padding read behavior desired

Returns:Base64Variant

Instance with desired padding read behavior setting (this if already has setting; new instance otherwise)

Since
2.12
withWritePaddingback to summary
public Base64Variant withWritePadding(boolean writePadding)
Parameters
writePadding:boolean

Determines if padding is output on write or not

Returns:Base64Variant

Base64Variant which writes padding or not depending on writePadding

Since
2.12
com.fasterxml.jackson.core back to summary

public final Enum Base64Variant.PaddingReadBehaviour

extends Enum<Base64Variant.PaddingReadBehaviour>
Class Inheritance

Defines how the Base64Variant deals with Padding while reading
Since
2.12

Field Summary

Modifier and TypeField and Description
public static final Base64Variant.PaddingReadBehaviour
PADDING_ALLOWED

Padding is allowed but not required in Base64 content being read: no exception thrown based on existence or absence, as long as proper padding characters are used.

public static final Base64Variant.PaddingReadBehaviour
PADDING_FORBIDDEN

Padding is not allowed in Base64 content being read (finding something that looks like padding at the end of content results in an exception)

public static final Base64Variant.PaddingReadBehaviour
PADDING_REQUIRED

Padding is required in Base64 content being read (missing padding for incomplete ending quartet results in an exception)

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Base64Variant.PaddingReadBehaviour
public static Base64Variant.PaddingReadBehaviour[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

PADDING_ALLOWEDback to summary
public static final Base64Variant.PaddingReadBehaviour PADDING_ALLOWED

Padding is allowed but not required in Base64 content being read: no exception thrown based on existence or absence, as long as proper padding characters are used.

PADDING_FORBIDDENback to summary
public static final Base64Variant.PaddingReadBehaviour PADDING_FORBIDDEN

Padding is not allowed in Base64 content being read (finding something that looks like padding at the end of content results in an exception)

PADDING_REQUIREDback to summary
public static final Base64Variant.PaddingReadBehaviour PADDING_REQUIRED

Padding is required in Base64 content being read (missing padding for incomplete ending quartet results in an exception)

Constructor Detail

PaddingReadBehaviourback to summary
private PaddingReadBehaviour()

Method Detail

valueOfback to summary
public static Base64Variant.PaddingReadBehaviour valueOf(String name)
valuesback to summary
public static Base64Variant.PaddingReadBehaviour[] values()