Class Base64
This class implements section 6.8. Base64 Content-Transfer-Encoding from RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies by Freed and Borenstein.
The class can be parameterized in the following manner with various constructors:
- URL-safe mode: Default off.
- Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
- Line separator: Default is CRLF ("\r\n")
Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).
- Since:
- 2.2
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionBase64()Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64(boolean urlSafe) Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.Base64(int lineLength) Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64(int lineLength, byte[] lineSeparator) Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
- 
Method SummaryModifier and TypeMethodDescriptionbyte[]decode(byte[] source) Deprecated.Decodes a byte array containing characters in the Base64 alphabet.byte[]Deprecated.Decodes a String containing characters in the Base64 alphabet.static byte[]decodeBase64(byte[] base64) Deprecated.Decodes Base64 data into octets.static byte[]decodeBase64(String base64) Deprecated.Decodes a Base64 String into octets.static BigIntegerdecodeInteger(byte[] source) Deprecated.Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signaturebyte[]encode(byte[] source) Deprecated.Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.static byte[]encodeBase64(byte[] source) Deprecated.Encodes binary data using the base64 algorithm but does not chunk the output.static byte[]encodeBase64(byte[] binaryData, boolean chunked) Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]encodeBase64(byte[] binaryData, boolean chunked, boolean urlSafe) Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]encodeBase64(byte[] binaryData, boolean chunked, boolean urlSafe, int maxResultSize) Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]encodeBase64Chunked(byte[] binaryData) Deprecated.Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks separated by CR-LF.static StringencodeBase64String(byte[] binaryData) Deprecated.Encodes binary data using the base64 algorithm into 76 character blocks separated by CR-LF.static StringencodeBase64String(byte[] binaryData, boolean chunked) Deprecated.Encodes binary data using the base64 algorithm.static StringencodeBase64StringUnChunked(byte[] binaryData) Deprecated.Encodes binary data using the base64 algorithm, without using chunking.static byte[]encodeBase64URLSafe(byte[] binaryData) Deprecated.Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.static StringencodeBase64URLSafeString(byte[] binaryData) Deprecated.Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.static byte[]encodeInteger(BigInteger bigInt) Deprecated.Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-SignatureencodeToString(byte[] source) Deprecated.Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet.static booleanisArrayByteBase64(byte[] arrayOctet) Deprecated.Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.static booleanisBase64(byte octet) Deprecated.Returns whether or not theoctetis in the base 64 alphabet.booleanDeprecated.Tests whether our current encoding mode.
- 
Constructor Details- 
Base64public Base64()Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. When decoding all variants are supported. 
- 
Base64Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. When decoding all variants are supported. - Parameters:
- urlSafe- if- true, URL-safe encoding is used. In most cases this should be set to- false.
- Since:
- 1.4
 
- 
Base64Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. When decoding all variants are supported. - Parameters:
- lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). If- lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
- Since:
- 1.4
 
- 
Base64Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE. Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. When decoding all variants are supported. - Parameters:
- lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). If- lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
- lineSeparator- Each line of encoded data will end with this sequence of bytes. Not used for decoding.
- Throws:
- IllegalArgumentException- Thrown when the provided lineSeparator included some base64 characters.
- Since:
- 1.4
 
- 
Base64Deprecated.Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE. Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. When decoding all variants are supported. - Parameters:
- lineLength- Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). If- lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
- lineSeparator- Each line of encoded data will end with this sequence of bytes. Not used for decoding.
- urlSafe- Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode operations. Decoding seamlessly handles both modes.
- Throws:
- IllegalArgumentException- The provided lineSeparator included some base64 characters. That's not going to work!
- Since:
- 1.4
 
 
- 
- 
Method Details- 
decodeBase64Deprecated.Decodes Base64 data into octets.- Parameters:
- base64- Byte array containing Base64 data
- Returns:
- Array containing decoded data.
 
- 
decodeBase64Deprecated.Decodes a Base64 String into octets.- Parameters:
- base64- String containing Base64 data
- Returns:
- Array containing decoded data.
- Since:
- 1.4
 
- 
decodeIntegerDeprecated.Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature- Parameters:
- source- a byte array containing base64 character data
- Returns:
- A BigInteger
- Since:
- 1.4
 
- 
encodeBase64Deprecated.Encodes binary data using the base64 algorithm but does not chunk the output.- Parameters:
- source- binary data to encode
- Returns:
- byte[] containing Base64 characters in their UTF-8 representation.
 
- 
encodeBase64Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
- binaryData- Array containing binary data to encode.
- chunked- if- truethis encoder will chunk the base64 output into 76 character blocks
- Returns:
- Base64-encoded data.
- Throws:
- IllegalArgumentException- Thrown when the input array needs an output array bigger than- Integer.MAX_VALUE
 
- 
encodeBase64Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
- binaryData- Array containing binary data to encode.
- chunked- if- truethis encoder will chunk the base64 output into 76 character blocks
- urlSafe- if- truethis encoder will emit - and _ instead of the usual + and / characters.
- Returns:
- Base64-encoded data.
- Throws:
- IllegalArgumentException- Thrown when the input array needs an output array bigger than- Integer.MAX_VALUE
- Since:
- 1.4
 
- 
encodeBase64public static byte[] encodeBase64(byte[] binaryData, boolean chunked, boolean urlSafe, int maxResultSize) Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
- binaryData- Array containing binary data to encode.
- chunked- if- truethis encoder will chunk the base64 output into 76 character blocks
- urlSafe- if- truethis encoder will emit - and _ instead of the usual + and / characters.
- maxResultSize- The maximum result size to accept.
- Returns:
- Base64-encoded data.
- Throws:
- IllegalArgumentException- Thrown when the input array needs an output array bigger than maxResultSize
- Since:
- 1.4
 
- 
encodeBase64ChunkedDeprecated.Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks separated by CR-LF.The return value ends in a CR-LF. - Parameters:
- binaryData- binary data to encode
- Returns:
- Base64 characters chunked in 76 character blocks
- Throws:
- ArithmeticException- if the- binaryDatawould overflows a byte[].
 
- 
encodeBase64StringDeprecated.Encodes binary data using the base64 algorithm into 76 character blocks separated by CR-LF.The return value ends in a CR-LF. For a non-chunking version, see encodeBase64StringUnChunked(byte[]).- Parameters:
- binaryData- binary data to encode
- Returns:
- String containing Base64 characters.
- Since:
- 1.4
 
- 
encodeBase64StringDeprecated.Encodes binary data using the base64 algorithm.- Parameters:
- binaryData- binary data to encode
- chunked- whether to split the output into chunks
- Returns:
- String containing Base64 characters.
- Since:
- 3.2
 
- 
encodeBase64StringUnChunkedDeprecated.Encodes binary data using the base64 algorithm, without using chunking.For a chunking version, see encodeBase64String(byte[]).- Parameters:
- binaryData- binary data to encode
- Returns:
- String containing Base64 characters.
- Since:
- 3.2
 
- 
encodeBase64URLSafeDeprecated.Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters.- Parameters:
- binaryData- binary data to encode
- Returns:
- byte[] containing Base64 characters in their UTF-8 representation.
- Since:
- 1.4
 
- 
encodeBase64URLSafeStringDeprecated.Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters.- Parameters:
- binaryData- binary data to encode
- Returns:
- String containing Base64 characters
- Since:
- 1.4
 
- 
encodeIntegerDeprecated.Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature- Parameters:
- bigInt- a BigInteger
- Returns:
- A byte array containing base64 character data
- Throws:
- NullPointerException- if null is passed in
- Since:
- 1.4
 
- 
isArrayByteBase64Deprecated.Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently, the method treats whitespace as valid.- Parameters:
- arrayOctet- byte array to test
- Returns:
- trueif all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise
 
- 
isBase64Deprecated.Returns whether or not theoctetis in the base 64 alphabet.- Parameters:
- octet- The value to test
- Returns:
- trueif the value is defined in the base 64 alphabet,- falseotherwise.
- Since:
- 1.4
 
- 
decodeDeprecated.Decodes a byte array containing characters in the Base64 alphabet.- Parameters:
- source- A byte array containing Base64 character data
- Returns:
- a byte array containing binary data; will return nullif provided byte array isnull.
 
- 
decodeDeprecated.Decodes a String containing characters in the Base64 alphabet.- Parameters:
- source- A String containing Base64 character data, must not be- null
- Returns:
- a byte array containing binary data
- Since:
- 1.4
 
- 
encodeDeprecated.Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.- Parameters:
- source- a byte array containing binary data
- Returns:
- A byte array containing only Base64 character data
 
- 
encodeToStringDeprecated.Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet.- Parameters:
- source- a byte array containing binary data
- Returns:
- A String containing only Base64 character data
- Since:
- 1.4
 
- 
isUrlSafeDeprecated.Tests whether our current encoding mode. True if we're URL-SAFE, false otherwise.- Returns:
- true if we're in URL-SAFE mode, false otherwise.
- Since:
- 1.4
 
 
- 
Base64.