Class Base64

java.lang.Object
  extended by Base64

public class Base64
extends java.lang.Object

A simple container for several static base64 methods.

One need never instantiate this class, but should simply use its static methods.


Method Summary
static byte[] decodeBase64(java.lang.String base64)
          Decodes base64 text into a new byte array.
static int decodeBase64Quad(char[] quad, byte[] dest, int offset)
          Decodes four base64 characters as three bytes.
static boolean isBase64Char(char c)
          Determines whether a character is a base64 digit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

decodeBase64

public static byte[] decodeBase64(java.lang.String base64)

Decodes base64 text into a new byte array.

The base64 digits are, in ascending order, 'A'-'Z', 'a'-'z', '0'-'9', '+', '/'. Since each digit represents six bits, every four digits corresponds to three bytes. Thus the number of digits in a valid base64 string is a multiple of four, and to encode a number of bytes which is not a multiple of three, '=' is used. If the last digit is '=', the last byte is dropped, and if the last two digits are '=', the last two bytes are dropped.

Any non-null String is valid input for this method, although the output is undefined for Strings containing '=' other than as the last one or two base64 digits. All non-base64 characters are simply ignored.

Parameters:
base64 - a non-null String
Returns:
a new byte array resulting from decoding base64

decodeBase64Quad

public static int decodeBase64Quad(char[] quad,
                                   byte[] dest,
                                   int offset)

Decodes four base64 characters as three bytes. This can be useful for serially decoding a stream of input.

The decoded bytes are placed into dest, beginning at the index offset. The number of decoded bytes is returned.

Unlike decodeBase64(String), all of the inputted characters are expected to be valid base64 characters. You can use isBase64Char(char) to help ensure this.

The behavior is unspecified if any of the characters are not base64 digits, or if the quad is too short or improperly padded.

Parameters:
quad - The input base64 digits. The first four elements must be valid base64 chars with valid padding.
dest - The destination byte array.
offset - The offset into dest at which bytes will be stored.
Returns:
The number of bytes decoded: 1, 2, or 3 depending on padding.
See Also:
isBase64Char(char), decodeBase64(String)

isBase64Char

public static boolean isBase64Char(char c)

Determines whether a character is a base64 digit. Returns true if the character is in the ranges 'A'-'Z', 'a'-'z', '0'-'9', or is '+', '/', or '='.

Parameters:
c - the character to be checked
Returns:
(c>='A'&&c<='Z') || (c>='a'&&c<='z') || (c>='0'&&c<='9') || c=='+' || c=='/' || c=='='