Class Base64
- 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).
This class is not thread-safe. Each thread should use its own instance.
- Since:
- 1.0
- Author:
- Apache Software Foundation
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
MIME chunk size per RFC 2045 section 6.8.static final int
PEM chunk size per RFC 1421 section 4.3.2.4. -
Constructor Summary
ConstructorsConstructorDescriptionBase64()
Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64
(boolean urlSafe) Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.Base64
(int lineLength) Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64
(int lineLength, byte[] lineSeparator) Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.Base64
(int lineLength, byte[] lineSeparator, boolean urlSafe) Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
decode
(byte[] pArray) Decodes a byte[] containing characters in the Base-N alphabet.Decodes an Object using the Base-N algorithm.byte[]
Decodes a String containing characters in the Base-N alphabet.static byte[]
decodeBase64
(byte[] base64Data) Decodes Base64 data into octetsstatic byte[]
decodeBase64
(String base64String) Decodes a Base64 String into octetsstatic BigInteger
decodeInteger
(byte[] pArray) Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signaturebyte[]
encode
(byte[] pArray) Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.Encodes an Object using the Base-N algorithm.encodeAsString
(byte[] pArray) Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.static byte[]
encodeBase64
(byte[] binaryData) Encodes binary data using the base64 algorithm but does not chunk the output.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked, boolean urlSafe) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]
encodeBase64Chunked
(byte[] binaryData) Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocksstatic String
encodeBase64String
(byte[] binaryData) Encodes binary data using the base64 algorithm but does not chunk the output.static byte[]
encodeBase64URLSafe
(byte[] binaryData) Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.static String
encodeBase64URLSafeString
(byte[] binaryData) Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.static byte[]
encodeInteger
(BigInteger bigInt) Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-SignatureencodeToString
(byte[] pArray) Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.long
getEncodedLength
(byte[] pArray) Calculates the amount of space needed to encode the supplied array.static boolean
isBase64
(byte octet) Returns whether theoctet
is in the base 64 alphabet.static boolean
isBase64
(byte[] arrayOctet) Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.static boolean
Tests a given String to see if it contains only valid characters within the Base64 alphabet.boolean
isInAlphabet
(byte[] arrayOctet, boolean allowWSPad) Tests a given byte array to see if it contains only valid characters within the alphabet.boolean
isInAlphabet
(String basen) Tests a given String to see if it contains only valid characters within the alphabet.boolean
Returns our current encode mode.
-
Field Details
-
MIME_CHUNK_SIZE
public static final int MIME_CHUNK_SIZEMIME chunk size per RFC 2045 section 6.8.The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
- See Also:
-
PEM_CHUNK_SIZE
public static final int PEM_CHUNK_SIZEPEM chunk size per RFC 1421 section 4.3.2.4.The 64 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
-
-
Constructor Details
-
Base64
public Base64()Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.When encoding the line length is 0 (no chunking), and the encoding table is STANDARD_ENCODE_TABLE.
When decoding all variants are supported.
-
Base64
public Base64(boolean urlSafe) 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
- iftrue
, URL-safe encoding is used. In most cases this should be set tofalse
.- Since:
- 1.4
-
Base64
public Base64(int lineLength) 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 nearest multiple of 4). If lineLength less than or equal to 0, then the output will not be divided into lines (chunks). Ignored when decoding.- Since:
- 1.4
-
Base64
public Base64(int lineLength, byte[] lineSeparator) 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 nearest multiple of 4). If lineLength is less than or equal to 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.- Throws:
IllegalArgumentException
- Thrown when the provided lineSeparator included some base64 characters.- Since:
- 1.4
-
Base64
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) 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 nearest multiple of 4). If lineLength less than or equal to 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.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
-
encode
Encodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[].- Parameters:
pObject
- Object to encode- Returns:
- An object (of type byte[]) containing the Base-N encoded data which corresponds to the byte[] supplied.
- Throws:
IllegalArgumentException
- if the parameter supplied is not of type byte[]
-
encodeToString
Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.- Parameters:
pArray
- a byte array containing binary data- Returns:
- A String containing only Base-N character data
-
decode
Decodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String.- Parameters:
pObject
- Object to decode- Returns:
- An object (of type byte[]) containing the binary data which corresponds to the byte[] or String supplied.
- Throws:
IllegalArgumentException
- if the parameter supplied is not of type byte[]
-
decode
Decodes a String containing characters in the Base-N alphabet.- Parameters:
pArray
- A String containing Base-N character data- Returns:
- a byte array containing binary data
-
decode
public byte[] decode(byte[] pArray) Decodes a byte[] containing characters in the Base-N alphabet.- Parameters:
pArray
- A byte array containing Base-N character data- Returns:
- a byte array containing binary data
-
encode
public byte[] encode(byte[] pArray) Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.- Parameters:
pArray
- a byte array containing binary data- Returns:
- A byte array containing only the basen alphabetic character data
-
encodeAsString
Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet. Uses UTF8 encoding.- Parameters:
pArray
- a byte array containing binary data- Returns:
- String containing only character data in the appropriate alphabet.
-
isInAlphabet
public boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad) Tests a given byte array to see if it contains only valid characters within the alphabet. The method optionally treats whitespace and pad as valid.- Parameters:
arrayOctet
- byte array to testallowWSPad
- iftrue
, then whitespace and PAD are also allowed- Returns:
true
if all bytes are valid characters in the alphabet or if the byte array is empty;false
, otherwise
-
isInAlphabet
Tests a given String to see if it contains only valid characters within the alphabet. The method treats whitespace and PAD as valid.- Parameters:
basen
- String to test- Returns:
true
if all characters in the String are valid characters in the alphabet or if the String is empty;false
, otherwise- See Also:
-
getEncodedLength
public long getEncodedLength(byte[] pArray) Calculates the amount of space needed to encode the supplied array.- Parameters:
pArray
- byte[] array which will later be encoded- Returns:
- amount of space needed to encoded the supplied array. Returns a long since a max-len array will require greater than Integer.MAX_VALUE
-
isUrlSafe
public boolean isUrlSafe()Returns our current encode mode. True if we're URL-SAFE, false otherwise.- Returns:
- true if we're in URL-SAFE mode, false otherwise.
- Since:
- 1.4
-
isBase64
public static boolean isBase64(byte octet) Returns whether theoctet
is in the base 64 alphabet.- Parameters:
octet
- The value to test- Returns:
true
if the value is defined in the the base 64 alphabet,false
otherwise.- Since:
- 1.4
-
isBase64
Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.- Parameters:
base64
- String to test- Returns:
true
if all characters in the String are valid characters in the Base64 alphabet or if the String is empty;false
, otherwise- Since:
- 1.5
-
isBase64
public static boolean isBase64(byte[] arrayOctet) 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:
true
if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;false
, otherwise- Since:
- 1.5
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData) Encodes binary data using the base64 algorithm but does not chunk the output.- Parameters:
binaryData
- binary data to encode- Returns:
- byte[] containing Base64 characters in their UTF-8 representation.
-
encodeBase64String
Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to single-line non-chunking (commons-codec-1.5).- Parameters:
binaryData
- binary data to encode- Returns:
- String containing Base64 characters.
- Since:
- 1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not).
-
encodeBase64URLSafe
public static byte[] encodeBase64URLSafe(byte[] binaryData) 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
-
encodeBase64URLSafeString
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
-
encodeBase64Chunked
public static byte[] encodeBase64Chunked(byte[] binaryData) Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks- Parameters:
binaryData
- binary data to encode- Returns:
- Base64 characters chunked in 76 character blocks
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
binaryData
- Array containing binary data to encode.isChunked
- iftrue
this 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 thanInteger.MAX_VALUE
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
binaryData
- Array containing binary data to encode.isChunked
- iftrue
this encoder will chunk the base64 output into 76 character blocksurlSafe
- iftrue
this 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 thanInteger.MAX_VALUE
- Since:
- 1.4
-
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize) Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.- Parameters:
binaryData
- Array containing binary data to encode.isChunked
- iftrue
this encoder will chunk the base64 output into 76 character blocksurlSafe
- iftrue
this 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
-
decodeBase64
Decodes a Base64 String into octets- Parameters:
base64String
- String containing Base64 data- Returns:
- Array containing decoded data.
- Since:
- 1.4
-
decodeBase64
public static byte[] decodeBase64(byte[] base64Data) Decodes Base64 data into octets- Parameters:
base64Data
- Byte array containing Base64 data- Returns:
- Array containing decoded data.
-
decodeInteger
Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature- Parameters:
pArray
- a byte array containing base64 character data- Returns:
- A BigInteger
- Since:
- 1.4
-
encodeInteger
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
-