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.

Internally it uses a RandomAccessFile to cache and seek the data. Since it must be able to random seek it can't be (easily) buffered internally. External programs should wrap this class with a BufferedInputStream to improve performances (especially if int read() method is called).

Since:
1.2.0
Version:
$Id: RandomAccessInputStream.java 527 2014-02-24 19:29:50Z gabriele.contini@gmail.com $
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
          Position in the stream when the mark() was issued.
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)
           Constructor for RandomAccessInputStream.
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)
           Constructor for RandomAccessInputStream.
 
Method Summary
 int available()
          
protected  void closeOnce()
          closeOnce
 Store getStore()
          Return the underlying store where the cache of data is kept.
protected  int innerRead(byte[] b, int off, int len)
          innerRead
 void mark(int readLimit)
          
 boolean markSupported()
           Overrides the markSupported() method of the InputStream class.
 void reset()
          
 void seek(long position)
           Reposition the read pointer in the stream.
 void setStore(Store store)
           Setter for the field 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

markLimit

protected long markLimit

markPosition

protected long markPosition
Position in the stream when the mark() was issued.


randomAccessIsPosition

protected long randomAccessIsPosition
Position of read cursor in the RandomAccessInputStream.


sourcePosition

protected long sourcePosition
Position of reading in the source stream.

Constructor Detail

RandomAccessInputStream

public RandomAccessInputStream(InputStream source)

Constructor for RandomAccessInputStream.

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)

Constructor for RandomAccessInputStream.

Parameters:
source - The underlying input stream.
store - a SeekableStore object.
Method Detail

available

public int available()
              throws IOException

Overrides:
available in class InputStream
Throws:
IOException

closeOnce

protected void closeOnce()
                  throws IOException

closeOnce

Specified by:
closeOnce in class AbstractInputStreamWrapper
Throws:
IOException - if any error occurs reading the bytes.

getStore

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

Returns:
The underlying store that caches data.

innerRead

protected int innerRead(byte[] b,
                        int off,
                        int len)
                 throws IOException

innerRead

Specified by:
innerRead in class AbstractInputStreamWrapper
Parameters:
b - an array of byte.
off - a int.
len - a int.
Returns:
a int.
Throws:
IOException - if any error occurs reading the bytes.

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
See Also:
reset(), InputStream.reset()

markSupported

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

Overrides:
markSupported in class InputStream
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

Reposition the read pointer in the stream. Next read will start from the position passed as argument.

Parameters:
position - a long indicating the position in the stream we want to go.
Throws:
IOException - throw if any IOException occurs reading the underlying stream or if it is attempted a seek to a position greater to the effective number of bytes in the stream.

setStore

public void setStore(Store store)

Setter for the field store.

Parameters:
store - a Store object.

toString

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

Overrides:
toString in class Object


Copyright © 2008–2014. All rights reserved.