org.exist.validation.internal
Class BlockingInputStream

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

public class BlockingInputStream
extends InputStream

BlockingOutputStream is a combination of an output stream and an input 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.


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()
          Equivalent of the close() method of an output stream.
 void closeInputStream()
          Closes this input stream and releases any system resources associated with the stream.
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out.
 int free()
          The number of bytes that can be written to this output stream without blocking by the next caller of a method for this output stream.
 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.
 void write(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 void write(int b)
          Writes the specified byte to this output stream.
 
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

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.

closeInputStream

public void closeInputStream()
                      throws IOException
Closes this input stream and releases any system resources associated with the stream.

Throws:
IOException - if an I/O error occurs.

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

write

public void write(int b)
           throws IOException
Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters:
b - the byte.
Throws:
IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

close

public void close()
           throws IOException
Equivalent of the close() method of an output stream. Renamed to solve the name clash with the close() method of the input stream also implemented by this class. Closes this output stream and releases any system resources associated with this stream. A closed stream cannot perform output operations and cannot be reopened.

This method blocks its caller until all bytes remaining in the buffer are read from the buffer by the receiving threads or an exception occurs.

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException - if an I/O error occurs.

flush

public void flush()
           throws IOException
Flushes this output stream and forces any buffered output bytes to be written out.

This methods blocks its caller until all buffered bytes are actually read by the consuming threads.

Throws:
IOException - if an I/O error occurs.

free

public int free()
The number of bytes that can be written to this output stream without blocking by the next caller of a method for this output stream.

Returns:
the number of bytes that can be written to this output stream without blocking.
Throws:
IOException - if an I/O error occurs.


Copyright (C) Wolfgang Meier. All rights reserved.