org.apache.commons.codec.binary
Class Base64

java.lang.Object
  extended by org.apache.commons.codec.binary.BaseNCodec
      extended by org.apache.commons.codec.binary.Base64
All Implemented Interfaces:
BinaryDecoder, BinaryEncoder, Decoder, Encoder

public class Base64
extends BaseNCodec

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:

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 not thread-safe. Each thread should use its own instance.

Since:
1.0
Version:
$Revision: 1201529 $
Author:
Apache Software Foundation
See Also:
RFC 2045

Field Summary
private static int BITS_PER_ENCODED_BYTE
          BASE32 characters are 6 bits in length.
private  int bitWorkArea
          Place holder for the bytes we're dealing with for our based logic.
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.
 
Fields inherited from class org.apache.commons.codec.binary.BaseNCodec
buffer, currentLinePos, eof, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, modulus, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE, pos
 
Constructor Summary
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.
 
Method Summary
(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 byte[] decodeBase64(java.lang.String base64String)
          Decodes a Base64 String into octets
static java.math.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)
           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 java.lang.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 java.lang.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(java.math.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(java.lang.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 isUrlSafe()
          Returns our current encode mode.
(package private) static byte[] toIntegerBytes(java.math.BigInteger bigInt)
          Returns a byte-array representation of a BigInteger without sign bit.
 
Methods inherited from class org.apache.commons.codec.binary.BaseNCodec
available, containsAlphabetOrPad, decode, decode, decode, encode, encode, encodeAsString, encodeToString, ensureBufferSize, getDefaultBufferSize, getEncodedLength, hasData, isInAlphabet, isInAlphabet, isWhiteSpace, readResults
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BITS_PER_ENCODED_BYTE

private static final int BITS_PER_ENCODED_BYTE
BASE32 characters are 6 bits in length. They are formed by taking a block of 3 octets to form a 24-bit string, which is converted into 4 BASE64 characters.

See Also:
Constant Field Values

BYTES_PER_UNENCODED_BLOCK

private static final int BYTES_PER_UNENCODED_BLOCK
See Also:
Constant Field Values

BYTES_PER_ENCODED_BLOCK

private static final int BYTES_PER_ENCODED_BLOCK
See Also:
Constant Field Values

CHUNK_SEPARATOR

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.

See Also:
RFC 2045 section 2.1

STANDARD_ENCODE_TABLE

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/


URL_SAFE_ENCODE_TABLE

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.


DECODE_TABLE

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/


MASK_6BITS

private static final int MASK_6BITS
Mask used to extract 6 bits, used when encoding

See Also:
Constant Field Values

encodeTable

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.


decodeTable

private final byte[] decodeTable

lineSeparator

private final byte[] lineSeparator
Line separator for encoding. Not used when decoding. Only used if lineLength > 0.


decodeSize

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;


encodeSize

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;


bitWorkArea

private int bitWorkArea
Place holder for the bytes we're dealing with for our based logic. Bitwise operations store and extract the encoding or decoding from this variable.

Constructor Detail

Base64

public Base64()
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)
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.
Since:
1.4

Base64

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.
Since:
1.4

Base64

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:
java.lang.IllegalArgumentException - Thrown when the provided lineSeparator included some base64 characters.
Since:
1.4

Base64

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:
java.lang.IllegalArgumentException - The provided lineSeparator included some base64 characters. That's not going to work!
Since:
1.4
Method Detail

isUrlSafe

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.
Since:
1.4

encode

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/

Specified by:
encode in class BaseNCodec
Parameters:
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.

decode

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/

Specified by:
decode in class BaseNCodec
Parameters:
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.

isArrayByteBase64

public static boolean isArrayByteBase64(byte[] arrayOctet)
Deprecated. 1.5 Use isBase64(byte[]), will be removed in 2.0.

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

isBase64

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.
Since:
1.4

isBase64

public static boolean isBase64(java.lang.String base64)
Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.

Parameters:
base64 - String to test
Returns:
true if all characters in the String are valid characters in the Base64 alphabet or if the String is empty; false, otherwise
Since:
1.5

isBase64

public static boolean isBase64(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
Since:
1.5

encodeBase64

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.

encodeBase64String

public static java.lang.String encodeBase64String(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behaviour 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).

encodeBase64URLSafe

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.
Since:
1.4

encodeBase64URLSafeString

public static java.lang.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
Since:
1.4

encodeBase64Chunked

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

encodeBase64

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:
java.lang.IllegalArgumentException - Thrown when the input array needs an output array bigger than Integer.MAX_VALUE

encodeBase64

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:
java.lang.IllegalArgumentException - Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
Since:
1.4

encodeBase64

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:
java.lang.IllegalArgumentException - Thrown when the input array needs an output array bigger than maxResultSize
Since:
1.4

decodeBase64

public static byte[] decodeBase64(java.lang.String base64String)
Decodes a Base64 String into octets

Parameters:
base64String - String containing Base64 data
Returns:
Array containing decoded data.
Since:
1.4

decodeBase64

public static byte[] decodeBase64(byte[] base64Data)
Decodes Base64 data into octets

Parameters:
base64Data - Byte array containing Base64 data
Returns:
Array containing decoded data.

decodeInteger

public static java.math.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
Since:
1.4

encodeInteger

public static byte[] encodeInteger(java.math.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:
java.lang.NullPointerException - if null is passed in
Since:
1.4

toIntegerBytes

static byte[] toIntegerBytes(java.math.BigInteger bigInt)
Returns a byte-array representation of a BigInteger without sign bit.

Parameters:
bigInt - BigInteger to be converted
Returns:
a byte array representation of the BigInteger parameter

isInAlphabet

protected boolean isInAlphabet(byte octet)
Returns whether or not the octet is in the Base32 alphabet.

Specified by:
isInAlphabet in class BaseNCodec
Parameters:
octet - The value to test
Returns:
true if the value is defined in the the Base32 alphabet false otherwise.


commons-codec version 1.6-SNAPSHOT - Copyright © 2002-2012 - Apache Software Foundation