tribble.io
Class Base64DecoderInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by tribble.io.Base64DecoderInputStream
All Implemented Interfaces:
java.io.Closeable

public class Base64DecoderInputStream
extends java.io.FilterInputStream

Base-64 decoding input stream.

Provides an I/O stream that decodes radix-64 characters into binary data. Printable 8-bit bytes (octets) are read from the input stream which encode data as base-64 (a.k.a. radix-64) ASCII characters and are converted into binary data bytes.

See Base64Decoder for more information about base-64 decoding.

Source code:
http://david.tribble.com/src/java/tribble/io/Base64DecoderInputStream.java
Documentation:
http://david.tribble.com/docs/tribble/io/Base64DecoderInputStream.html

Since:
2005-04-11
Version:
$Revision: 1.3 $ $Date: 2009/02/04 03:49:46 $
Author:
David R. Tribble (david@tribble.com)

Copyright ©2005-2009 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by by the United States Department of State as a terrorist, or terrorist government or agency, to use and distribute this source code provided that the original copyright notice remains present and unaltered.

See Also:
Base64EncoderInputStream, Base64DecoderOutputStream, Base64Encoder

Field Summary
protected  boolean m_eof
          End of input stream reached.
protected  byte[] m_inBuf
          Pending input queue.
protected  short m_inLen
          Pending input queue length.
protected  byte[] m_outBuf
          Output queue.
protected  short m_outLen
          Current output byte queue length.
protected  short m_outPos
          Next output byte queue index.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
Base64DecoderInputStream(java.io.InputStream in)
          Constructor.
Base64DecoderInputStream(java.io.Reader in)
          Constructor.
 
Method Summary
 void close()
          Close this input stream.
static void decode(java.io.InputStream in, java.io.OutputStream out)
          Read and convert the contents of an input stream.
static void decode(java.io.InputStream in, java.io.OutputStream out, int bufLen)
          Read and convert the contents of an input stream.
 void decode(java.io.OutputStream out)
          Read and convert the contents of an input stream.
 void decode(java.io.OutputStream out, int bufLen)
          Read and convert the contents of an input stream.
 java.io.InputStream getInput()
          Retrieve the underlying input stream for this output stream.
static void main(java.lang.String[] args)
          Test driver.
 int read()
          Read a character from this input stream.
 int read(byte[] buf, int off, int len)
          Reads up to len bytes of data from this input stream into an array of bytes.
protected  void readPending()
          Read base-64 encoded data bytes from the input stream and convert them into data bytes (octets).
 
Methods inherited from class java.io.FilterInputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_inBuf

protected byte[] m_inBuf
Pending input queue.


m_outBuf

protected byte[] m_outBuf
Output queue.


m_inLen

protected short m_inLen
Pending input queue length.


m_outPos

protected short m_outPos
Next output byte queue index.


m_outLen

protected short m_outLen
Current output byte queue length.


m_eof

protected boolean m_eof
End of input stream reached.

Constructor Detail

Base64DecoderInputStream

public Base64DecoderInputStream(java.io.InputStream in)
Constructor.

Parameters:
in - The underlying input stream for this input stream.
Since:
1.1, 2005-04-11

Base64DecoderInputStream

public Base64DecoderInputStream(java.io.Reader in)
Constructor.

Parameters:
in - The underlying input stream for this input stream.
Since:
1.1, 2005-04-11
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Test driver. Read one or more base-64 encoded input files and write them as binary output form.

Usage

java tribble.io.Base64DecoderInputStream [-o outfile] file...

Parameters:
args - Command line arguments.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
Since:
1.1, 2005-04-01

decode

public static void decode(java.io.InputStream in,
                          java.io.OutputStream out)
                   throws java.io.IOException
Read and convert the contents of an input stream. This method is equivalent to the call: decode(in, out, 0).

Parameters:
in - An input stream containing base-64 encoded character data to be converted into binary 8-bit octet data.
out - An output stream to which is written the decoded binary 8-bit octet data bytes.
Throws:
java.io.IOException - Thrown if an I/O (read or write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if in or out is null.
Since:
1.3, 2009-02-03
See Also:
decode()

decode

public static void decode(java.io.InputStream in,
                          java.io.OutputStream out,
                          int bufLen)
                   throws java.io.IOException
Read and convert the contents of an input stream.

The entire contents of an input stream are read as base-64 encoded characters and converted into 8-bit octets, which are then written to an output stream.

Note that the output stream is flushed after the converted data is written, but is not closed, i.e., method flush() is called but close() is not.

Parameters:
in - An input stream containing base-64 encoded character data to be converted into binary 8-bit octet data.
out - An output stream to which is written the decoded binary 8-bit octet data bytes.
bufLen - Specifies the size, in bytes, of the internal buffer to use for reading data from the input stream. Specifying a value less than 4 causes a default size of 64K (65,536) to be used.
Throws:
java.io.IOException - Thrown if an I/O (read or write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if in or out is null.
Since:
1.3, 2009-02-03
See Also:
decode()

close

public void close()
           throws java.io.IOException
Close this input stream.

Any pending base-64 character input is discarded.

Subsequent attempts to read from this input stream will cause exceptions to be thrown.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterInputStream
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2005-04-11

read

public int read()
         throws java.io.IOException
Read a character from this input stream.

A base-64 character encoding is converted into its corresponding 8-bit binary code (octet) after being read from the underlying input stream.

Overrides:
read in class java.io.FilterInputStream
Returns:
An 8-bit character code, or -1 if the end of the input stream has been reached.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2005-04-11

read

public int read(byte[] buf,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

Overrides:
read in class java.io.FilterInputStream
Parameters:
buf - The buffer into which the data is read.
off - The start offset within buf to read data into.
len - The maximum number of bytes read into buf.
Returns:
The total number of bytes read into the array, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2005-04-11

getInput

public java.io.InputStream getInput()
Retrieve the underlying input stream for this output stream.

Note that this method is not specified by the standard FilterInputStream library class, but is provided as a convenient enhancement.

Returns:
The underlying input stream for this stream, or null if it has been closed.
Since:
1.1, 2005-04-11

decode

public void decode(java.io.OutputStream out)
            throws java.io.IOException
Read and convert the contents of an input stream. This method is equivalent to the call: decode(in, 0).

Parameters:
out - An output stream to which is written the decoded binary 8-bit octet data bytes.
Throws:
java.io.IOException - Thrown if an I/O (read or write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if in is null.
Since:
1.3, 2009-02-03
See Also:
decode()

decode

public void decode(java.io.OutputStream out,
                   int bufLen)
            throws java.io.IOException
Read and convert the contents of an input stream.

The entire contents of an input stream are read as base-64 encoded characters and converted into 8-bit octets, which are then written to this output stream.

Note that the output stream is flushed after the converted data is written, but is not closed, i.e., method flush() is called but close() is not.

Parameters:
out - An output stream to which is written the decoded binary 8-bit octet data bytes.
bufLen - Specifies the size, in bytes, of the internal buffer to use for reading data from the input stream. Specifying a value less than 4 causes a default size of 64K (65,536) to be used.
Throws:
java.io.IOException - Thrown if an I/O (read or write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if in is null.
Since:
1.3, 2009-02-03

readPending

protected void readPending()
                    throws java.io.IOException
Read base-64 encoded data bytes from the input stream and convert them into data bytes (octets).

Note that this method should only be called if m_outPos is greater than m_outLen, indicating that the output queue has been exhausted.

Throws:
java.io.IOException - Thrown if an I/O (read) error occurs.
Since:
1.1, 2005-04-11