org.exist.storage.io
Class BlockingInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.exist.storage.io.BlockingInputStream
All Implemented Interfaces:
Closeable

public class BlockingInputStream
extends InputStream

BlockingInputStream is a combination of an input stream and an output stream, connected through a (circular) buffer in memory. It is intended for coupling producer threads to consumer threads via a (byte) stream. When the buffer is full producer threads will be blocked until the buffer has some free space again. When the buffer is empty the consumer threads will be blocked until some bytes are available again. Closing of the output stream will block until the inputstream is closed. A special version of the close function enables the consumer threads to specify that an exception has occurred. This will cause producer calls to be unblocked and throw an IOException containing this exception as cause.

Author:
Chris Offerman

Constructor Summary
BlockingInputStream()
           
 
Method Summary
 int available()
          The number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 void close()
          Closes this input stream and releases the buffer associated with this stream.
 void close(Exception ex)
          Closes this input stream, specifying that an exception has occurred.
 BlockingOutputStream getOutputStream()
          BlockingOutputStream adapter for this BlockingInputStream.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes.
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockingInputStream

public BlockingInputStream()
Method Detail

getOutputStream

public BlockingOutputStream getOutputStream()
BlockingOutputStream adapter for this BlockingInputStream.


read

public int read()
         throws IOException
Reads the next byte of data from the input stream. 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 thrown.

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 the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

Overrides:
read in class InputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to 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.
NullPointerException - if b is null.

close

public void close()
Closes this input stream and releases the buffer associated with this stream.

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

close

public void close(Exception ex)
Closes this input stream, specifying that an exception has occurred. This will cause all producer calls to be unblocked and throw an ExistIOException with this exception as its cause. Releases the buffer associated with this stream. BlockingInputStream specific method.


available

public int available()
The number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Overrides:
available in class InputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
ExistIOException - if an I/O error occurs.


Copyright (C) Wolfgang Meier. All rights reserved.