Class BufferInputStream

java.lang.Object
java.io.InputStream
org.eclipse.net4j.buffer.BufferInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, IBufferHandler
Direct Known Subclasses:
ChannelInputStream

public class BufferInputStream extends InputStream implements IBufferHandler
A buffer handler that defragments the passed buffers into a continuous byte sequence which is exposed as an input stream.

Reading is restricted to one reader thread at a time. Buffers may be supplied concurrently by producer threads, and abort(RuntimeException), close(), and restartTimeout() may be called during a read. Concurrent close never releases a buffer while the reader accesses its contents. Waiting readers are notified when a buffer arrives, the stream is aborted or closed, or the timeout is restarted. Abort is observed at buffer acquisition boundaries, without per-byte checks. This class does not guarantee fairness or producer priority.

A buffer's ownership transfers to this stream when handleBuffer(IBuffer) accepts it. The stream releases it after consumption or closure. A reader that has detached an exhausted buffer releases it after leaving the monitor.

Author:
Eike Stepper
  • Field Details

    • NO_TIMEOUT

      public static final long NO_TIMEOUT
      See Also:
    • DEFAULT_MILLIS_BEFORE_TIMEOUT

      public static final long DEFAULT_MILLIS_BEFORE_TIMEOUT
      See Also:
    • DEFAULT_MILLIS_INTERRUPT_CHECK

      @Deprecated public static final long DEFAULT_MILLIS_INTERRUPT_CHECK
      Deprecated.
      Periodic interrupt checks are no longer performed.
      Since:
      2.0
      See Also:
  • Constructor Details

    • BufferInputStream

      public BufferInputStream()
  • Method Details

    • isCCAM

      public boolean isCCAM()
      Returns whether the currently acquired buffer requested that its channel be closed after consumption.
      Since:
      4.4
    • getMillisBeforeTimeout

      public long getMillisBeforeTimeout()
    • restartTimeout

      public void restartTimeout()
      Restarts the current read timeout and wakes a waiting reader to recompute its deadline.
      Since:
      2.0
    • getAbortCause

      public RuntimeException getAbortCause()
      Returns the cause supplied to abort(RuntimeException), or null if this stream has not been aborted.
      Since:
      4.23
    • abort

      public void abort(RuntimeException cause)
      Aborts this stream and immediately wakes a thread waiting for its next buffer. The first non-null cause wins; later calls do not replace it. Abort is observed at a buffer-acquisition boundary and does not release queued or acquired buffers, which remain owned until consumed or closed.
      Parameters:
      cause - the non-null cause thrown by a waiting or subsequent reader
      Throws:
      NullPointerException - if cause is null
      Since:
      4.23
    • handleBuffer

      public void handleBuffer(IBuffer buffer)
      Accepts a buffer from a producer. Invoking this method transfers ownership to the stream. If it has already been closed, this method releases the supplied buffer itself; the caller must not release it again.
      Specified by:
      handleBuffer in interface IBufferHandler
      Parameters:
      buffer - the buffer to accept
    • read

      public int read() throws IOException
      Returns the next byte, or IOUtil.EOF at end of stream.

      The monitor protects acquisition and access to the current buffer. If this method waits for a buffer, Object.wait() releases the monitor fully, including this method's reentrant acquisition, so producers and lifecycle operations can proceed. An exhausted buffer is detached under the monitor and released afterward.

      Specified by:
      read in class InputStream
      Throws:
      IOException - if waiting for a buffer times out
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Reads bytes from this stream, acquiring the monitor for at most one buffer per iteration. A read spanning multiple buffers releases the monitor between them so producers and lifecycle operations can proceed. After bytes have been copied, a later timeout or EOS returns the partial result; an exception before any bytes are read is propagated.
      Overrides:
      read in class InputStream
      Parameters:
      b - the destination array
      off - the destination offset
      len - the maximum number of bytes to read
      Returns:
      the number of bytes read, or IOUtil.EOF if a positive-length read reaches end of stream before reading any bytes
      Throws:
      IOException - if waiting for a buffer fails before any bytes are read
    • close

      public void close() throws IOException
      Closes the stream, detaches its current and queued buffers, and wakes any waiting reader. Buffer release and channel lifecycle callbacks occur outside the stream monitor.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if the superclass close operation fails
    • toString

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

      protected boolean ensureBuffer() throws IOException
      Throws:
      IOException
    • closeChannel

      protected void closeChannel()
      Subclasses may override.
      Since:
      4.5
    • getMillisInterruptCheck

      @Deprecated public long getMillisInterruptCheck()
      Deprecated.
      Periodic interrupt checks are no longer performed.
      Since:
      2.0
    • getException

      @Deprecated public RuntimeException getException()
      Deprecated.
      Returns the exception injected into this stream.
      Returns:
      the injected exception, or null if none was set
      Since:
      2.0
    • setException

      @Deprecated public void setException(RemoteException exception)
      Deprecated.
      Use abort(RuntimeException) to abort the stream.
      Replaces or clears the historical exception property and wakes a waiting reader. Passing null clears the cause. Unlike abort(RuntimeException), this compatibility setter may replace an earlier cause.
      Parameters:
      exception - the exception to inject, or null to clear it
      Since:
      4.0