ice.util.io
Class LazyCachedInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byice.util.io.LazyCachedInputStream

public class LazyCachedInputStream
extends InputStream

A LazyCachedInputStream adds functionality to another input stream, namely the ability to lazily cache the input. As bytes from the underlying stream are read, the internal byte cache is filled as necessary.

By using the methods read(int), read(int, byte[]) and read(int, byte[], int, int) one could even have a custom InputStream that uses a LazyCachedInputStream as underlying InputStream and administers its own position. In this way you could have more than one client of a LazyCachedInputStream and every client can read all the data from start to finish.

Since:
1.5
Version:
1.5
Author:
Jack van Ooststroom

Constructor Summary
LazyCachedInputStream(InputStream inputStream)
          Creates a LazyCachedInputStream and saves its argument, the specified inputStream, for later use.
LazyCachedInputStream(InputStream inputStream, int initialCacheSize)
          Creates a LazyCachedInputStream and saves its argument, the specified inputStream, for later use.
 
Method Summary
 int available()
           
 void close()
           
 int read()
           
 int read(byte[] bytes)
           
 int read(byte[] bytes, int offset, int length)
           
 int read(int position)
          Reads the next byte of data from the input stream from the specified position.
 int read(int position, byte[] bytes)
          Reads some number of bytes from the input stream from the specified position and stores them into the specified buffer array bytes.
 int read(int position, byte[] bytes, int offset, int length)
          Reads up to the specified length bytes of data from the input stream from the specified position into the specified array bytes at the specified offset.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LazyCachedInputStream

public LazyCachedInputStream(InputStream inputStream)
                      throws IllegalArgumentException

Creates a LazyCachedInputStream and saves its argument, the specified inputStream, for later use.

Parameters:
inputStream - the underlying input stream.
Throws:
IllegalArgumentException - if the specified inputStream is null.

LazyCachedInputStream

public LazyCachedInputStream(InputStream inputStream,
                             int initialCacheSize)
                      throws IllegalArgumentException

Creates a LazyCachedInputStream and saves its argument, the specified inputStream, for later use. An internal byte cache of initialCacheSize is created.

Parameters:
inputStream - the underlying input stream.
initialCacheSize - the initial cache size.
Throws:
IllegalArgumentException - if one of the following occurs:
  • the specified inputStream is null or
  • the specified initialCacheSize is less than 0.
Method Detail

available

public int available()
              throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Throws:
IOException

read

public int read()
         throws IOException
Throws:
IOException

read

public int read(byte[] bytes)
         throws IOException
Throws:
IOException

read

public int read(byte[] bytes,
                int offset,
                int length)
         throws IOException
Throws:
IOException

read

public int read(int position)
         throws IndexOutOfBoundsException,
                IOException

Reads the next byte of data from the input stream from the specified position.

Parameters:
position - the position to start reading from.
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IndexOutOfBoundsException - if the specified position is less than 0.
IOException - if an I/O error occurs.
See Also:
read()

read

public int read(int position,
                byte[] bytes)
         throws IllegalArgumentException,
                IndexOutOfBoundsException,
                IOException

Reads some number of bytes from the input stream from the specified position and stores them into the specified buffer array bytes.

Parameters:
position - the position to start reading from.
bytes - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IllegalArgumentException - if the specified bytes is null.
IndexOutOfBoundsException - if the specified position is less than 0.
IOException - if an I/O error occurs.
See Also:
read(byte[]), read(int, byte[], int, int)

read

public int read(int position,
                byte[] bytes,
                int offset,
                int length)
         throws IllegalArgumentException,
                IndexOutOfBoundsException,
                IOException

Reads up to the specified length bytes of data from the input stream from the specified position into the specified array bytes at the specified offset.

Parameters:
position - the position to start reading from.
bytes - the buffer into which the data is read.
offset - the start offset in array bytes at which the data is written.
length - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IllegalArgumentException - if the specified bytes is null.
IndexOutOfBoundsException - if one of the following occurs:
  • the specified position is less than 0,
  • the specified offset is either less than 0 or greater than bytes.length,
  • the specified length is less than 0 or
  • the offset + length is either less than 0 or greater than bytes.length.
IOException - if an I/O error occurs.
See Also:
read(byte[], int, int)