org.csource.common
Class Base64

java.lang.Object
  extended by org.csource.common.Base64

public class Base64
extends java.lang.Object

Encode arbitrary binary into printable ASCII using BASE64 encoding. very loosely based on the Base64 Reader by Dr. Mark Thornton Optrak Distribution Software Ltd. http://www.optrak.co.uk and Kevin Kelley's http://www.ruralnet.net/~kelley/java/Base64.java Base64 is a way of encoding 8-bit characters using only ASCII printable characters similar to UUENCODE. UUENCODE includes a filename where BASE64 does not. The spec is described in RFC 2045. Base64 is a scheme where 3 bytes are concatenated, then split to form 4 groups of 6-bits each; and each 6-bits gets translated to an encoded printable ASCII character, via a table lookup. An encoded string is therefore longer than the original by about 1/3. The "=" character is used to pad the end. Base64 is used, among other things, to encode the user:password string in an Authorization: header for HTTP. Don't confuse Base64 with x-www-form-urlencoded which is handled by Java.net.URLEncoder.encode/decode If you don't like this code, there is another implementation at http://www.ruffboy.com/download.htm Sun has an undocumented method called sun.misc.Base64Encoder.encode. You could use hex, simpler to code, but not as compact. If you wanted to encode a giant file, you could do it in large chunks that are even multiples of 3 bytes, except for the last chunk, and append the outputs. To encode a string, rather than binary data java.net.URLEncoder may be better. See printable characters in the Java glossary for a discussion of the differences. version 1.4 2002 February 15 -- correct bugs with uneven line lengths, allow you to configure line separator. now need Base64 object and instance methods. new mailing address. version 1.3 2000 September 12 -- fix problems with estimating output length in encode version 1.2 2000 September 09 -- now handles decode as well. version 1.1 1999 December 04 -- more symmetrical encoding algorithm. more accurate StringBuffer allocation size. version 1.0 1999 December 03 -- posted in comp.lang.java.programmer. Futures Streams or files.


Constructor Summary
Base64()
           
Base64(char chPlus, char chSplash, char chPad, int lineLength)
           
Base64(int lineLength)
           
 
Method Summary
 byte[] decode(java.lang.String s)
          decode a well-formed complete Base64 string back into an array of bytes.
 byte[] decodeAuto(java.lang.String s)
          decode a well-formed complete Base64 string back into an array of bytes.
static void display(byte[] b)
          debug display array
 java.lang.String encode(byte[] b)
          Encode an arbitrary array of bytes as Base64 printable ASCII.
static void main(java.lang.String[] args)
          test driver
 void setLineLength(int length)
          determines how long the lines are that are generated by encode.
 void setLineSeparator(java.lang.String lineSeparator)
          How lines are separated.
static void show(byte[] b)
          debug display array
static void test()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64

public Base64()

Base64

public Base64(char chPlus,
              char chSplash,
              char chPad,
              int lineLength)

Base64

public Base64(int lineLength)
Method Detail

encode

public java.lang.String encode(byte[] b)
                        throws java.io.IOException
Encode an arbitrary array of bytes as Base64 printable ASCII. It will be broken into lines of 72 chars each. The last line is not terminated with a line separator. The output will always have an even multiple of data characters, exclusive of \n. It is padded out with =.

Throws:
java.io.IOException

decodeAuto

public byte[] decodeAuto(java.lang.String s)
decode a well-formed complete Base64 string back into an array of bytes. It must have an even multiple of 4 data characters (not counting \n), padded out with = as needed.


decode

public byte[] decode(java.lang.String s)
decode a well-formed complete Base64 string back into an array of bytes. It must have an even multiple of 4 data characters (not counting \n), padded out with = as needed.


setLineLength

public void setLineLength(int length)
determines how long the lines are that are generated by encode. Ignored by decode.

Parameters:
length - 0 means no newlines inserted. Must be a multiple of 4.

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
How lines are separated. Ignored by decode.

Parameters:
lineSeparator - may be "" but not null. Usually contains only a combination of chars \n and \r. Could be any chars not in set A-Z a-z 0-9 + /.

show

public static void show(byte[] b)
debug display array


display

public static void display(byte[] b)
debug display array


main

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


test

public static void test()