webfunds.util
Class Base64Coder

java.lang.Object
  |
  +--webfunds.util.Base64Coder

public class Base64Coder
extends java.lang.Object

This class provides methods for encoding and decoding data in MIME base64 format.

All of the methods either take a byte array, or a String. and return a byte array or String (depending upon the method) containing only the base 64 encoded data. The return value represents a sequence of characters, and depending upon the method, can be either a byte array or String, since these characters are from a subset of both the Unicode and ASCII character repertoires. Base64 encoding encodes 6 binary bits per character, using the characters 'A'-'Z', 'a'-'z', '0'-'9', '+', '/', and '=' (for padding).

References:

  1. RFC 2045, Multipurpose Internet Mail Extensions (MIME) Part One, Format of Internet Message Bodies, "Section 6.8 Base64 Content-Transfer-Encoding," http://www.imc.org/rfc2045

Users are Armoury.java and Contract.java.

Repository copied from webfunds/sox/utils/Base64.java at 1.5.


Field Summary
protected static byte[] dec_table
          Maps the above specific chars back to the 6 bit integer.
protected static char[] enc_table
          Maps 6 bit integers to a specific char.
protected  java.lang.String newline
          The newline used on encodes is by default the system property one.
protected  int numGroups
          Each line is encoded as numGroups groups, where each group is 4 Base64 letters making up a 3-byte binary sequence.
 
Constructor Summary
Base64Coder()
          Set up Coder with default paramaters, which are thought to be best for all circumstances (although these are mostly historical...).
 
Method Summary
static byte[] dec(byte[] buf)
          Ignores trailing whitespace and newlines
protected static short dec(byte[] ret, int ret_off, byte a, byte b, byte c, byte d)
           
 byte[] decode(byte[] buf)
          Decode a Base 64 byte array.
 byte[] decode(java.lang.String msg)
          Decode a Base 64 string.
protected static java.lang.String enc(byte[] buf, int numGroups, java.lang.String nl)
          Encodes into numGroups groups, where each group is 4 Base64 letters making up a 3-byte binary sequence.
 java.lang.String encode(byte[] buf)
          Encode a byte array into Base 64.
 java.lang.String encode(java.lang.String s)
          Encode a string into Base 64.
static byte[] getDecodeTable()
           
static char[] getEncodeTable()
          For filtering, etc.
 int getMaximumLineLength()
           
 java.lang.String getNewline()
           
 int getNumberOfGroups()
           
static void main(java.lang.String[] args)
           
 void setMaximumLineLength(int i)
          set the number of Base 64 characters per encoded line
 void setNewline(java.lang.String s)
          set the newline string sequence for encoding
 void setNumberOfGroups(int i)
          set the number of groups per encoded line
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

newline

protected java.lang.String newline
The newline used on encodes is by default the system property one.

numGroups

protected int numGroups
Each line is encoded as numGroups groups, where each group is 4 Base64 letters making up a 3-byte binary sequence. Current default of 19 gives 57 ascii chars written per line.

enc_table

protected static final char[] enc_table
Maps 6 bit integers to a specific char.

dec_table

protected static final byte[] dec_table
Maps the above specific chars back to the 6 bit integer.
Constructor Detail

Base64Coder

public Base64Coder()
Set up Coder with default paramaters, which are thought to be best for all circumstances (although these are mostly historical...).
Method Detail

getNewline

public java.lang.String getNewline()
Returns:
the current set newline for encoding (default is property)

setNewline

public void setNewline(java.lang.String s)
set the newline string sequence for encoding

getNumberOfGroups

public int getNumberOfGroups()
Returns:
the current number of groups for each line

setNumberOfGroups

public void setNumberOfGroups(int i)
set the number of groups per encoded line

getMaximumLineLength

public int getMaximumLineLength()
Returns:
the current number of Base 64 characters for each line

setMaximumLineLength

public void setMaximumLineLength(int i)
set the number of Base 64 characters per encoded line

getEncodeTable

public static char[] getEncodeTable()
For filtering, etc.

getDecodeTable

public static byte[] getDecodeTable()

encode

public java.lang.String encode(byte[] buf)
Encode a byte array into Base 64.

encode

public java.lang.String encode(java.lang.String s)
Encode a string into Base 64.

decode

public byte[] decode(java.lang.String msg)
Decode a Base 64 string. Ignores trailing whitespace and newlines

decode

public byte[] decode(byte[] buf)
Decode a Base 64 byte array. Ignores trailing whitespace and newlines

enc

protected static java.lang.String enc(byte[] buf,
                                      int numGroups,
                                      java.lang.String nl)
Encodes into numGroups groups, where each group is 4 Base64 letters making up a 3-byte binary sequence.
Parameters:
buf - binary sequence to be encoded
numGroups - number of groups to use per line
nl - is the newline to be appended to each line

dec

public static byte[] dec(byte[] buf)
Ignores trailing whitespace and newlines

dec

protected static short dec(byte[] ret,
                           int ret_off,
                           byte a,
                           byte b,
                           byte c,
                           byte d)
Returns:
the number of bytes converted

main

public static void main(java.lang.String[] args)