public class

Base64

extends Object
implements BinaryDecoder BinaryEncoder
java.lang.Object
   ↳ org.odata4j.repack.org.apache.commons.codec.binary.Base64

Class Overview

Provides Base64 encoding and decoding as defined by RFC 2045.

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")

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).

See Also

Summary

Constants
int CHUNK_SIZE Chunk size per RFC 2045 section 6.8.
int DEFAULT_BUFFER_RESIZE_FACTOR
int DEFAULT_BUFFER_SIZE
int MASK_6BITS Mask used to extract 6 bits, used when encoding
int MASK_8BITS Mask used to extract 8 bits, used in decoding base64 bytes
byte PAD Byte used to pad output.
Fields
static final byte[] CHUNK_SEPARATOR Chunk separator per RFC 2045 section 2.1.
private static final 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 static final 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 final 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.
private byte[] buffer Buffer for streaming.
private int currentLinePos Variable tracks how many characters have been written to the current line.
private final int decodeSize Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
private final int encodeSize Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
private final byte[] encodeTable Encode table to use: either STANDARD or URL_SAFE.
private boolean eof Boolean flag to indicate the EOF has been reached.
private final int lineLength Line length for encoding.
private final byte[] lineSeparator Line separator for encoding.
private int modulus Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when decoding.
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.
Public Constructors
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.
Public Methods
byte[] decode(byte[] pArray)
Decodes a byte[] containing containing characters in the Base64 alphabet.
Object decode(Object pObject)
Decodes an Object using the base64 algorithm.
byte[] decode(String pArray)
Decodes a String containing containing characters in the Base64 alphabet.
static byte[] decodeBase64(String base64String)
Decodes a Base64 String into octets
static byte[] decodeBase64(byte[] base64Data)
Decodes Base64 data into octets
static BigInteger decodeInteger(byte[] pArray)
Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
Object encode(Object pObject)
Encodes an Object using the base64 algorithm.
byte[] encode(byte[] pArray)
Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 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, int maxResultSize)
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[] 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 into 76 character blocks separated by CRLF.
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 Base64 alphabet.
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.
boolean isUrlSafe()
Returns our current encode mode.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.odata4j.repack.org.apache.commons.codec.BinaryDecoder
From interface org.odata4j.repack.org.apache.commons.codec.BinaryEncoder
From interface org.odata4j.repack.org.apache.commons.codec.Decoder
From interface org.odata4j.repack.org.apache.commons.codec.Encoder

Constants

static final int CHUNK_SIZE

Chunk size per RFC 2045 section 6.8.

The {@value } character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.

Constant Value: 76 (0x0000004c)

private static final int DEFAULT_BUFFER_RESIZE_FACTOR

Constant Value: 2 (0x00000002)

private static final int DEFAULT_BUFFER_SIZE

Constant Value: 8192 (0x00002000)

private static final int MASK_6BITS

Mask used to extract 6 bits, used when encoding

Constant Value: 63 (0x0000003f)

private static final int MASK_8BITS

Mask used to extract 8 bits, used in decoding base64 bytes

Constant Value: 255 (0x000000ff)

private static final byte PAD

Byte used to pad output.

Constant Value: 61 (0x0000003d)

Fields

static final byte[] CHUNK_SEPARATOR

Chunk separator per RFC 2045 section 2.1.

N.B. The next major release may break compatibility and make this field private.

private static final 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. Characters that are not in the Base64 alphabet but fall within the bounds of the array are translated to -1. Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder seamlessly handles both URL_SAFE and STANDARD base64. (The encoder, on the other hand, needs to know ahead of time what to emit). Thanks to "commons" project in ws.apache.org for this code. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/

private static final 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. Thanks to "commons" project in ws.apache.org for this code. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/

private static final 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. This table is only used when the Base64's mode is set to URL-SAFE.

private byte[] buffer

Buffer for streaming.

private int currentLinePos

Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to make sure each encoded line never goes beyond lineLength (if lineLength > 0).

private final int decodeSize

Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. decodeSize = 3 + lineSeparator.length;

private final int encodeSize

Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. encodeSize = 4 + lineSeparator.length;

private final byte[] encodeTable

Encode table to use: either STANDARD or URL_SAFE. Note: the DECODE_TABLE above remains static because it is able to decode both STANDARD and URL_SAFE streams, but the encodeTable must be a member variable so we can switch between the two modes.

private boolean eof

Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this Base64 object becomes useless, and must be thrown away.

private final int lineLength

Line length for encoding. Not used when decoding. A value of zero or less implies no chunking of the base64 encoded data.

private final byte[] lineSeparator

Line separator for encoding. Not used when decoding. Only used if lineLength > 0.

private int modulus

Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when decoding. This variable helps track that.

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. Bitwise operations store and extract the base64 encoding or decoding from this variable.

Public Constructors

public Base64 ()

Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe 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.

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 If true, URL-safe encoding is used. In most cases this should be set to false.

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 <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.

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 <= 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.

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 <= 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!

Public Methods

public byte[] decode (byte[] pArray)

Decodes a byte[] containing containing characters in the Base64 alphabet.

Parameters
pArray A byte array containing Base64 character data
Returns
  • a byte array containing binary data

public Object decode (Object pObject)

Decodes an Object using the base64 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
DecoderException if the parameter supplied is not of type byte[]

public byte[] decode (String pArray)

Decodes a String containing containing characters in the Base64 alphabet.

Parameters
pArray A String containing Base64 character data
Returns
  • a byte array containing binary data

public static byte[] decodeBase64 (String base64String)

Decodes a Base64 String into octets

Parameters
base64String String containing Base64 data
Returns
  • Array containing decoded data.

public static byte[] decodeBase64 (byte[] base64Data)

Decodes Base64 data into octets

Parameters
base64Data Byte array containing Base64 data
Returns
  • Array containing decoded data.

public static BigInteger decodeInteger (byte[] pArray)

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

public Object encode (Object pObject)

Encodes an Object using the base64 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 base64 encoded data which corresponds to the byte[] supplied.
Throws
EncoderException if the parameter supplied is not of type byte[]

public byte[] encode (byte[] pArray)

Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.

Parameters
pArray A byte array containing binary data
Returns
  • A byte array containing only Base64 character data

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.

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 If true 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 than MAX_VALUE

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 If true this encoder will chunk the base64 output into 76 character blocks
urlSafe If true 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

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 If true this encoder will chunk the base64 output into 76 character blocks
urlSafe If true 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 than MAX_VALUE

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

public static String encodeBase64String (byte[] binaryData)

Encodes binary data using the base64 algorithm into 76 character blocks separated by CRLF.

Parameters
binaryData Binary data to encode
Returns
  • String containing Base64 characters.

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.

public static String encodeBase64URLSafeString (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
  • String containing Base64 characters

public static byte[] encodeInteger (BigInteger bigInt)

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

public String encodeToString (byte[] pArray)

Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet.

Parameters
pArray A byte array containing binary data
Returns
  • A String containing only Base64 character data

public static boolean isArrayByteBase64 (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

public static boolean isBase64 (byte octet)

Returns whether or not the octet 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.

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.