org.openadaptor.util.base64
Class Base64

java.lang.Object
  extended by org.openadaptor.util.base64.Base64

public class Base64
extends java.lang.Object

Helper class for encoding byte arrays into base64 Strings and vice-versa. Base64 is the MIME / RFC1521 standard for turning binary byte streams into meaningless "printable" (i.e. a-z A-Z 0-9 etc.) strings for safe shipping around by text-based utilities.


Field Summary
(package private) static java.lang.StringBuffer encoded
           
static int INVALID_CHAR
          Magic number representing the invalid chars when decoding base64.
static int PAD_CHAR
          Magic number representing the padding char when decoding base64.
static int WHITESPACE_CHAR
          Magic number representing the whitespace chars when decoding base64.
 
Constructor Summary
Base64()
           
 
Method Summary
(package private) static boolean compareByteArrays(byte[] a1, byte[] a2)
           
static byte[] decode(java.lang.String base64)
          Decodes a Base64 String into a byte array.
static java.lang.String encode(byte[] raw)
          Encodes a byte array as a Base64 String.
static java.lang.String encode(byte[] raw, int maxLineLength)
          Encodes a byte array as a Base64 String with line breaks.
protected static char[] encodeBlock(byte[] raw, int offset)
          Encodes three characters at offset + [0,1,2] in raw into base64 characters.
protected static char getChar(int sixBit)
          Given a six bit value, returns the corresponding base64 character
protected static int getValue(char c)
          Decodes a single Base64 charater, and returns it as a six bit value.
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAD_CHAR

public static final int PAD_CHAR
Magic number representing the padding char when decoding base64.

See Also:
Constant Field Values

WHITESPACE_CHAR

public static final int WHITESPACE_CHAR
Magic number representing the whitespace chars when decoding base64.

See Also:
Constant Field Values

INVALID_CHAR

public static final int INVALID_CHAR
Magic number representing the invalid chars when decoding base64.

See Also:
Constant Field Values

encoded

static java.lang.StringBuffer encoded
Constructor Detail

Base64

public Base64()
Method Detail

encode

public static java.lang.String encode(byte[] raw)
Encodes a byte array as a Base64 String. The string is encoded with no line breaks, so cannot be handled easily by text editors etc.

Parameters:
raw - Byte Array to be encoded.
Returns:
Byte64 encoded String representation of raw

encode

public static java.lang.String encode(byte[] raw,
                                      int maxLineLength)
Encodes a byte array as a Base64 String with line breaks. The user must pass a maximum line length, which should be a multiple of 4 characters (if not it will be rounded up). The String returned will have have line breaks ('\n') at or before the maximum line length. In addition, a linebreak will be added at the end of the message. This whitespace will be ignored by decoders. A line length of zero or less means no line breaks.

Parameters:
raw - Byte Array to be encoded.
lineLength - maximum length of line.
Returns:
Byte64 encoded String representation of raw

encodeBlock

protected static char[] encodeBlock(byte[] raw,
                                    int offset)
Encodes three characters at offset + [0,1,2] in raw into base64 characters. Note this is ripe for optimisation!


getChar

protected static char getChar(int sixBit)
Given a six bit value, returns the corresponding base64 character

Parameters:
sixBit - Six bit integer value.
Returns:
Corresponding base64 character, or '?' for a number outside 0..63.

decode

public static byte[] decode(java.lang.String base64)
                     throws Base64Exception
Decodes a Base64 String into a byte array. Note that is can handle embedded whitespace i.e. linebreaks. Note this is ripe for optimisation!

Parameters:
base64 - Base64 String to be decoded.
Returns:
Decoded byte Array containing bytes in the original message.
Throws:
Base64Exception - If base64 contains an invalid base64 message, i.e. illegal charaters, too many padding chars etc.

getValue

protected static int getValue(char c)
Decodes a single Base64 charater, and returns it as a six bit value.

Returns:
Six bit value of char c or PAD_CHAR, WHITESPACE_CHAR or INVALID_CHAR.

main

public static void main(java.lang.String[] args)

compareByteArrays

static boolean compareByteArrays(byte[] a1,
                                 byte[] a2)