com.bristle.javalib.util
Class Base64

java.lang.Object
  extended by com.bristle.javalib.util.Base64

public class Base64
extends Object

This class contains support for Base64 encoding and decoding.

Usage:
   - The typical scenario for using this class is:
     - To encode a string as Base64:
               String strEncoded = Base64.encode(strDecoded);
     - To decode a Base64 string:
               String strDecoded = Base64.decode(strEncoded);

   - See the source code of the inner Tester class for more examples.
  
Assumptions:
Effects:
       - None.
Anticipated Changes:
Notes:
Implementation Notes:
Portability Issues:
Revision History:
   $Log$


Nested Class Summary
static class Base64.InvalidBase64DigitException
          This exception is thrown when an invalid character is found as a digit in a Base64 encoded string that is being decoded.
static class Base64.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
static class Base64.TruncatedBase64EncodingException
          This exception is thrown when a Base64 encoded string that is being decoded has an invalid length, or has a trailing Base64 digit that implies that there should be more digits.
 
Field Summary
private static char[] arrchBASE_64_DIGITS
           
 
Constructor Summary
Base64()
           
 
Method Summary
private static int assembleByte(byte byte1, int intMask1, int intShift1, byte byte2, int intMask2, int intShift2)
          Assemble an 8-bit byte from 2 8-bit bytes, using the specified shifts and bitmasks.
static String decode(String strIn)
          Return the decoded string value of the specified Base64 encoded string.
static String encode(String strIn)
          Return the Base64 encoded string value of the specified string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

arrchBASE_64_DIGITS

private static final char[] arrchBASE_64_DIGITS
Constructor Detail

Base64

public Base64()
Method Detail

assembleByte

private static int assembleByte(byte byte1,
                                int intMask1,
                                int intShift1,
                                byte byte2,
                                int intMask2,
                                int intShift2)
Assemble an 8-bit byte from 2 8-bit bytes, using the specified shifts and bitmasks. Each byte is masked, then shifted. Then the bytes are OR'ed together and the low order 8-bits are returned. Deals with all the issues like sign bit extension during implicit promotion to int before shifting.

Parameters:
byte1 - First byte
intMask1 - Bitmask to apply before shifting
intShift1 - Number of bits to shift left (negative for right)
byte2 - Second byte
intMask2 - Bitmask to apply before shifting
intShift2 - Number of bits to shift left (negative for right)
Returns:
Assembled byte.

encode

public static String encode(String strIn)
                     throws UnsupportedEncodingException
Return the Base64 encoded string value of the specified string. The specified string is assumed to use the ISO-8859-1 (Latin-1) encoding.

Parameters:
strIn - String to be encoded.
Returns:
Encoded string.
Throws:
UnsupportedEncodingException - When the incoming string does not use the ISO-8859-1 (Latin-1) encoding.

decode

public static String decode(String strIn)
                     throws UnsupportedEncodingException,
                            Base64.InvalidBase64DigitException,
                            Base64.TruncatedBase64EncodingException
Return the decoded string value of the specified Base64 encoded string. The specified string is assumed to use the ISO-8859-1 (Latin-1) encoding.

Parameters:
strIn - String to be decoded.
Returns:
Decoded string.
Throws:
UnsupportedEncodingException - When the incoming string does not use the ISO-8859-1 (Latin-1) encoding.
Base64.InvalidBase64DigitException - When the string to be decoded contains a character that is not a valid Base64 digit char.
Base64.TruncatedBase64EncodingException - When the string to be decoded has an invalid length.