tribble.util
Class Base64Encoder

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

public class Base64Encoder
extends java.lang.Object

Base-64 encoding 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 base-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 base-64 encoded data (as a sequence of 8-bit characters) back into straight binary data is the reverse operation, substituting each base-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 base-64 encoded data.

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

Since:
2003-02-26
Version:
$Revision: 1.2 $ $Date: 2005/04/10 20:05:52 $
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:
Base64Decoder, sun.misc.BASE64Encoder

Field Summary
static int SERIES
          Class revision number.
 
Constructor Summary
Base64Encoder()
          Default constructor.
 
Method Summary
 java.lang.String encode(byte[] data)
          Encode binary data as base-64 characters.
static byte[] encodeAsBytes(byte[] data)
          Encode binary data as base-64 character octets.
static byte[] encodeAsBytes(byte[] data, int off, int len)
          Encode binary data as base-64 characters.
static int encodeAsBytes(byte[] data, int off, int len, byte[] enc, int encOff)
          Encode binary data as base-64 characters.
static java.lang.String encodeAsString(byte[] data)
          Encode binary data as base-64 characters.
static java.lang.String encodeAsString(byte[] data, int off, int len)
          Encode binary data as base-64 characters.
static byte toBase64(int bits)
          Convert a 6-bit binary value into its corresponding base-64 printable ASCII character code.
 
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

Base64Encoder

public Base64Encoder()
Default constructor.

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

Since:
1.2, 2005-04-10
Method Detail

encodeAsString

public static java.lang.String encodeAsString(byte[] data)
Encode binary data as base-64 characters.

Parameters:
data - (const) Binary data, as an array of bytes (8-bit octets).
Returns:
A printable ASCII string containing the contents of data encoded as base-64 characters. This string will contain data.length*4/3+p characters, where p is 0, 1, or 2, so as to make the total output length a multiple of 4.
Since:
1.1, 2003-02-26
See Also:
encodeAsString(), Base64Decoder.decodeString()

encodeAsString

public static java.lang.String encodeAsString(byte[] data,
                                              int off,
                                              int len)
Encode binary data as base-64 characters.

Parameters:
data - (const) Binary data, as an array of bytes (8-bit octets).
off - Index of the first byte (octet) within data to encode.
len - Number of bytes (octets) within data to encode.
Returns:
A printable ASCII string containing the contents of data encoded as base-64 characters. This string will contain len*4/3+p characters, where p is 0, 1, or 2, so as to make the total output length a multiple of 4.
Since:
1.1, 2003-02-26
See Also:
encodeAsBytes(), Base64Decoder.decodeString()

encodeAsBytes

public static byte[] encodeAsBytes(byte[] data)
Encode binary data as base-64 character octets.

Parameters:
data - (const) Binary data, as an array of bytes (8-bit octets).
Returns:
An array of bytes containing printable ASCII characters representating the contents of data encoded as base-64 characters. This array will contain data.length*4/3+p characters, where p is 0, 1, or 2, so as to make the total output length a multiple of 4.
Since:
1.1, 2005-04-01
See Also:
encodeAsBytes(), Base64Decoder.decodeBytes()

encodeAsBytes

public static byte[] encodeAsBytes(byte[] data,
                                   int off,
                                   int len)
Encode binary data as base-64 characters.

Parameters:
data - (const) Binary data, as an array of bytes (8-bit octets).
off - Index of the first byte (octet) within data to encode.
len - Number of bytes (octets) within data to encode.
Returns:
An array of bytes containing printable ASCII characters containing the contents of data encoded as base-64 characters. This array will contain data.length*4/3+p characters, where p is 0, 1, or 2, so as to make the total output length a multiple of 4.
Since:
1.1, 2005-04-01
See Also:
encodeAsBytes(), Base64Decoder.decodeBytes()

encodeAsBytes

public static int encodeAsBytes(byte[] data,
                                int off,
                                int len,
                                byte[] enc,
                                int encOff)
Encode binary data as base-64 characters.

Base-64 (a.k.a. radix-64) encoding converts 8-bit bytes (octets) into characters by encoding 6 bits of data (sextets) in each output character; thus 3 8-bit input bytes can be converted into 4 6-bit output characters (for a total of 24 data bits). Any input bytes left over (two or less) are converted and padded.

    +---------------+---------------+---------------+
    |     octet     |     octet     |     octet     |
    |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|  input octets
    :- - - - - -:- -:- - - -:- - - -:- -:- - - - - -:
    |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|  output sextets
    |   sextet  |   sextet  |   sextet  |   sextet  |
    +-----------+-----------+-----------+-----------+

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

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

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

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

Parameters:
data - (const) Binary data, as an array of bytes (8-bit octets).
off - Index of the first byte (octet) within data to encode.
len - Number of bytes (octets) within data to encode.
enc - An array of bytes into which will be written printable ASCII characters representating the contents of data encoded as base-64 characters.
encOff - Index of the first byte (octet) within enc to write to.
Returns:
The number of bytes (8-bit octets) actually written into enc. This will be exactly len*4/3+p bytes, where p is 0, 1, or 2, so as to make the total output length a multiple of 4.
Since:
1.1, 2005-04-01
See Also:
encodeAsBytes(), Base64Decoder.decodeBytes()

toBase64

public static byte toBase64(int bits)
Convert a 6-bit binary value into its corresponding base-64 printable ASCII character code.

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

encode

public java.lang.String encode(byte[] data)
Encode binary data as base-64 characters.

This method produces the same result as the encode() method of the sun.misc.BASE64Encoder class.

Parameters:
data - (const) Binary data, as an array of bytes (8-bit octets).
Returns:
A printable ASCII string containing the contents of data encoded as base-64 characters. This string will contain data.length*4/3+p characters, where p is 0, 1, or 2, so as to make the total output length a multiple of 4.
Since:
1.2, 2005-04-10
See Also:
encodeAsString(), Base64Decoder.decodeString()