tribble.util
Class Base64Decoder

java.lang.Object
  extended by tribble.util.Base64Decoder

public class Base64Decoder
extends java.lang.Object

Base-64 decoding methods.

Base-64 encoding (a.k.a. radix-64 encoding) is a form of encoding binary data in 6-bit units. (6 bits provides 64 distinct binary values, hence the moniker base-64 or radix-64.) Groups of three 8-bit octets (bytes), totalling 24 bits, can be converted into four 6-bit units. Each 6-bit unit can in turn be represented by one of 64 unique printable ASCII (8-bit) characters.

Converting straight binary data (as a sequence of 8-bit octets) into a radix-64 representation is thus simply the the process of packing groups of three 8-bit octets into four 6-bit units, and then substituting each 6-bit unit with its corresponding ASCII character code.

Converting radix-64 encoded data (as a sequence of 8-bit characters) back into straight binary data is the reverse operation, substituting each radix-64 ASCII character code with its corresponding 6-bit unit, then unpacking groups of four 6-bit units into three 8-bit octets.

A 65th special ASCII character is used to indicate trailing padding in radix-64 encoded data.

This class can be used as a simple replacement for the sun.misc.BASE64Decoder class.

Since:
2003-02-26
Version:
$Revision: 1.4 $ $Date: 2005/04/16 16:05:55 $
Author:
David R. Tribble (david@tribble.com).
Copyright 2003-2005 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
Base64Encoder, sun.misc.BASE64Decoder

Field Summary
static int SERIES
          Class revision number.
 
Constructor Summary
Base64Decoder()
          Default constructor.
 
Method Summary
 byte[] decodeBuffer(java.lang.String chars)
          Decode base-64 characters into binary data.
static byte[] decodeBytes(byte[] chars)
          Decode base-64 character bytes into binary data.
static byte[] decodeBytes(byte[] chars, int off, int len)
          Decode base-64 character bytes into binary data.
static int decodeBytes(byte[] chars, int off, int len, byte[] dec, int decOff)
          Decode base-64 character bytes into binary data.
static byte[] decodeString(java.lang.String chars)
          Decode base-64 characters into binary data.
static int decodeString(java.lang.String chars, byte[] dec, int off)
          Decode base-64 characters into binary data.
static int fromBase64(int ch)
          Convert a radix-64 printable ASCII character code into its corresponding 6-bit binary value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SERIES

public static final int SERIES
Class revision number.

See Also:
Constant Field Values
Constructor Detail

Base64Decoder

public Base64Decoder()
Default constructor.

This constructor is provided for compatibility with the sun.misc.BASE64Decoder class, which allows this class to be used as a simple replacement for it.

Since:
1.2, 2005-04-10
Method Detail

decodeString

public static byte[] decodeString(java.lang.String chars)
                           throws java.text.ParseException
Decode base-64 characters into binary data.

Parameters:
chars - (const) Printable ASCII string containing binary data encoded as radix-64 characters.
Returns:
Binary data, as an array of bytes (8-bit octets). This array will contain (chars.length()-p)*3/4 bytes, where p is 0, 1, or 2, such that the total input length is a multiple of 4.
Throws:
java.text.ParseException - Thrown if chars is malformed or contains an invalid character code.
Since:
1.1, 2003-02-26
See Also:
decodeString(), Base64Encoder.encodeAsString()

decodeString

public static int decodeString(java.lang.String chars,
                               byte[] dec,
                               int off)
                        throws java.text.ParseException
Decode base-64 characters into binary data.

See decodeBytes() for more details.

Parameters:
chars - (const) Printable ASCII string containing binary data encoded as radix-64 characters.
dec - Decoded byte (8-bit octet) array, which is filled with the decoded data.
off - Index of the first byte (8-bit octet) within dec to write to.
Returns:
The number of bytes (8-bit octets) actually written into dec. This will be (len+p)*3/4 bytes, where p is 0, 1, or 2, such that the total input length is a multiple of 4.
Throws:
java.text.ParseException - Thrown if chars is malformed or contains an invalid character code.
Since:
1.1, 2003-02-26
See Also:
decodeString(), Base64Encoder.encodeAsString()

decodeBytes

public static byte[] decodeBytes(byte[] chars)
                          throws java.text.ParseException
Decode base-64 character bytes into binary data.

See decodeBytes() for more details.

Parameters:
chars - (const) An array of bytes containing printable ASCII characters, representating binary data encoded as radix-64 characters.
Returns:
Binary data, as an array of bytes (8-bit octets). This array will contain (chars.length+p)*3/4 bytes, where p is 0, 1, or 2, such that the total input length is a multiple of 4.
Throws:
java.text.ParseException - Thrown if chars is malformed or contains an invalid character code.
Since:
1.4, 2005-04-16
See Also:
decodeBytes(), Base64Encoder.encodeAsBytes()

decodeBytes

public static byte[] decodeBytes(byte[] chars,
                                 int off,
                                 int len)
                          throws java.text.ParseException
Decode base-64 character bytes into binary data.

See decodeBytes() for more details.

Parameters:
chars - (const) An array of bytes containing printable ASCII characters, representating binary data encoded as radix-64 characters.
off - Index of the first byte (8-bit octet) within chars to decode.
len - Number of bytes (8-bit octets) to decode from chars.
Returns:
Binary data, as an array of bytes (8-bit octets). This array will contain (len+p)*3/4 bytes, where p is 0, 1, or 2, such that the total input length is a multiple of 4.
Throws:
java.text.ParseException - Thrown if chars is malformed or contains an invalid character code.
Since:
1.1, 2005-04-01
See Also:
decodeBytes(), Base64Encoder.encodeAsBytes()

decodeBytes

public static int decodeBytes(byte[] chars,
                              int off,
                              int len,
                              byte[] dec,
                              int decOff)
                       throws java.text.ParseException
Decode base-64 character bytes into binary data.
    +-----------+-----------+-----------+-----------+
    |   sextet  |   sextet  |   sextet  |   sextet  |
    |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|  input sextets
    :- - - - - -:- -:- - - -:- - - -:- -:- - - - - -:
    |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|  output octets
    |     octet     |     octet     |     octet     |
    +---------------+---------------+---------------+

    +-----------+-----------+-----------+-----------+
    |   sextet  |   sextet  |   sextet  |  padding  |
    |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|    '='    |  input sextets
    :- - - - - -:- -:- - - -:- - - -:- -:- - - - - -:
    |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|x x            :  output octets
    |     octet     |     octet     |   discarded   :
    +---------------+---------------+ - - - - - - - +

    +-----------+-----------+-----------+-----------+
    |   sextet  |   sextet  |  padding  |  padding  |
    |5 4 3 2 1 0|5 4 3 2 1 0|    '='    |    '='    |  input sextets
    :- - - - - -:- -:- - - -:- - - -:- -:- - - - - -:
    |7 6 5 4 3 2 1 0|x x x x        :               :  output octets
    |     octet     |   discarded   :   discarded   :
    +---------------+ - - - - - - - + - - - - - - - + 

For example, the following table list some sextet sequences and their resulting octet decodings:

    04,11,04,11              => 11,11,11
    04,12,08,33              => 11,22,33
    3F,3F,3F,3F              => FF,FF,FF
    04,11,04, =              => 11,11
    04,11,07, =              => 11,11
    3F,3F,3C, =              => FF,FF
    04,10, =, =              => 11
    04,1F, =, =              => 11
    3F,30, =, =              => FF
    3F,30,03,3F,00,0F,3C,00  => FF,00,FF,00,FF,00 

Parameters:
chars - (const) An array of bytes containing printable ASCII characters, representating binary data encoded as radix-64 characters.
off - Index of the first byte (8-bit octet) within chars to decode.
len - Number of bytes (8-bit octets) to decode from chars.
dec - Decoded byte (8-bit octet) array, which is filled with the decoded data.
decOff - Index of the first byte (8-bit octet) within dec to write to.
Returns:
The number of bytes (8-bit octets) actually written into dec. This will be (len+p)*3/4 bytes, where p is 0, 1, or 2, such that the total input length is a multiple of 4.
Throws:
java.text.ParseException - Thrown if chars is malformed or contains an invalid character code.
Since:
1.1, 2003-02-26
See Also:
Base64Encoder.encodeAsBytes()

fromBase64

public static int fromBase64(int ch)
Convert a radix-64 printable ASCII character code into its corresponding 6-bit binary value.

Parameters:
ch - A printable radix-64 ASCII character code.
Returns:
A 6-bit binary value in the range [0,63]; or the value 64, indicating the special trailing padding code; or -1, indicating an invalid character.
Since:
1.1, 2003-02-26
See Also:
Base64Encoder.toBase64()

decodeBuffer

public byte[] decodeBuffer(java.lang.String chars)
Decode base-64 characters into binary data.

This method is provided for compatibility, producing the same result as the decodeBuffer() method of the sun.misc.BASE64Decoder class.

Parameters:
chars - (const) Printable ASCII string containing binary data encoded as radix-64 characters.
Returns:
Binary data, as an array of bytes (8-bit octets). This array will contain (chars.length()-p)*3/4 bytes, where p is 0, 1, or 2, such that the total input length is a multiple of 4.
Throws:
java.lang.IllegalArgumentException - (unchecked) Thrown if chars is malformed or contains an invalid character code.
Since:
1.2, 2005-04-10
See Also:
decodeString(), Base64Encoder.encode()