uk.org.ogsadai.common
Class Base64Reader

java.lang.Object
  |
  +--java.io.Reader
        |
        +--uk.org.ogsadai.common.Base64Reader

public class Base64Reader
extends java.io.Reader

A java.io.Reader that wraps a java.io.InputStream and Base64 encodes its contents.

Author:
The OGSA-DAI Project Team

Field Summary
private static int BUFFER_SIZE
           
private static java.lang.String COPYRIGHT_NOTICE
           
private static DAILogger LOG
           
private  char[] mBase64Data
           
private  byte[] mByteBuffer
           
private  boolean mClosed
           
private  boolean mEncoded
           
private  java.io.InputStream mInputStream
           
private  int mRemaining
           
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
Base64Reader(java.io.InputStream input)
          Constructs a Base64Reader that wraps the specified InputStream.
 
Method Summary
 void close()
           
 int read(char[] cbuf, int off, int len)
          Read Base64 encoded characters into a portion of an array.
 java.lang.String readAll()
          Reads and Base64 encodes the entire contents of the InputStream.
private  void readAndEncode()
          Reads from the wrapped InputStream into the buffer, then encodes to Base64.
 
Methods inherited from class java.io.Reader
mark, markSupported, read, read, ready, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
See Also:
Constant Field Values

LOG

private static final DAILogger LOG

BUFFER_SIZE

private static final int BUFFER_SIZE
See Also:
Constant Field Values

mInputStream

private final java.io.InputStream mInputStream

mByteBuffer

private byte[] mByteBuffer

mBase64Data

private char[] mBase64Data

mEncoded

private boolean mEncoded

mRemaining

private int mRemaining

mClosed

private boolean mClosed
Constructor Detail

Base64Reader

public Base64Reader(java.io.InputStream input)
             throws java.lang.IllegalArgumentException,
                    OutOfMemoryException
Constructs a Base64Reader that wraps the specified InputStream. Data read from the input stream will be Base64 encoded.

Default buffer size is 4096 * 3 bytes.

Parameters:
input - The InputStream to wrap and encode.
Throws:
java.lang.IllegalArgumentException - if input is null.
OutOfMemoryException - if there is no memory available.
Method Detail

read

public int read(char[] cbuf,
                int off,
                int len)
         throws java.io.IOException,
                java.lang.IllegalArgumentException
Read Base64 encoded characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

Specified by:
read in class java.io.Reader
Parameters:
cbuf - Destination buffer. Must be able to hold len.
off - Offset at which to start storing characters. Must be >= 0 and < size of cbuf.
len - Maximum number of characters to read. Must be >= 1 and < size of cbuf - off.
Returns:
The number of characters read, or -1 if the end of the stream has been reached.
Throws:
java.io.IOException - if the reader has already been closed or there is a problem when reading from the InputStream.
java.lang.IllegalArgumentException - if the cbuf is null or if off or len contain unsuitable values.
See Also:
Reader.read(char[], int, int)

readAndEncode

private void readAndEncode()
                    throws java.io.IOException
Reads from the wrapped InputStream into the buffer, then encodes to Base64. Called whenever there is no more encoded data to be read.

Throws:
java.io.IOException - if a problem is encountered in reading the data from the InputStream.

close

public void close()
Specified by:
close in class java.io.Reader

readAll

public java.lang.String readAll()
                         throws java.io.IOException,
                                OutOfMemoryException
Reads and Base64 encodes the entire contents of the InputStream. Note that this method may carry a significant memory overhead because all the input data will be encoded and stored in memory in its entirety.

Returns:
a String containing the Base64 encoded data.
Throws:
java.io.IOException - if the reader has already been closed or there is a problem when reading from the InputStream.
OutOfMemoryException - if there is no memory available.