com.gc.iotools.stream.is
Class ReadAheadInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.gc.iotools.stream.is.InputStreamFromOutputStream<Void>
          extended by com.gc.iotools.stream.is.ReadAheadInputStream
All Implemented Interfaces:
Closeable

public class ReadAheadInputStream
extends InputStreamFromOutputStream<Void>

This class tries to read the InputStream passed as a parameter in a memory buffer, thus improving the reading performances.

This can speed up reading when the time spent in reading from the stream is same order as the time spent in elaborating the stream, because it decouples the reading process and the elaboration in two different threads putting them in parallel.

Sample Usage:

 InputStream source = ... some slow InputStream.
 InputStream fastIs = new ReadAheadInputStream(source);
 //use here fastIs instead of the source InputStream
 ...
 fastIs.close();
 

Since:
1.2.6
Version:
$Id: ReadAheadInputStream.java 463 2011-01-21 23:54:17Z dvd.smnt@gmail.com $
Author:
dvd.smnt

Constructor Summary
ReadAheadInputStream(InputStream source)
          Constructor for ReadAheadInputStream.
ReadAheadInputStream(InputStream source, int bufferSize)
          Constructor for ReadAheadInputStream.
ReadAheadInputStream(InputStream source, int bufferSize, ExecutionModel executionModel)
          Constructor for ReadAheadInputStream.
ReadAheadInputStream(InputStream source, int bufferSize, ExecutorService executorService)
          Constructor for ReadAheadInputStream.
 
Method Summary
protected  Void produce(OutputStream sink)
           This method must be implemented by the user of this class to produce the data that must be read from the external InputStream.
 
Methods inherited from class com.gc.iotools.stream.is.InputStreamFromOutputStream
afterClose, close, getActiveThreadNames, getResult, read, read, setDefaultBufferSize, setDefaultPipeSize
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReadAheadInputStream

public ReadAheadInputStream(InputStream source)

Constructor for ReadAheadInputStream.

Parameters:
source - a InputStream object.

ReadAheadInputStream

public ReadAheadInputStream(InputStream source,
                            int bufferSize)

Constructor for ReadAheadInputStream.

Parameters:
source - a InputStream object.
bufferSize - a int.

ReadAheadInputStream

public ReadAheadInputStream(InputStream source,
                            int bufferSize,
                            ExecutionModel executionModel)

Constructor for ReadAheadInputStream.

Parameters:
source - a InputStream object.
bufferSize - a int.
executionModel - a ExecutionModel object.

ReadAheadInputStream

public ReadAheadInputStream(InputStream source,
                            int bufferSize,
                            ExecutorService executorService)

Constructor for ReadAheadInputStream.

Parameters:
source - a InputStream object.
bufferSize - a int.
executorService - a ExecutorService object.
Method Detail

produce

protected Void produce(OutputStream sink)
                throws Exception

This method must be implemented by the user of this class to produce the data that must be read from the external InputStream.

Special care must be paid passing arguments to this method or setting global fields because it is executed in another thread.

The right way to set a field variable is to return a value in the produceand retrieve it in the getResult().

Specified by:
produce in class InputStreamFromOutputStream<Void>
Parameters:
sink - the implementing class should write its data to this stream.
Returns:
The implementing class can use this to return a result of data production. The result will be available through the method InputStreamFromOutputStream.getResult().
Throws:
Exception - the exception eventually thrown by the implementing class is returned by the InputStreamFromOutputStream.read() methods.
See Also:
InputStreamFromOutputStream.getResult()


Copyright © 2008-2011. All Rights Reserved.