com.yosokumo.core
Class Base64

java.lang.Object
  extended by com.yosokumo.core.Base64

 class Base64
extends java.lang.Object

Provides methods for converting byte sequences to Base64 character strings and vice versa.

Be aware that there is not a one-to-one correspondence between byte sequences and Base64 character sequences. Given any character sequence C created by encodeBytes, the call decodeString(C) will return the original byte sequence. However, there exist character sequences which are never produced by encodeBytes, and, hence, applying decodeString to such character sequences produces byte sequences from which the original character sequences cannot be recovered. For example, decodeString converts both AA== and AB== to the single byte 0, and encodeBytes converts the single byte 0 to AA==. There does not exist a sequence of bytes which will cause encodeBytes to return AB==.


Field Summary
private static char[] encode64
          Base64 character set: A-Z, a-z, 0-9, +, /.
 
Constructor Summary
Base64()
           
 
Method Summary
(package private) static byte decode64(char c)
          Convert a 6-bit Base64 character to an 8-bit byte.
static byte[] decodeString(java.lang.String source)
          Convert a Base64 string to a sequence of bytes.
static java.lang.String encodeBytes(byte[] source)
          Convert a sequence of bytes to a Base64 string.
private static void map3to4(byte A, byte B, byte C, java.lang.StringBuilder abcd)
          Convert a sequence of three bytes to a sequence of four characters.
private static void map4to3(char a, char b, char c, char d, byte[] ABC, int j, int n)
          Convert a sequence of four characters to a sequence of three bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

encode64

private static final char[] encode64
Base64 character set: A-Z, a-z, 0-9, +, /.

Constructor Detail

Base64

Base64()
Method Detail

map3to4

private static void map3to4(byte A,
                            byte B,
                            byte C,
                            java.lang.StringBuilder abcd)
Convert a sequence of three bytes to a sequence of four characters. Map three 8-bit bytes to four 6-bit bytes:
       765432 10 7654 3210 76 543210 
      +---------+---------+---------+
      |AAAAAA AA|BBBB BBBB|CC CCCCCC|
      +------+-------+-------+------+
      |aaaaaa|bb bbbb|cccc cc|dddddd|
      +------+-------+-------+------+
       543210 54 3210 5432 10 543210

Parameters:
A - the first byte.
B - the second byte.
C - the third byte.
abcd - the resulting four characters are appended to abcd.

encodeBytes

public static java.lang.String encodeBytes(byte[] source)
Convert a sequence of bytes to a Base64 string.

Parameters:
source - is a sequence of zero or more 8-bit bytes.
Returns:
a String containing the Base64 representation of the input bytes, 6-bits per character.

decode64

static byte decode64(char c)
Convert a 6-bit Base64 character to an 8-bit byte.

Parameters:
c - a 6-bit Base64 character.
Returns:
the 8-bit byte corresponding to the input character.
Throws:
java.lang.IllegalArgumentException - if the input character is not a Base64 character.

map4to3

private static void map4to3(char a,
                            char b,
                            char c,
                            char d,
                            byte[] ABC,
                            int j,
                            int n)
Convert a sequence of four characters to a sequence of three bytes. See the comment for map3to4 for the exact mapping.

Parameters:
a - the first character.
b - the second character.
c - the third character.
d - the fourth character.
ABC - the resulting three bytes are stored here.
j - an index to the first byte in ABC to change.
n - 1, 2, or 3, indicating how many bytes to store in ABC.
Throws:
java.lang.IllegalArgumentException - if any input character is not a Base64 character.

decodeString

public static byte[] decodeString(java.lang.String source)
Convert a Base64 string to a sequence of bytes.

Parameters:
source - a string of zero or more 6-bit Base64 characters.
Returns:
a byte sequence containing the 8-bit bytes corresponding to the input characters.
Throws:
java.lang.IllegalArgumentException - if the length of the input string is not a multiple of four.
java.lang.IllegalArgumentException - if the input string contains a character which is not a Base64 character.