Class BufferInputStream
- All Implemented Interfaces:
Closeable, AutoCloseable, IBufferHandler
- Direct Known Subclasses:
ChannelInputStream
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final longstatic final longDeprecated.Periodic interrupt checks are no longer performed.static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidabort(RuntimeException cause) Aborts this stream and immediately wakes a thread waiting for its next buffer.voidclose()Closes the stream, detaches its current and queued buffers, and wakes any waiting reader.protected voidSubclasses may override.protected booleanReturns the cause supplied toabort(RuntimeException), ornullif this stream has not been aborted.Deprecated.UsegetAbortCause().longlongDeprecated.Periodic interrupt checks are no longer performed.voidhandleBuffer(IBuffer buffer) Accepts a buffer from a producer.booleanisCCAM()Returns whether the currently acquired buffer requested that its channel be closed after consumption.intread()Returns the next byte, orIOUtil.EOFat end of stream.intread(byte[] b, int off, int len) Reads bytes from this stream, acquiring the monitor for at most one buffer per iteration.voidRestarts the current read timeout and wakes a waiting reader to recompute its deadline.voidsetException(RemoteException exception) Deprecated.Useabort(RuntimeException)to abort the stream.toString()Methods inherited from class InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
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.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
Returns the cause supplied toabort(RuntimeException), ornullif this stream has not been aborted.- Since:
- 4.23
-
abort
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- ifcauseisnull- Since:
- 4.23
-
handleBuffer
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:
handleBufferin interfaceIBufferHandler- Parameters:
buffer- the buffer to accept
-
read
Returns the next byte, orIOUtil.EOFat 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:
readin classInputStream- Throws:
IOException- if waiting for a buffer times out
-
read
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:
readin classInputStream- Parameters:
b- the destination arrayoff- the destination offsetlen- the maximum number of bytes to read- Returns:
- the number of bytes read, or
IOUtil.EOFif 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
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- if the superclass close operation fails
-
toString
-
ensureBuffer
- Throws:
IOException
-
closeChannel
protected void closeChannel()Subclasses may override.- Since:
- 4.5
-
getMillisInterruptCheck
Deprecated.Periodic interrupt checks are no longer performed.- Since:
- 2.0
-
getException
Deprecated.UsegetAbortCause().Returns the exception injected into this stream.- Returns:
- the injected exception, or
nullif none was set - Since:
- 2.0
-
setException
Deprecated.Useabort(RuntimeException)to abort the stream.Replaces or clears the historical exception property and wakes a waiting reader. Passingnullclears the cause. Unlikeabort(RuntimeException), this compatibility setter may replace an earlier cause.- Parameters:
exception- the exception to inject, ornullto clear it- Since:
- 4.0
-