com.oreilly.servlet
Class Base64Decoder

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by com.oreilly.servlet.Base64Decoder
All Implemented Interfaces:
java.io.Closeable

public class Base64Decoder
extends java.io.FilterInputStream

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

This class can be used for decoding strings:

 String encoded = "d2VibWFzdGVyOnRyeTJndWVTUw";
 String decoded = Base64Decoder.decode(encoded);
 
or for decoding streams:
 InputStream in = new Base64Decoder(System.in);
 

Version:
1.1, 2002/11/01, added decodeToBytes() to better handle binary data (thanks to Sean Graham), 1.0, 2000/06/11
Author:
Jason Hunter, Copyright © 2000

Constructor Summary
Base64Decoder(java.io.InputStream in)
          Constructs a new Base64 decoder that reads input from the given InputStream.
 
Method Summary
static java.lang.String decode(java.lang.String encoded)
          Returns the decoded form of the given encoded string, as a String.
static byte[] decodeToBytes(java.lang.String encoded)
          Returns the decoded form of the given encoded string, as bytes.
static void main(java.lang.String[] args)
           
 int read()
          Returns the next decoded character from the stream, or -1 if end of stream was reached.
 int read(byte[] buf, int off, int len)
          Reads decoded data into an array of bytes and returns the actual number of bytes read, or -1 if end of stream was reached.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64Decoder

public Base64Decoder(java.io.InputStream in)
Constructs a new Base64 decoder that reads input from the given InputStream.

Parameters:
in - the input stream
Method Detail

read

public int read()
         throws java.io.IOException
Returns the next decoded character from the stream, or -1 if end of stream was reached.

Overrides:
read in class java.io.FilterInputStream
Returns:
the decoded character, or -1 if the end of the input stream is reached
Throws:
java.io.IOException - if an I/O error occurs

read

public int read(byte[] buf,
                int off,
                int len)
         throws java.io.IOException
Reads decoded data into an array of bytes and returns the actual number of bytes read, or -1 if end of stream was reached.

Overrides:
read in class java.io.FilterInputStream
Parameters:
buf - the buffer into which the data is read
off - the start offset of the data
len - the maximum number of bytes to read
Returns:
the actual number of bytes read, or -1 if the end of the input stream is reached
Throws:
java.io.IOException - if an I/O error occurs

decode

public static java.lang.String decode(java.lang.String encoded)
Returns the decoded form of the given encoded string, as a String. Note that not all binary data can be represented as a String, so this method should only be used for encoded String data. Use decodeToBytes() otherwise.

Parameters:
encoded - the string to decode
Returns:
the decoded form of the encoded string

decodeToBytes

public static byte[] decodeToBytes(java.lang.String encoded)
Returns the decoded form of the given encoded string, as bytes.

Parameters:
encoded - the string to decode
Returns:
the decoded form of the encoded string

main

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