public class Base64 extends 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.
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.
Modifier and Type | Class and Description |
---|---|
class |
Base64.CharEncoding
Character encoding names required of every implementation of the Java
platform.
|
static class |
Base64.Charsets
Charsets required of every implementation of the Java platform.
|
static class |
Base64.StringUtils
Converts String to and from bytes using the encodings required by the
Java specification.
|
Modifier and Type | Field and Description |
---|---|
protected int |
lineLength
Chunksize for encoding.
|
protected static int |
MASK_8BITS
Mask used to extract 8 bits, used in decoding bytes
|
static int |
MIME_CHUNK_SIZE
MIME chunk size per RFC 2045 section 6.8.
|
protected byte |
PAD |
protected static byte |
PAD_DEFAULT
Byte used to pad output.
|
static int |
PEM_CHUNK_SIZE
PEM chunk size per RFC 1421 section 4.3.2.4.
|
Modifier | 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.
|
protected |
Base64(int unencodedBlockSize,
int encodedBlockSize,
int lineLength,
int chunkSeparatorLength)
Note
lineLength is rounded down to the nearest multiple of
encodedBlockSize If chunkSeparatorLength is zero,
then chunking is disabled. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
containsAlphabetOrPad(byte[] arrayOctet)
Tests a given byte array to see if it contains any characters within the
alphabet or PAD.
|
byte[] |
decode(byte[] pArray)
Decodes a byte[] containing characters in the Base-N alphabet.
|
Object |
decode(Object obj)
Decodes an Object using the Base-N algorithm.
|
byte[] |
decode(String pArray)
Decodes a String containing characters in the Base-N alphabet.
|
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
|
byte[] |
encode(byte[] pArray)
Encodes a byte[] containing binary data, into a byte[] containing
characters in the alphabet.
|
Object |
encode(Object obj)
Encodes an Object using the Base-N algorithm.
|
String |
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 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
|
String |
encodeToString(byte[] pArray)
Encodes a byte[] containing binary data, into a String containing
characters in the Base-N alphabet.
|
protected void |
ensureBufferSize(int size,
ca.uhn.hl7v2.hoh.util.repackage.Base64.Context context)
Ensure that the buffer has room for
size bytes |
protected int |
getDefaultBufferSize()
Get the default buffer size.
|
long |
getEncodedLength(byte[] pArray)
Calculates the amount of space needed to encode the supplied array.
|
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 Base32 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 |
isUrlSafe()
Returns our current encode mode.
|
protected static boolean |
isWhiteSpace(byte byteToCheck)
Checks if a byte value is whitespace or not.
|
static void |
main(String[] args) |
public static final int MIME_CHUNK_SIZE
The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
public static final int PEM_CHUNK_SIZE
The 64 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
protected static final int MASK_8BITS
protected static final byte PAD_DEFAULT
protected final byte PAD
protected int lineLength
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.IllegalArgumentException
- The provided lineSeparator included some base64 characters.
That's not going to work!protected Base64(int unencodedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength)
lineLength
is rounded down to the nearest multiple of
encodedBlockSize
If chunkSeparatorLength
is zero,
then chunking is disabled.unencodedBlockSize
- the size of an unencoded block (e.g. Base64 = 3)encodedBlockSize
- the size of an encoded block (e.g. Base64 = 4)lineLength
- if > 0, use chunking with a length lineLength
chunkSeparatorLength
- the chunk separator length, if relevantpublic boolean isUrlSafe()
@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.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.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 inprotected boolean isInAlphabet(byte octet)
octet
is in the Base32 alphabet.octet
- The value to testtrue
if the value is defined in the the Base32 alphabet
false
otherwise.protected int getDefaultBufferSize()
DEFAULT_BUFFER_SIZE
protected void ensureBufferSize(int size, ca.uhn.hl7v2.hoh.util.repackage.Base64.Context context)
size
bytessize
- minimum spare space requiredcontext
- the context to be usedprotected static boolean isWhiteSpace(byte byteToCheck)
byteToCheck
- the byte to checkpublic Object encode(Object obj) throws Exception
obj
- Object to encodeEncoderException
- if the parameter supplied is not of type byte[]Exception
public String encodeToString(byte[] pArray)
pArray
- a byte array containing binary datapublic String encodeAsString(byte[] pArray)
pArray
- a byte array containing binary datapublic Object decode(Object obj) throws Exception
obj
- Object to decodeDecoderException
- if the parameter supplied is not of type byte[]Exception
public byte[] decode(String pArray)
pArray
- A String containing Base-N character datapublic byte[] decode(byte[] pArray)
pArray
- A byte array containing Base-N character datapublic byte[] encode(byte[] pArray)
pArray
- a byte array containing binary datapublic boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad)
arrayOctet
- byte array to testallowWSPad
- if true
, then whitespace and PAD are also allowedtrue
if all bytes are valid characters in the alphabet or
if the byte array is empty; false
, otherwisepublic boolean isInAlphabet(String basen)
basen
- String to testtrue
if all characters in the String are valid characters
in the alphabet or if the String is empty; false
,
otherwiseisInAlphabet(byte[], boolean)
protected boolean containsAlphabetOrPad(byte[] arrayOctet)
arrayOctet
- byte array to testtrue
if any byte is a valid character in the alphabet or
PAD; false
otherwisepublic long getEncodedLength(byte[] pArray)
pArray
- byte[] array which will later be encodedCopyright © 2012–2017 University Health Network. All rights reserved.