class Base64
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
private static byte[] |
base64ToInt
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 byte[] |
buf
Buffer for streaming.
|
(package private) static byte[] |
CHUNK_SEPARATOR
Chunk separator per RFC 2045 section 2.1.
|
(package private) static int |
CHUNK_SIZE
Chunk size per RFC 2045 section 6.8.
|
private int |
currentLinePos
Variable tracks how many characters have been written to the current line.
|
private int |
decodeSize
Convenience variable to help us determine when our buffer is going to run out of
room and needs resizing.
|
private int |
encodeSize
Convenience variable to help us determine when our buffer is going to run out of
room and needs resizing.
|
private boolean |
eof
Boolean flag to indicate the EOF has been reached.
|
private static byte[] |
intToBase64
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 int |
lineLength
Line length for encoding.
|
private byte[] |
lineSeparator
Line separator for encoding.
|
private static int |
MASK_6BITS
Mask used to extract 6 bits, used when encoding
|
private static int |
MASK_8BITS
Mask used to extract 8 bits, used in decoding base64 bytes
|
private int |
modulus
Writes to the buffer only occur after every 3 reads when encoding, an
every 4 reads when decoding.
|
private static byte |
PAD
Byte used to pad output.
|
private int |
pos
Position where next character should be written in the buffer.
|
private int |
readPos
Position where next character should be read from the buffer.
|
private int |
x
Place holder for the 3 bytes we're dealing with for our base64 logic.
|
Constructor and Description |
---|
Base64()
Default constructor: lineLength is 76, and the lineSeparator is CRLF
when encoding, and all forms can be decoded.
|
Base64(int lineLength)
Consumer can use this constructor to choose a different lineLength
when encoding (lineSeparator is still CRLF).
|
Base64(int lineLength,
byte[] lineSeparator)
Consumer can use this constructor to choose a different lineLength
and lineSeparator when encoding.
|
Modifier and Type | Method and Description |
---|---|
(package private) int |
avail()
Returns the amount of buffered data available for reading.
|
private static boolean |
containsBase64Byte(byte[] arrayOctet) |
byte[] |
decode(byte[] pArray)
Decodes a byte[] containing containing characters in the Base64 alphabet.
|
(package private) void |
decode(byte[] in,
int inPos,
int inAvail)
Decodes all of the provided data, starting at inPos, for inAvail bytes.
|
static byte[] |
decodeBase64(byte[] base64Data)
Decodes Base64 data into octets
|
static java.math.BigInteger |
decodeInteger(byte[] pArray)
Decode a byte64-encoded integer according to crypto
standards such as W3C's XML-Signature
|
(package private) static byte[] |
discardNonBase64(byte[] data)
Discards any characters outside of the base64 alphabet, per the requirements on page 25 of RFC 2045 - "Any
characters outside of the base64 alphabet are to be ignored in base64 encoded data."
|
byte[] |
encode(byte[] pArray)
Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
|
(package private) void |
encode(byte[] in,
int inPos,
int inAvail)
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[] |
encodeBase64Chunked(byte[] binaryData)
Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
|
static byte[] |
encodeInteger(java.math.BigInteger bigInt)
Encode to a byte64-encoded integer according to crypto
standards such as W3C's XML-Signature
|
(package private) boolean |
hasData()
Returns true if this Base64 object has buffered data for reading.
|
static boolean |
isArrayByteBase64(byte[] arrayOctet)
Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
|
static boolean |
isBase64(byte octet)
Returns whether or not the
octet is in the base 64 alphabet. |
private static boolean |
isWhiteSpace(byte byteToCheck)
Check if a byte value is whitespace or not.
|
(package private) int |
readResults(byte[] b,
int bPos,
int bAvail)
Extracts buffered data into the provided byte[] array, starting
at position bPos, up to a maximum of bAvail bytes.
|
private void |
resizeBuf()
Doubles our buffer.
|
(package private) void |
setInitialBuffer(byte[] out,
int outPos,
int outAvail)
Small optimization where we try to buffer directly to the consumer's
output array for one round (if consumer calls this method first!) instead
of starting our own buffer.
|
(package private) static byte[] |
toIntegerBytes(java.math.BigInteger bigInt)
Returns a byte-array representation of a
BigInteger
without sign bit. |
static final int CHUNK_SIZE
The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
static final byte[] CHUNK_SEPARATOR
private static final byte[] intToBase64
private static final byte PAD
private static final byte[] base64ToInt
private static final int MASK_6BITS
private static final int MASK_8BITS
private final int lineLength
private final byte[] lineSeparator
private final int decodeSize
decodeSize = 3 + lineSeparator.length;
private final int encodeSize
encodeSize = 4 + lineSeparator.length;
private byte[] buf
private int pos
private int readPos
private int currentLinePos
private int modulus
private boolean eof
private int x
public Base64()
public Base64(int lineLength)
Consumer can use this constructor to choose a different lineLength when encoding (lineSeparator is still CRLF). All forms of data can be decoded.
Note: lineLengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
lineLength
- each line of encoded data will be at most this long
(rounded up 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)
Consumer can use this constructor to choose a different lineLength and lineSeparator when encoding. All forms of data can be decoded.
Note: lineLengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
lineLength
- Each line of encoded data will be at most this long
(rounded up to nearest multiple of 4). Ignored when decoding.
If <= 0, then output will not be divided into lines (chunks).lineSeparator
- Each line of encoded data will end with this
sequence of bytes.
If lineLength <= 0, then the lineSeparator is not used.java.lang.IllegalArgumentException
- The provided lineSeparator included
some base64 characters. That's not going to work!boolean hasData()
int avail()
private void resizeBuf()
int readResults(byte[] b, int bPos, int bAvail)
b
- byte[] array to extract the buffered data into.bPos
- position in byte[] array to start extraction at.bAvail
- amount of bytes we're allowed to extract. We may extract
fewer (if fewer are available).void setInitialBuffer(byte[] out, int outPos, int outAvail)
out
- byte[] array to buffer directly to.outPos
- Position to start buffering into.outAvail
- Amount of bytes available for direct buffering.void encode(byte[] in, int inPos, int inAvail)
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, so flush last remaining bytes (if not multiple of 3).
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/
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.void decode(byte[] in, int inPos, int inAvail)
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/
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.public 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 isArrayByteBase64(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, otherwiseprivate static boolean containsBase64Byte(byte[] arrayOctet)
public static byte[] encodeBase64(byte[] binaryData)
binaryData
- binary data to encodepublic static byte[] encodeBase64Chunked(byte[] binaryData)
binaryData
- binary data to encodepublic byte[] decode(byte[] pArray)
pArray
- A byte array containing Base64 character datapublic 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 blocksjava.lang.IllegalArgumentException
- Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
public static byte[] decodeBase64(byte[] base64Data)
base64Data
- Byte array containing Base64 dataprivate static boolean isWhiteSpace(byte byteToCheck)
byteToCheck
- the byte to checkstatic byte[] discardNonBase64(byte[] data)
data
- The base-64 encoded data to groompublic byte[] encode(byte[] pArray)
pArray
- a byte array containing binary datapublic static java.math.BigInteger decodeInteger(byte[] pArray)
pArray
- a byte array containing base64 character datapublic static byte[] encodeInteger(java.math.BigInteger bigInt)
bigInt
- a BigIntegerjava.lang.NullPointerException
- if null is passed instatic byte[] toIntegerBytes(java.math.BigInteger bigInt)
BigInteger
without sign bit.bigInt
- BigInteger
to be converted