org.yajul.io
Class Base64Decoder

java.lang.Object
  extended by org.yajul.io.Base64Decoder

public class Base64Decoder
extends java.lang.Object

Decode a BASE64 encoded input stream to some output stream. This class implements BASE64 decoding, as specified in the MIME specification.

Author:
josh (Refactored to use stream filter)
See Also:
Base64Encoder

Constructor Summary
Base64Decoder()
          Default constructor.
Base64Decoder(byte[] bytes, int offset, int length)
          Creates a decoder that will decode the specified array of bytes.
Base64Decoder(java.io.InputStream in, java.io.OutputStream out)
          Create a decoder to decode a stream.
Base64Decoder(java.lang.String input)
          Create a decoder to decode a String.
 
Method Summary
static byte[] decode(byte[] input)
          Convenience method that will decode the given byte array.
static byte[] decode(byte[] input, int offset, int length)
          Convenience method that will decode the given byte array.
static java.lang.String decode(java.lang.String input)
          Convenience method that will decode the given string.
 void initialize(byte[] bytes, int offset, int length)
          Initializes the decoder with an array of base 64 encoded bytes.
 void process()
          Do the actual decoding.
 java.lang.String processString()
          Do the decoding, and return a String.
 byte[] toByteArray()
          Returns the byte array that is the result of the decoding the input array of bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64Decoder

public Base64Decoder()
Default constructor.


Base64Decoder

public Base64Decoder(java.io.InputStream in,
                     java.io.OutputStream out)
Create a decoder to decode a stream.

Parameters:
in - The input stream (to be decoded).
out - The output stream, to write decoded data to.

Base64Decoder

public Base64Decoder(java.lang.String input)
Create a decoder to decode a String.

Parameters:
input - The string to be decoded.

Base64Decoder

public Base64Decoder(byte[] bytes,
                     int offset,
                     int length)
Creates a decoder that will decode the specified array of bytes.

Parameters:
bytes - The array of bytes to be decoded.
offset - The offset in the buffer of the first byte to read.
length - The maximum number of bytes to read from the buffer.
Method Detail

decode

public static java.lang.String decode(java.lang.String input)
                               throws Base64FormatException
Convenience method that will decode the given string.

Parameters:
input - The input string
Returns:
String - the decoded string.
Throws:
Base64FormatException - if the input is not valid.

decode

public static byte[] decode(byte[] input)
                     throws Base64FormatException
Convenience method that will decode the given byte array.

Parameters:
input - The input byte array
Returns:
String - the decoded byte array.
Throws:
Base64FormatException - if the input is not valid.

decode

public static byte[] decode(byte[] input,
                            int offset,
                            int length)
                     throws Base64FormatException
Convenience method that will decode the given byte array.

Parameters:
input - The input byte array
offset - The offset in the buffer of the first byte to read.
length - The maximum number of bytes to read from the buffer.
Returns:
String - the decoded byte array.
Throws:
Base64FormatException - if the input is not valid.

initialize

public void initialize(byte[] bytes,
                       int offset,
                       int length)
Initializes the decoder with an array of base 64 encoded bytes.

Parameters:
bytes - The array of bytes to be decoded.
offset - The offset in the buffer of the first byte to read.
length - The maximum number of bytes to read from the buffer.

process

public void process()
             throws java.io.IOException
Do the actual decoding. Process the input stream by decoding it and emiting the resulting bytes into the output stream.

Throws:
java.io.IOException - If the input or output stream accesses failed.
Base64FormatException - If the input stream is not compliant with the BASE64 specification.

processString

public java.lang.String processString()
                               throws Base64FormatException
Do the decoding, and return a String. This methods should be called when the decoder is used in String mode. It decodes the input string to an output string that is returned.
Throws: RuntimeException If the object wasn't constructed to decode a String.

Returns:
String - The decoded string.
Throws:
Base64FormatException - If the input string is not compliant with the BASE64 specification.

toByteArray

public byte[] toByteArray()
Returns the byte array that is the result of the decoding the input array of bytes. The instance must have been initialized using the initialize(byte[]) method.

Returns:
byte[] An array of decoded bytes.


Copyright © 2008. All Rights Reserved.