org.shlublu.javax.data
Class Base64

java.lang.Object
  |
  +--org.shlublu.javax.data.Base64

public class Base64
extends java.lang.Object

RFC 2045 compliant Base 64 encoder/decoder.

Since:
0.7

Field Summary
static char[] ENCODING_TABLE
          The encoding table, according to the RFC.
static char PAD
          Metachar, known as PAD in the RFC
 
Constructor Summary
Base64()
           
 
Method Summary
 byte[] decode(char chA, char chB, char chC, char chD)
          Decoder : take four base 64 digits to produce 1 to 3 bytes, depending the padding chars.
 byte[] decode(java.lang.String strBase64Code)
          Decoder : take a base 64 string to produce the binary equivalent.
 char[] encode(byte byA)
          Encoder : take one byte to produce four base 64 digits, the two last ones padded
 java.lang.String encode(byte[] abyData)
          Encoder : convert the binary given data to a base 64 String.
 char[] encode(byte byA, byte byB)
          Encoder : take two bytes to produce four base 64 digits, the last one padded
 char[] encode(byte byA, byte byB, byte byC)
          Encoder : take three bytes to produce four base 64 digits
static int getBinaryLength(int iCodeLength, int iPadNumber)
          Return the length of the binary produced for the given base 64 code specs.
static int getBinaryLength(java.lang.String strCode)
          Return the length of the binary produced for the given base 64 code.
static int getCodeLength(byte[] abyToEncode)
          Return the length of the code produced for the given binary.
static int getCodeLength(int iBinaryLength)
          Return the length of the code produced for the given binary length.
static int getDigitIndex(char ch)
          Get the given char's correponding index
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCODING_TABLE

public static final char[] ENCODING_TABLE
The encoding table, according to the RFC. 'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X', 'Y','Z','a','b','c','d','e','f', 'g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v', 'w','x','y','z','0','1','2','3', '4','5','6','7','8','9','+','/'

PAD

public static final char PAD
Metachar, known as PAD in the RFC
Constructor Detail

Base64

public Base64()
Method Detail

getCodeLength

public static final int getCodeLength(byte[] abyToEncode)
Return the length of the code produced for the given binary. Length dedicated to the CRLF insertions is included.
Parameters:
abyToEncode - The binary to encode
Returns:
int the implied length

getCodeLength

public static final int getCodeLength(int iBinaryLength)
Return the length of the code produced for the given binary length. Length dedicated to the CRLF insertions is included.
Parameters:
abyToEncode - The binary to encode
Returns:
int the implied length

getBinaryLength

public static final int getBinaryLength(java.lang.String strCode)
                                 throws TruncatedBase64CodeException,
                                        UnexpectedPadException
Return the length of the binary produced for the given base 64 code. The code is assumed as clean, the LFs and CRs are silentely ignored.
Parameters:
strCode - The base 64 code
Returns:
int the implied length
Throws:
TruncatedBase64CodeException - if the code length is not a multiple of 4
UnexpectedPadException - if ththis code contains more than 2 PADs

getBinaryLength

public static int getBinaryLength(int iCodeLength,
                                  int iPadNumber)
                           throws TruncatedBase64CodeException,
                                  UnexpectedPadException
Return the length of the binary produced for the given base 64 code specs.
Parameters:
iCodeLength - A code length, without CR/LFs
iPadNumber - The number of padding char in this code E [0;2]
Returns:
int the implied length
Throws:
TruncatedBase64CodeException - if the code length is not a multiple of 4
UnexpectedPadException - if the pad number is not E [0;2]

encode

public final java.lang.String encode(byte[] abyData)
Encoder : convert the binary given data to a base 64 String.
According to the RFC 2045, the output is a set of lines of 76 character each.
Parameters:
byte - [] abyData The binary data to encode
Returns:
String The base 64 corresponding code, as a String obtained by new String(internal char buffer). So there's an array copy
See Also:
String.String(char[])

encode

public final char[] encode(byte byA,
                           byte byB,
                           byte byC)
Encoder : take three bytes to produce four base 64 digits
Parameters:
byte - The bytes to encode
Returns:
char[4] The base 64 corresponding digits.

encode

public final char[] encode(byte byA,
                           byte byB)
Encoder : take two bytes to produce four base 64 digits, the last one padded
Parameters:
byte - The bytes to encode
Returns:
char[4] The base 64 corresponding digits.

encode

public final char[] encode(byte byA)
Encoder : take one byte to produce four base 64 digits, the two last ones padded
Parameters:
byte - The byte to encode
Returns:
char[4] The base 64 corresponding digits.

decode

public final byte[] decode(java.lang.String strBase64Code)
                    throws UnknownBase64DigitException,
                           UnexpectedPadException,
                           TruncatedBase64CodeException
Decoder : take a base 64 string to produce the binary equivalent. The line breaks contained in the string are ignored (LF and so CR) silentely.
Parameters:
strBase64Code - The digits to decode
Returns:
byte[] The corresponding bytes.
Throws:
UnknownBase64DigitException - if one at least of the given chars is not PAD and is unknown in the encoding table.
UnexpectedPadException - if a padding char is found at an unexpected position.
TruncatedBase64CodeException - if the given code is not multiple of 4 chars

decode

public final byte[] decode(char chA,
                           char chB,
                           char chC,
                           char chD)
                    throws UnknownBase64DigitException,
                           UnexpectedPadException
Decoder : take four base 64 digits to produce 1 to 3 bytes, depending the padding chars.
Parameters:
char - The digits to decode
Returns:
byte[1 to 3] The corresponding bytes.
Throws:
UnknownBase64DigitException - if one at least of the given chars is not PAD and is unknown in the encoding table.
UnexpectedPadException - if a padding char is found in the two firsts digits, or in third position without having a padding fourth digit.

getDigitIndex

public static final int getDigitIndex(char ch)
                               throws UnknownBase64DigitException
Get the given char's correponding index
Parameters:
char - ch A base 64 digit, or PAD
Returns:
int The ENCODING_TABLE index e [0;63] of the given char, -1 if PAD
Throws:
UnknownBase64DigitException - if the given char is not PAD and unknown in the encoding table.