com.oreilly.servlet
Class Base64Encoder

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by com.oreilly.servlet.Base64Encoder
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class Base64Encoder
extends java.io.FilterOutputStream

A class to encode Base64 streams and strings. See RFC 1521 section 5.2 for details of the Base64 algorithm.

This class can be used for encoding strings:

 String unencoded = "webmaster:try2gueSS";
 String encoded = Base64Encoder.encode(unencoded);
 
or for encoding streams:
 OutputStream out = new Base64Encoder(System.out);
 

Version:
1.2, 2002/11/01, added encode(byte[]) method to better handle binary data (thanks to Sean Graham), 1.1, 2000/11/17, fixed bug with sign bit for char values, 1.0, 2000/06/11
Author:
Jason Hunter, Copyright © 2000

Constructor Summary
Base64Encoder(java.io.OutputStream out)
          Constructs a new Base64 encoder that writes output to the given OutputStream.
 
Method Summary
 void close()
          Closes the stream, this MUST be called to ensure proper padding is written to the end of the output stream.
static java.lang.String encode(byte[] bytes)
          Returns the encoded form of the given unencoded string.
static java.lang.String encode(java.lang.String unencoded)
          Returns the encoded form of the given unencoded string.
static void main(java.lang.String[] args)
           
 void write(byte[] buf, int off, int len)
          Writes the given byte array to the output stream in an encoded form.
 void write(int b)
          Writes the given byte to the output stream in an encoded form.
 
Methods inherited from class java.io.FilterOutputStream
flush, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64Encoder

public Base64Encoder(java.io.OutputStream out)
Constructs a new Base64 encoder that writes output to the given OutputStream.

Parameters:
out - the output stream
Method Detail

write

public void write(int b)
           throws java.io.IOException
Writes the given byte to the output stream in an encoded form.

Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException - if an I/O error occurs

write

public void write(byte[] buf,
                  int off,
                  int len)
           throws java.io.IOException
Writes the given byte array to the output stream in an encoded form.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
b - the data to be written
off - the start offset of the data
len - the length of the data
Throws:
java.io.IOException - if an I/O error occurs

close

public void close()
           throws java.io.IOException
Closes the stream, this MUST be called to ensure proper padding is written to the end of the output stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException - if an I/O error occurs

encode

public static java.lang.String encode(java.lang.String unencoded)
Returns the encoded form of the given unencoded string. The encoder uses the ISO-8859-1 (Latin-1) encoding to convert the string to bytes. For greater control over the encoding, encode the string to bytes yourself and use encode(byte[]).

Parameters:
unencoded - the string to encode
Returns:
the encoded form of the unencoded string

encode

public static java.lang.String encode(byte[] bytes)
Returns the encoded form of the given unencoded string.

Parameters:
unencoded - the string to encode
Returns:
the encoded form of the unencoded string

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception