com.ca.commons.cbutil
Class CBBase64

java.lang.Object
  |
  +--com.ca.commons.cbutil.CBBase64

public class CBBase64
extends java.lang.Object

Converts back and forth between binary and base 64 (rfc 1521). There are almost certainly java classes that already do this, but it will take longer to find them than to write this :-)

Not fully optimised for speed - might be made faster if necessary...

Maybe should rewrite sometime as a stream?


Nested Class Summary
protected static class CBBase64.Base64EncodingException
           
 
Method Summary
static java.lang.String binaryToString(byte[] byteArray)
          Takes a binary byte array and converts it to base64 mime encoded data.
static java.lang.String binaryToString(byte[] byteArray, int offset)
          Takes a binary byte array and converts it to base64 mime encoded data.
static byte[] decode(byte[] rawData)
          Decodes a byte array containing base64 encoded data.
static byte[] encode(byte[] byteArray)
          Encodes an arbitrary byte array into an array of base64 encoded bytes (i.e. bytes that have values corresponding to the ascii values of base 64 data, and can be type cast to those chars).
static byte[] encodeFormatted(byte[] byteArray, int start, int colSize)
          Encodes an arbitrary byte array into an array of base64 encoded bytes (i.e. bytes that have values corresponding to the ascii values of base 64 data, and can be type cast to those chars).
static java.lang.String format(byte[] base64Data, int offset)
          This returns a formatted string representing the base64 data.
static byte[] stringToBinary(java.lang.String chars)
          Takes a base 64 encoded string and converts it to a binary byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

binaryToString

public static java.lang.String binaryToString(byte[] byteArray)
Takes a binary byte array and converts it to base64 mime encoded data.

Parameters:
byteArray - an array of 8 bit bytes to be converted
Returns:
the resultant encoded string

binaryToString

public static java.lang.String binaryToString(byte[] byteArray,
                                              int offset)
Takes a binary byte array and converts it to base64 mime encoded data.

Parameters:
byteArray - an array of 8 bit bytes to be converted
offset - The first line of the string may be offset by by this many characters for nice formatting (e.g. in an ldif file, the first line may include 'att value = ...' at the beginning of the base 64 encoded block).
Returns:
the resultant encoded string

format

public static java.lang.String format(byte[] base64Data,
                                      int offset)
This returns a formatted string representing the base64 data.

Parameters:
base64Data - a byte array of base64 encoded data.
offset - the string is formatted to fit inside column 76. The offset number provides for the first line to be foreshortened (basically for pretty formatting in ldif files).

encode

public static byte[] encode(byte[] byteArray)
Encodes an arbitrary byte array into an array of base64 encoded bytes (i.e. bytes that have values corresponding to the ascii values of base 64 data, and can be type cast to those chars).

Parameters:
byteArray - the raw data to encode
Returns:
the base64 encoded bytes (the length of this is ~ 4/3 the length of the raw data).

encodeFormatted

public static byte[] encodeFormatted(byte[] byteArray,
                                     int start,
                                     int colSize)
                              throws CBBase64.Base64EncodingException
Encodes an arbitrary byte array into an array of base64 encoded bytes (i.e. bytes that have values corresponding to the ascii values of base 64 data, and can be type cast to those chars).

Parameters:
byteArray - the raw data to encode
start - the number of padding spaces to have at the start (must be less than colSize)
colSize - the length of each line of text (i.e. the right hand margin). Must be a multiple of 4.
Returns:
the base64 encoded bytes (the length of this is ~ 4/3 the length of the raw data).
CBBase64.Base64EncodingException

stringToBinary

public static byte[] stringToBinary(java.lang.String chars)
Takes a base 64 encoded string and converts it to a binary byte array.

Parameters:
chars - a string of base64 encoded characters to be converted
Returns:
the resultant binary array

decode

public static byte[] decode(byte[] rawData)
Decodes a byte array containing base64 encoded data.

Parameters:
rawData - a byte array, each byte of which is a seven-bit ASCII value.
Returns:
the raw binary data, each byte of which may have any value from 0 - 255. This value will be null if a decoding error occurred.