jsync
Class JoinInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjsync.JoinInputStream

public class JoinInputStream
extends java.io.InputStream

Class multiplexing input from several streams. This class merge several input streams into single input stream. Size of input buffer determines maximal amount of data, which can be read from one stream without interleaving with data from other streams.


Field Summary
static int defaultBufferSize
          Default size of read buffer.
 
Constructor Summary
JoinInputStream(java.io.InputStream[] streams, int bufferSize)
          Create stream joining streams specified in the array.
JoinInputStream(java.io.InputStream one, java.io.InputStream two)
          Create stream joining two specified streams.
JoinInputStream(java.io.InputStream one, java.io.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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultBufferSize

public static int defaultBufferSize
Default size of read buffer.

Constructor Detail

JoinInputStream

public JoinInputStream(java.io.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(java.io.InputStream one,
                       java.io.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(java.io.InputStream one,
                       java.io.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 java.io.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.

Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs.

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.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.

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:
java.io.IOException - if an I/O error occurs.

close

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

Throws:
java.io.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.