tribble.io
Class Base64DecoderOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by tribble.io.Base64DecoderOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class Base64DecoderOutputStream
extends java.io.FilterOutputStream

Base-64 decoding output stream.

Provides an I/O stream that decodes base-64 characters into binary data. Printable 8-bit characters that encode data as base-64 (a.k.a. radix-64) ASCII characters are converted into 8-bit bytes (octets) as they are written to the output stream.

See Base64Decoder for more information about base-64 decoding.

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

Since:
2005-04-08
Version:
$Revision: 1.6 $ $Date: 2009/02/04 03:37:07 $
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:
Base64DecoderInputStream, Base64EncoderOutputStream, Base64Decoder

Field Summary
protected  byte[] m_inBuf
          Pending input queue, composed of groups of 8-bit octets.
protected  short m_inLen
          Pending input queue length.
protected  byte[] m_outBuf
          Output queue, composed of groups of 6-bit sextets.
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
Base64DecoderOutputStream(java.io.OutputStream out)
          Constructor.
 
Method Summary
 void close()
          Flush and close this output stream.
 void decode(java.io.InputStream in)
          Read and convert the contents of an input stream.
 void decode(java.io.InputStream in, int bufLen)
          Read and convert the contents of an 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 finish()
          Complete all pending output to this output stream.
 void flush()
          Flush all pending output to this output stream.
 java.io.OutputStream getOutput()
          Retrieve the underlying output stream for this output stream.
static void main(java.lang.String[] args)
          Test driver.
 void write(byte[] data, int off, int len)
          Write a set of bytes to this output stream.
 void write(int c)
          Write a byte to this output stream.
protected  void writePending()
          Convert pending input data bytes into base-64 character bytes and write them to the output stream.
 
Methods inherited from class java.io.FilterOutputStream
write
 
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, composed of groups of 8-bit octets.


m_outBuf

protected byte[] m_outBuf
Output queue, composed of groups of 6-bit sextets.


m_inLen

protected short m_inLen
Pending input queue length.

Constructor Detail

Base64DecoderOutputStream

public Base64DecoderOutputStream(java.io.OutputStream out)
Constructor.

Parameters:
out - The underlying output stream for this output stream.
Throws:
java.lang.NullPointerException - (unchecked) Thrown if out is null.
Since:
1.1, 2005-04-04
Method Detail

main

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

Usage

java tribble.io.Base64DecoderOutputStream [-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-04

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.6, 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 finished and flushed after the converted data is written, but is not closed, i.e., methods finish() and flush() are 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.6, 2009-02-03
See Also:
decode()

finish

public void finish()
            throws java.io.IOException
Complete all pending output to this output stream.

All pending base-64 character output is completed.

Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if the underlying output stream is closed.
Since:
1.4, 2005-07-18
See Also:
flush()

flush

public void flush()
           throws java.io.IOException
Flush all pending output to this output stream.

Any pending base-64 character output is written to the underlying output stream, then the underlying output stream itself is flushed.

Note that output characters may still remain pending after this operation if the last bytes written to this stream do not constitute a complete 24-bit octet group. Use the finish() method to force completion of the output stream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.FilterOutputStream
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if the underlying output stream is closed.
Since:
1.1, 2005-04-04
See Also:
finish()

close

public void close()
           throws java.io.IOException
Flush and close this output stream.

Any pending base-64 character output is completed, and the final padding characters (if any) are written to the underlying output stream.

Subsequent attempts to write to this output stream will cause exceptions to be thrown.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
Since:
1.1, 2005-04-04
See Also:
finish(), flush()

write

public void write(int c)
           throws java.io.IOException
Write a byte to this output stream.

The 8-bit byte is converted into its corresponding base-64 character encoding before being written to the underlying output stream.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
c - A character code. Note that only the lower 8 bits are written to the underlying output stream, and the upper 24 bits are ignored.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if the underlying output stream is closed.
Since:
1.1, 2005-04-04

write

public void write(byte[] data,
                  int off,
                  int len)
           throws java.io.IOException
Write a set of bytes to this output stream.

The 8-bit bytes are converted into their corresponding base-64 character encoding before being written to the underlying output stream.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
data - An array of bytes to write.
off - Index of the first byte within data to write.
len - Number of bytes from data to write.
Throws:
java.io.IOException - Thrown if an I/O (write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if the underlying output stream is closed.
java.lang.IndexOutOfBoundsException - (unchecked) Thrown if off or len specify a negative offset or length.
Since:
1.1, 2005-04-04

getOutput

public java.io.OutputStream getOutput()
Retrieve the underlying output stream for this output stream.

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

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

decode

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

Parameters:
in - An input stream containing base-64 encoded character data to be converted into binary 8-bit octet data.
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, 2005-04-16
See Also:
decode()

decode

public void decode(java.io.InputStream in,
                   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 finished and flushed after the converted data is written, but is not closed, i.e., methods finish() and flush() are 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.
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, 2005-04-16

writePending

protected void writePending()
                     throws java.io.IOException
Convert pending input data bytes into base-64 character bytes and write them to the output stream.

Throws:
java.io.IOException - Thrown if an I/O (write) error occurs, or if a base-64 character conversion error occurs.
Since:
1.1, 2005-04-04