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")
The URL-safe parameter is only applied to encode operations. Decoding seamlessly handles both modes.
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 thread-safe.
- Since:
- 1.0
- See Also:
-
Field Summary
Fields inherited from class org.apache.tomcat.util.codec.binary.BaseNCodec
lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD_DEFAULT, sm
-
Constructor Summary
ConstructorDescriptionBase64()
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 Summary
Modifier and TypeMethodDescriptionstatic byte[]
decodeBase64
(byte[] base64Data, int off, int len) Deprecated.static byte[]
decodeBase64
(String base64String) Deprecated.Decodes a Base64 String into octets.static byte[]
decodeBase64URLSafe
(String base64String) Deprecated.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked) Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static byte[]
encodeBase64
(byte[] binaryData, boolean isChunked, boolean urlSafe) Deprecated.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) Deprecated.Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.static String
encodeBase64String
(byte[] binaryData) Deprecated.Encodes binary data using the base64 algorithm but does not chunk the output.static String
encodeBase64URLSafeString
(byte[] binaryData) Deprecated.Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.protected boolean
isInAlphabet
(byte octet) Deprecated.Returns whether or not theoctet
is in the Base64 alphabet.static boolean
isInAlphabet
(char c) Deprecated.boolean
Deprecated.Returns our current encode mode.Methods inherited from class org.apache.tomcat.util.codec.binary.BaseNCodec
containsAlphabetOrPad, decode, decode, decode, encode, encode, encodeAsString, ensureBufferSize, getDefaultBufferSize, getEncodedLength
-
Constructor Details
-
Base64
public Base64()Deprecated.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) Deprecated.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) Deprecated.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
-
Base64
public Base64(int lineLength, byte[] lineSeparator) Deprecated.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.- Throws:
IllegalArgumentException
- Thrown when the provided lineSeparator included some base64 characters.- Since:
- 1.4
-
Base64
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) Deprecated.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.urlSafe
- Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode operations. Decoding seamlessly handles both modes. Note: no padding is added when using the URL-safe alphabet.- Throws:
IllegalArgumentException
- Thrown when thelineSeparator
contains Base64 characters.- Since:
- 1.4
-
-
Method Details
-
decodeBase64
public static byte[] decodeBase64(byte[] base64Data, int off, int len) Deprecated. -
decodeBase64
Deprecated.Decodes a Base64 String into octets.Note: this method seamlessly handles data encoded in URL-safe or normal mode.
- Parameters:
base64String
- String containing Base64 data- Returns:
- Array containing decoded data.
- Since:
- 1.4
-
decodeBase64URLSafe
Deprecated. -
encodeBase64
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) Deprecated.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) Deprecated.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. Note: no padding is added when encoding using the URL-safe alphabet.- 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) Deprecated.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. Note: no padding is added when encoding using the URL-safe alphabet.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
-
encodeBase64String
Deprecated.Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behavior 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).
-
encodeBase64URLSafeString
Deprecated.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. Note: no padding is added.- Parameters:
binaryData
- binary data to encode- Returns:
- String containing Base64 characters
- Since:
- 1.4
-
isInAlphabet
public static boolean isInAlphabet(char c) Deprecated. -
isInAlphabet
protected boolean isInAlphabet(byte octet) Deprecated.Returns whether or not theoctet
is in the Base64 alphabet.- Specified by:
isInAlphabet
in classBaseNCodec
- Parameters:
octet
- The value to test- Returns:
true
if the value is defined in the Base64 alphabetfalse
otherwise.
-
isUrlSafe
public boolean isUrlSafe()Deprecated.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
-