codec
Class Base64

java.lang.Object
  extended by codec.Base64

public class Base64
extends Object

Encodes and decodes data according to Base64 encoding as described in RFC 1521. Encoded data is broken into lines of 76 charcters each (19 groups of 4 characters which represent 3 bytes of input per group.

The three input bytes are divided into four groups of six bits each which are encoded according to the table base64 given below.

Version:
"$Id: Base64.java 1913 2007-08-08 02:41:53Z jpeters $"
Author:
Volker Roth

Field Summary
private static char[] base64
          The basic Base64 encoding table.
private static byte F
          Marks an entry in the decoding table as an invalid code character.
private static String hex
          Used to transform bytes into a hexadecimal string representation - basically a convenience method which facilitates debugging and verification.
private static byte PAD
          Marks the pad character '=' in the decoding table.
private static char pad1
          If the input is congruent 2 modulo 3 then one pad character is required.
private static String pad2
          If the input is congruent 1 modulo 3 then two pad characters are required.
private static byte[] reverse
          The table reverse serves to transform encoded characters back into the corresponding six bit values efficiently.
 
Constructor Summary
private Base64()
          This class is never instantiated; use the class methods instead.
 
Method Summary
static byte[] decode(byte[] code)
          Decodes a given byte array containing Unicode BASE_LATIN encoded characters with one byte encoding per character.
static byte[] decode(String input)
          Decodes a Base64 encoded string into an array of bytes with exactly the length of the encoded data.
static String encode(byte[] input)
          Encodes the input array of bytes into a Base64 encoded string with padding if required.
static String toHex(byte[] b)
          Encodes a byte array into a hexadecimal string representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

base64

private static final char[] base64
The basic Base64 encoding table. The index into the string is the value being encoded with the corresponding character.


pad1

private static char pad1
If the input is congruent 2 modulo 3 then one pad character is required.


pad2

private static String pad2
If the input is congruent 1 modulo 3 then two pad characters are required.


F

private static final byte F
Marks an entry in the decoding table as an invalid code character.

See Also:
Constant Field Values

PAD

private static final byte PAD
Marks the pad character '=' in the decoding table.

See Also:
Constant Field Values

reverse

private static final byte[] reverse
The table reverse serves to transform encoded characters back into the corresponding six bit values efficiently.


hex

private static final String hex
Used to transform bytes into a hexadecimal string representation - basically a convenience method which facilitates debugging and verification.

See Also:
Constant Field Values
Constructor Detail

Base64

private Base64()
This class is never instantiated; use the class methods instead.

Method Detail

encode

public static String encode(byte[] input)
Encodes the input array of bytes into a Base64 encoded string with padding if required. The output is not broken into lines.

Parameters:
input - The byte array to be encoded.
Returns:
The Base64 encoded String representing the input byte array.

decode

public static byte[] decode(String input)
                     throws CorruptedCodeException
Decodes a Base64 encoded string into an array of bytes with exactly the length of the encoded data. The encoded string may contain arbitrarily much garbage data in the form of control sequences and non-base64 characters as long as the local charcter encoding is Unicode BASE_LATIN.

Parameters:
input - The encoded Base64 character String.
Returns:
The decoded data.
Throws:
CorruptedCodeException - if the Base64 code contains errors such as a missing character or bad padding.

decode

public static byte[] decode(byte[] code)
                     throws CorruptedCodeException
Decodes a given byte array containing Unicode BASE_LATIN encoded characters with one byte encoding per character. The array is garbled in the course of the decoding, because it is partly overwritten with the six Bit groups represented by the code letters! This method is used by the decoding method byte[] decode(String).

Parameters:
code - The BASE_LATIN Base64 encoded input with one byte per character.
Returns:
The byte array containing the decoded data. The array has the exact length of the decoded data.
Throws:
CorruptedCodeException - if the decoding process revealed errors such as bad padding or missing charcters.

toHex

public static String toHex(byte[] b)
Encodes a byte array into a hexadecimal string representation.

Parameters:
b - The input byte array.
Returns:
The string with the hexadecimal representation.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.