public class Base64 extends BaseNCodec
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:
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.
BaseNCodec.Context
Modifier and Type | Field and Description |
---|---|
private static int |
BITS_PER_ENCODED_BYTE
BASE32 characters are 6 bits in length.
|
private static int |
BYTES_PER_ENCODED_BLOCK |
private static int |
BYTES_PER_UNENCODED_BLOCK |
(package private) static byte[] |
CHUNK_SEPARATOR
Chunk separator per RFC 2045 section 2.1.
|
private static byte[] |
DECODE_TABLE
This array is a lookup table that translates Unicode characters drawn from the "Base64 Alphabet" (as specified
in Table 1 of RFC 2045) into their 6-bit positive integer equivalents.
|
private int |
decodeSize
Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
|
private byte[] |
decodeTable |
private int |
encodeSize
Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
|
private byte[] |
encodeTable
Encode table to use: either STANDARD or URL_SAFE.
|
private byte[] |
lineSeparator
Line separator for encoding.
|
private static int |
MASK_6BITS
Mask used to extract 6 bits, used when encoding
|
private static byte[] |
STANDARD_ENCODE_TABLE
This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet"
equivalents as specified in Table 1 of RFC 2045.
|
private static byte[] |
URL_SAFE_ENCODE_TABLE
This is a copy of the STANDARD_ENCODE_TABLE above, but with + and /
changed to - and _ to make the encoded Base64 results more URL-SAFE.
|
EOF, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE
Constructor and Description |
---|
Base64()
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.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
decode(byte[] in,
int inPos,
int inAvail,
BaseNCodec.Context context)
Decodes all of the provided data, starting at inPos, for inAvail bytes.
|
static byte[] |
decodeBase64(byte[] base64Data)
Decodes Base64 data into octets
|
static byte[] |
decodeBase64(String base64String)
Decodes a Base64 String into octets
|
static BigInteger |
decodeInteger(byte[] pArray)
Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
|
(package private) void |
encode(byte[] in,
int inPos,
int inAvail,
BaseNCodec.Context context)
Encodes all of the provided data, starting at inPos, for inAvail bytes.
|
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 blocks
|
static 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-Signature
|
static boolean |
isArrayByteBase64(byte[] arrayOctet)
Deprecated.
1.5 Use
isBase64(byte[]) , will be removed in 2.0. |
static boolean |
isBase64(byte octet)
Returns whether or not the
octet 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 |
isBase64(String base64)
Tests a given String to see if it contains only valid characters within the Base64 alphabet.
|
protected boolean |
isInAlphabet(byte octet)
Returns whether or not the
octet is in the Base64 alphabet. |
boolean |
isUrlSafe()
Returns our current encode mode.
|
(package private) static byte[] |
toIntegerBytes(BigInteger bigInt)
Returns a byte-array representation of a
BigInteger without sign bit. |
available, containsAlphabetOrPad, decode, decode, decode, encode, encode, encodeAsString, encodeToString, ensureBufferSize, getDefaultBufferSize, getEncodedLength, hasData, isInAlphabet, isInAlphabet, isWhiteSpace, readResults
private static final int BITS_PER_ENCODED_BYTE
private static final int BYTES_PER_UNENCODED_BLOCK
private static final int BYTES_PER_ENCODED_BLOCK
static final byte[] CHUNK_SEPARATOR
N.B. The next major release may break compatibility and make this field private.
private static final byte[] STANDARD_ENCODE_TABLE
private static final byte[] URL_SAFE_ENCODE_TABLE
private static final byte[] DECODE_TABLE
private static final int MASK_6BITS
private final byte[] encodeTable
private final byte[] decodeTable
private final byte[] lineSeparator
private final int decodeSize
decodeSize = 3 + lineSeparator.length;
private final int encodeSize
encodeSize = 4 + lineSeparator.length;
public Base64()
When encoding the line length is 0 (no chunking), and the encoding table is STANDARD_ENCODE_TABLE.
When decoding all variants are supported.
public Base64(boolean urlSafe)
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.
urlSafe
- if true
, URL-safe encoding is used. In most cases this should be set to false
.public Base64(int lineLength)
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.
lineLength
- Each line of encoded data will be at most of the given length (rounded down to nearest multiple of
4). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when
decoding.public Base64(int lineLength, byte[] lineSeparator)
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.
lineLength
- Each line of encoded data will be at most of the given length (rounded down to 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.IllegalArgumentException
- Thrown when the provided lineSeparator included some base64 characters.public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe)
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.
lineLength
- Each line of encoded data will be at most of the given length (rounded down to 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.IllegalArgumentException
- The provided lineSeparator included some base64 characters. That's not going to work!public boolean isUrlSafe()
void encode(byte[] in, int inPos, int inAvail, BaseNCodec.Context context)
Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, to flush last remaining bytes (if not multiple of 3).
Note: no padding is added when encoding using the URL-safe alphabet.
Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
encode
in class BaseNCodec
in
- byte[] array of binary data to base64 encode.inPos
- Position to start reading data from.inAvail
- Amount of bytes available from input for encoding.context
- the context to be usedvoid decode(byte[] in, int inPos, int inAvail, BaseNCodec.Context context)
Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1" call is not necessary when decoding, but it doesn't hurt, either.
Ignores all non-base64 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in, garbage-out philosophy: it will not check the provided data for validity.
Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
decode
in class BaseNCodec
in
- byte[] array of ascii data to base64 decode.inPos
- Position to start reading data from.inAvail
- Amount of bytes available from input for encoding.context
- the context to be used@Deprecated public static boolean isArrayByteBase64(byte[] arrayOctet)
isBase64(byte[])
, will be removed in 2.0.arrayOctet
- byte array to testtrue
if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;
false
, otherwisepublic static boolean isBase64(byte octet)
octet
is in the base 64 alphabet.octet
- The value to testtrue
if the value is defined in the the base 64 alphabet, false
otherwise.public static boolean isBase64(String base64)
base64
- String to testtrue
if all characters in the String are valid characters in the Base64 alphabet or if
the String is empty; false
, otherwisepublic static boolean isBase64(byte[] arrayOctet)
arrayOctet
- byte array to testtrue
if all bytes are valid characters in the Base64 alphabet or if the byte array is empty;
false
, otherwisepublic static byte[] encodeBase64(byte[] binaryData)
binaryData
- binary data to encodepublic static String encodeBase64String(byte[] binaryData)
binaryData
- binary data to encodepublic static byte[] encodeBase64URLSafe(byte[] binaryData)
binaryData
- binary data to encodepublic static String encodeBase64URLSafeString(byte[] binaryData)
binaryData
- binary data to encodepublic static byte[] encodeBase64Chunked(byte[] binaryData)
binaryData
- binary data to encodepublic static byte[] encodeBase64(byte[] binaryData, boolean isChunked)
binaryData
- Array containing binary data to encode.isChunked
- if true
this encoder will chunk the base64 output into 76 character blocksIllegalArgumentException
- Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe)
binaryData
- Array containing binary data to encode.isChunked
- if true
this encoder will chunk the base64 output into 76 character blocksurlSafe
- if true
this encoder will emit - and _ instead of the usual + and / characters.
Note: no padding is added when encoding using the URL-safe alphabet.IllegalArgumentException
- Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)
binaryData
- Array containing binary data to encode.isChunked
- if true
this encoder will chunk the base64 output into 76 character blocksurlSafe
- if true
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.IllegalArgumentException
- Thrown when the input array needs an output array bigger than maxResultSizepublic static byte[] decodeBase64(String base64String)
base64String
- String containing Base64 datapublic static byte[] decodeBase64(byte[] base64Data)
base64Data
- Byte array containing Base64 datapublic static BigInteger decodeInteger(byte[] pArray)
pArray
- a byte array containing base64 character datapublic static byte[] encodeInteger(BigInteger bigInt)
bigInt
- a BigIntegerNullPointerException
- if null is passed instatic byte[] toIntegerBytes(BigInteger bigInt)
BigInteger
without sign bit.bigInt
- BigInteger
to be convertedprotected boolean isInAlphabet(byte octet)
octet
is in the Base64 alphabet.isInAlphabet
in class BaseNCodec
octet
- The value to testtrue
if the value is defined in the the Base64 alphabet false
otherwise.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses