com.mindbright.util
Class InputStreamPipe

java.lang.Object
  extended byjava.io.InputStream
      extended bycom.mindbright.util.InputStreamPipe

public final class InputStreamPipe
extends java.io.InputStream

An input stream pipe should be connected to an output stream pipe; the input stream pipe then provides whatever data bytes are written to the output stream pipe. This is very close to the PipedInputStream and PipedOutputStream. The main difference is that there is a timeout in the write code so that any waiting write will be aborted if the pipe is closed.

The input and output pipes are connected via a circular buffer which decouples write and read operations.

See Also:
OutputStreamPipe, PipedInputStream, PipedOutputStream

Constructor Summary
InputStreamPipe()
          Create an unconnected InputStreamPipe with the default circular buffer size (8192 bytes).
InputStreamPipe(int bufferSize)
          Create an unconnected InputStreamPipe with the given circular buffer size.
InputStreamPipe(OutputStreamPipe source)
          Create an InputStreamPipe with the default circular buffer size (8192 bytes) which is connected to the given output stream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read without blocking.
 void close()
          Close this stream and abort any ongoing write operation in the corresponding OutputStreamPipe.
 void connect(OutputStreamPipe source)
          Causes this InputStreamPipe to be connected to the given OutputStreamPipe.
protected  void eof()
          Signal that this stream is closing.
 void flush()
          Notify all instances waiting on this stream.
protected  void put(byte[] buf, int off, int len)
          Put data into this input stream pipe.
protected  void put(int b)
          Put a byte of data into this input stream pipe.
 int read()
          Read a byte of data from the pipe.
 int read(byte[] buf, int off, int len)
          Read data from the pipe.
 
Methods inherited from class java.io.InputStream
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

InputStreamPipe

public InputStreamPipe(int bufferSize)
Create an unconnected InputStreamPipe with the given circular buffer size.

Parameters:
bufferSize - size of circular buffer

InputStreamPipe

public InputStreamPipe()
Create an unconnected InputStreamPipe with the default circular buffer size (8192 bytes).


InputStreamPipe

public InputStreamPipe(OutputStreamPipe source)
                throws java.io.IOException
Create an InputStreamPipe with the default circular buffer size (8192 bytes) which is connected to the given output stream.

Parameters:
source - the output stream to connect to
Method Detail

connect

public void connect(OutputStreamPipe source)
             throws java.io.IOException
Causes this InputStreamPipe to be connected to the given OutputStreamPipe.

Parameters:
source - the stream to connect to
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Read a byte of data from the pipe. This call will wait for data to become available if needed.

Returns:
the next byte waiting to be read
Throws:
java.io.IOException

read

public int read(byte[] buf,
                int off,
                int len)
         throws java.io.IOException
Read data from the pipe. This call will wait for data to become available if needed.

Parameters:
buf - buffer to store read data into
off - where in the buffer the first byte should be stored
len - how many bytes of data to read
Returns:
the number of bytes actually read
Throws:
java.io.IOException

available

public int available()
Returns the number of bytes that can be read without blocking.

Returns:
the number of bytes that can be read without blocking.

close

public void close()
           throws java.io.IOException
Close this stream and abort any ongoing write operation in the corresponding OutputStreamPipe.

Throws:
java.io.IOException

flush

public void flush()
Notify all instances waiting on this stream.


put

protected void put(int b)
            throws java.io.IOException
Put a byte of data into this input stream pipe. If the buffer is full then this call will wait until room becomes available or the stream is closed.

Parameters:
b - the byte of data to put
Throws:
java.io.IOException

put

protected void put(byte[] buf,
                   int off,
                   int len)
            throws java.io.IOException
Put data into this input stream pipe. If the buffer is full then this call will wait until room becomes available or the stream is closed. The function handles the case where the amount of data to write is larger than the circular buffer.

Parameters:
buf - array holding data to put
off - offset of first byte to put
len - number of bytes to put
Throws:
java.io.IOException

eof

protected void eof()
Signal that this stream is closing.