Class BlockingInputStream

java.lang.Object
java.io.InputStream
org.jgroups.util.BlockingInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class BlockingInputStream extends InputStream
Bounded input stream. A consumer reads bytes until the end of the stream is reached, or the input stream is closed. The producer writes bytes to the tail and blocks if the capacity has been reached (until the consumer reads more bytes).

This class is for only 1 producer and 1 consumer; multiple producers/consumers will most likely yield incorrect results !

Note that the implementation of this class is optimized for reading and adding a few K at a time; performance will be suboptimal if single bytes are added and read.

Since:
2.12.2
  • Field Details

    • closed

      protected boolean closed
      Set to true when close() is called
    • buf

      protected final byte[] buf
      The transfer buffer
    • read_pos

      protected int read_pos
      Index into buf at which the next bytes will be read. Has to be between 0 and buf.length-1
    • write_pos

      protected int write_pos
      Index into buf at which bytes will be written. Has to be between 0 and buf.length-1
    • lock

      protected final Lock lock
    • not_full

      protected final Condition not_full
      Signalled when the buf becomes 'writeable'; ie. is not full anymore
    • not_empty

      protected final Condition not_empty
      Signalled when the buf becomes 'readable'; ie. is not empty anymore
  • Constructor Details

    • BlockingInputStream

      public BlockingInputStream()
    • BlockingInputStream

      public BlockingInputStream(int capacity)
  • Method Details

    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • write

      public void write(byte[] buf) throws IOException
      Appends bytes to the end of the stream
      Parameters:
      buf -
      Throws:
      IOException
      See Also:
    • write

      public void write(byte[] buf, int offset, int length) throws IOException
      Appends bytes to the end of the stream. If the number of bytes to be written is greater than the remaining capacity, write() will block until the bytes can be added, or the stream is closed.

      This method will try to append partial buffers to the stream, e.g. if the remaining capacity is 2K, but the length of the buffer is 5K, 2K will be written and then write() will block until the remaining 3K can be added.

      Parameters:
      buf - The buffer to be added to the end of the stream
      offset - The offset within buf at which bytes are read
      length - The number of bytes to be added
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • capacity

      public int capacity()
    • close

      public void close() throws IOException
      Closes the stream. Writes to a closed stream will fail, reads will successfully read the bytes that are already in the buffer and then return -1 (EOF)
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • isClosed

      public boolean isClosed()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • size

      protected int size()
    • remaining

      protected int remaining()
    • compact

      protected void compact()
      Moves the bytes between [read_pos and write_pos] read_pos bytes to the left, such that the new read_pos is 0 and the write_pos is write_pos - read_pos. Lock must be held.
    • sanityCheck

      protected static void sanityCheck(byte[] buf, int offset, int length)
      Verifies that length doesn't exceed a buffer's length
      Parameters:
      buf -
      offset -
      length -