edu.rice.cs.util
Class JoinInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by edu.rice.cs.util.JoinInputStream
All Implemented Interfaces:
Closeable

public class JoinInputStream
extends InputStream


Field Summary
protected  int currentReader
           
static int defaultBufferSize
          Default size of read buffer.
protected  int nReaders
           
protected  int openedStreams
           
protected  edu.rice.cs.util.ReaderThread[] reader
           
 
Constructor Summary
JoinInputStream(InputStream[] streams, int bufferSize)
          Create stream joining streams specified in the array.
JoinInputStream(InputStream one, InputStream two)
          Create stream joining two specified streams.
JoinInputStream(InputStream one, InputStream two, int bufferSize)
          Create stream joining two specified streams.
 
Method Summary
 void close()
          Close all attached input streams and stop their listener threads.
 int getStreamIndex()
          Get index of thread from which data was retrieved in last read operation.
 int read()
          Reads the next byte of data from one of input streams.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from one of input streams into an array of bytes.
 
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
 

Field Detail

defaultBufferSize

public static int defaultBufferSize
Default size of read buffer.


nReaders

protected int nReaders

reader

protected edu.rice.cs.util.ReaderThread[] reader

currentReader

protected int currentReader

openedStreams

protected int openedStreams
Constructor Detail

JoinInputStream

public JoinInputStream(InputStream[] streams,
                       int bufferSize)
Create stream joining streams specified in the array.

Parameters:
streams - array with input streams which should be joined
bufferSize - specifies size of read buffer

JoinInputStream

public JoinInputStream(InputStream one,
                       InputStream two,
                       int bufferSize)
Create stream joining two specified streams.

Parameters:
one - first input stream to be merged
two - second input stream to be merged
bufferSize - specifies size of read buffer

JoinInputStream

public JoinInputStream(InputStream one,
                       InputStream two)
Create stream joining two specified streams.

Parameters:
one - first input stream to be merged
two - second input stream to be merged
Method Detail

read

public int read()
         throws IOException
Reads the next byte of data from one of input streams. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected or an exception is catched.

Specified by:
read in class InputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Reads up to len bytes of data from one of input streams into an array of bytes. This method blocks until some input is available. If the first argument is null, up to len bytes are read and discarded.

The read method of InputStream reads a single byte at a time using the read method of zero arguments to fill in the array. Subclasses are encouraged to provide a more efficient implementation of this method.

Overrides:
read in class InputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the maximum number of bytes 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:
IOException - if an I/O error occurs.

close

public void close()
           throws IOException
Close all attached input streams and stop their listener threads.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

getStreamIndex

public final int getStreamIndex()
Get index of thread from which data was retrieved in last read operation. Indices are started from 0.

Returns:
index of thread from which data was taken in last read operation.