com.gc.iotools.stream.is
Class RandomAccessInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.gc.iotools.stream.base.AbstractInputStreamWrapper
          extended by com.gc.iotools.stream.is.RandomAccessInputStream
All Implemented Interfaces:
Closeable

public class RandomAccessInputStream
extends AbstractInputStreamWrapper

A RandomAccessInputStream adds functionality to another input stream-namely, the ability to buffer the input, allowing it to be read multiple times, and to support the mark and reset methods.

When the RandomAccessInputStream is created, an internal Store is created. As bytes from the stream are read or skipped, the internal store is refilled as necessary from the source input stream. The implementation of store can be changed to fit the application needs: cache on disk rather than in memory. The default store implementation caches 64K in memory and then write the content on disk.

It also adds the functionality of marking an InputStream without specifying a mark length, thus allowing a reset after an indefinite length of bytes has been read. Check the #mark(int)) javadoc for details.

Since:
1.2.0
Author:
dvd.smnt
See Also:
Store

Field Summary
static int DEFAULT_DISK_TRHESHOLD
          Default size for passing from memory allocation to disk allocation for the buffer.
protected  long markLimit
           
protected  long markPosition
           
protected  long randomAccessIsPosition
          Position of read cursor in the RandomAccessInputStream.
protected  long sourcePosition
          Position of reading in the source stream.
 
Fields inherited from class com.gc.iotools.stream.base.AbstractInputStreamWrapper
closeCalled, source
 
Constructor Summary
RandomAccessInputStream(InputStream source)
           
RandomAccessInputStream(InputStream source, int threshold)
           Creates a RandomAccessInputStream with the specified treshold, and saves its argument, the input stream source, for later use.
RandomAccessInputStream(InputStream source, SeekableStore store)
           
 
Method Summary
 int available()
          
protected  void closeOnce()
           
 Store getStore()
          Return the underlying store where the cache of data is kept.
protected  int innerRead(byte[] b, int off, int len)
           
 void mark(int readLimit)
           Marks the current position in this input stream.
 boolean markSupported()
          Overrides the markSupported() method of the InputStream class.
 void reset()
           Repositions this stream to the position at the time the mark method was last called on this input stream.
 void seek(long position)
          .
 void setStore(Store store)
           
 String toString()
          Provides a String representation of the state of the stream for debugging purposes.
 
Methods inherited from class com.gc.iotools.stream.base.AbstractInputStreamWrapper
close, read, read, skip
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_DISK_TRHESHOLD

public static final int DEFAULT_DISK_TRHESHOLD
Default size for passing from memory allocation to disk allocation for the buffer.

See Also:
Constant Field Values

sourcePosition

protected long sourcePosition
Position of reading in the source stream.


randomAccessIsPosition

protected long randomAccessIsPosition
Position of read cursor in the RandomAccessInputStream.


markPosition

protected long markPosition

markLimit

protected long markLimit
Constructor Detail

RandomAccessInputStream

public RandomAccessInputStream(InputStream source)
Parameters:
source - The underlying input stream.

RandomAccessInputStream

public RandomAccessInputStream(InputStream source,
                               int threshold)

Creates a RandomAccessInputStream with the specified treshold, and saves its argument, the input stream source, for later use.

When data read under threshold size treshold is kept into memory. Over this size it is placed on disk.

Parameters:
source - The underlying input stream.
threshold - Maximum number of bytes to keep into memory.
See Also:
ThresholdStore

RandomAccessInputStream

public RandomAccessInputStream(InputStream source,
                               SeekableStore store)
Parameters:
source - The underlying input stream.
store -
Method Detail

available

public int available()
              throws IOException

Overrides:
available in class InputStream
Throws:
IOException

getStore

public Store getStore()
Return the underlying store where the cache of data is kept.

Returns:
The underlying store that caches data.

mark

public void mark(int readLimit)

Marks the current position in this input stream. A subsequent call to the reset() method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

This method extends the original behavior of the class InputStream allowing to use indefinite marking.

Overrides:
mark in class InputStream
Parameters:
readLimit - the maximum limit of bytes that can be read before the mark position becomes invalid. If negative allows indefinite marking (the mark never becomes invalid).
See Also:
reset(), InputStream.reset()

markSupported

public boolean markSupported()
Overrides the markSupported() method of the InputStream class.

Overrides:
markSupported in class InputStream
Returns:
Always returns true for this kind of stream.
See Also:
InputStream.markSupported();

reset

public void reset()
           throws IOException

Repositions this stream to the position at the time the mark method was last called on this input stream.

After invoking mark it can be invoked multiple times and it always reset the stream at the previously marked position.

Overrides:
reset in class InputStream
Throws:
IOException - if this stream has not been marked or if the mark has been invalidated.
See Also:
mark(int), InputStream.reset()

seek

public void seek(long position)
          throws IOException
.

Throws:
IOException

setStore

public void setStore(Store store)

toString

public String toString()
Provides a String representation of the state of the stream for debugging purposes.

Overrides:
toString in class Object
Returns:
String that represent the state.

closeOnce

protected void closeOnce()
                  throws IOException
Specified by:
closeOnce in class AbstractInputStreamWrapper
Throws:
IOException

innerRead

protected int innerRead(byte[] b,
                        int off,
                        int len)
                 throws IOException
Specified by:
innerRead in class AbstractInputStreamWrapper
Throws:
IOException


Copyright © 2008-2009. All Rights Reserved.