tribble.io
Class Base64EncoderInputStream

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

public class Base64EncoderInputStream
extends java.io.FilterInputStream

Base-64 encoding input stream.

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

See Base64Encoder for more information about base-64 encoding.

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

Since:
2005-04-13
Version:
$Revision: 1.3 $ $Date: 2009/02/04 03:41:26 $
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, 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  short m_newlineStyle
          Newline output style.
protected  byte[] m_outBuf
          Output queue.
protected  short m_outLen
          Current output byte queue length.
protected  short m_outPos
          Next output byte queue index.
protected static byte[] NEWLINE
          Platform-specific newline character sequence.
static short NEWLINE_BYTE
          Newline output style: Single byte ('\n').
static short NEWLINE_NATIVE
          Newline output style: Native platform character sequence.
static short NEWLINE_NONE
          Newline output style: No newline sequences.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
Base64EncoderInputStream(java.io.InputStream in)
          Constructor.
 
Method Summary
 void close()
          Close this input stream.
static void encode(java.io.InputStream in, java.io.OutputStream out)
          Read and convert the contents of an input stream.
static void encode(java.io.InputStream in, java.io.OutputStream out, int bufLen)
          Read and convert the contents of an input stream.
 void encode(java.io.OutputStream out)
          Read and convert the contents of this input stream.
 void encode(java.io.OutputStream out, int bufLen)
          Read and convert the contents of this 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 8-bit data bytes (octets) from the input stream and convert them into base-64 encoded character bytes.
 int setNewline(int style)
          Establish the handling of newline sequences for this output stream.
 
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

NEWLINE_NONE

public static final short NEWLINE_NONE
Newline output style: No newline sequences.

See Also:
Constant Field Values

NEWLINE_BYTE

public static final short NEWLINE_BYTE
Newline output style: Single byte ('\n').

See Also:
Constant Field Values

NEWLINE_NATIVE

public static final short NEWLINE_NATIVE
Newline output style: Native platform character sequence.

See Also:
Constant Field Values

NEWLINE

protected static final byte[] NEWLINE
Platform-specific newline character sequence.


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_newlineStyle

protected short m_newlineStyle
Newline output style.


m_eof

protected boolean m_eof
End of input stream reached.

Constructor Detail

Base64EncoderInputStream

public Base64EncoderInputStream(java.io.InputStream 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 input files and write them in base-64 output form.

Usage

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

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

encode

public static void encode(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: encode(in, out, 0).

Parameters:
in - An input stream containing 8-bit octet data to be converted into base-64 character output.
out - An output stream to which the converted base-64 character output is to be written.
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:
encode()

encode

public static void encode(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 8-bit octets and converted into base-64 encoded characters, 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 8-bit octet data to be converted into base-64 character output.
out - An output stream to which the converted base-64 character output is to be written.
bufLen - Specifies the size, in bytes, of the internal buffer to use for reading data from the input stream. Specifying a value less than 3 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:
encode()

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.

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

Overrides:
read in class java.io.FilterInputStream
Returns:
An ASCII base-64 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. Each 8-bit binary code (octet) is converted into its corresponding base-64 character encoding after being read from the underlying input stream.
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

encode

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

Parameters:
out - An output stream to which the converted base-64 character output is to be written.
Throws:
java.io.IOException - Thrown if an I/O (read or write) error occurs.
java.lang.NullPointerException - (unchecked) Thrown if out is null.
Since:
1.3, 2009-02-03
See Also:
encode()

encode

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

The entire contents of this input stream are read as 8-bit octets and converted into base-64 encoded characters, 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:
out - An output stream to which the converted base-64 character output is to be written.
bufLen - Specifies the size, in bytes, of the internal buffer to use for reading data from the input stream. Specifying a value less than 3 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 out is null.
Since:
1.3, 2009-02-03

setNewline

public int setNewline(int style)
Establish the handling of newline sequences for this output stream.

This method provides a means for "prettying up" the character input of this stream.

The default style is NEWLINE_NATIVE.

Parameters:
style - Specifies the way that newlines are inserted into the input stream. This value must be one of the NEWLINE_XXX constants:
NEWLINE_NONE
No newlines are inserted into the input.
NEWLINE_BYTE
A single newline character byte ('\n') is inserted after every 64 bytes of input.
NEWLINE_NATIVE
The native character sequence is inserted after every 64 bytes of input. The sequence is defined by the "line.separator" system property (see System.getProperty()).
Returns:
The previous setting.
Throws:
java.lang.IllegalArgumentException - (unchecked) Thrown if style is not a valid NEWLINE_XXX constant.
Since:
1.1, 2005-04-11

readPending

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

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