public final class ByteBufferInputStream extends NonBlockingInputStream
InputStream
backed by a queue of byte buffers
to be read.
This input stream serves as a bridging inbound I/O component between a blocking upper I/O layer
and an underlying non-blocking I/O layer. In addition to the blocking InputStream.read
operations,
this input stream provides the non-blocking tryRead
counterparts.
NOTHING
Constructor and Description |
---|
ByteBufferInputStream()
Create a new input stream that is backed by a a queue of
byte buffers
to be read. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns an estimate of the number of bytes that can be read (or
skipped over) from this input stream without blocking by the next
invocation of a method for this input stream.
|
void |
close() |
void |
closeQueue()
Closes the byte buffer sink of this input stream to indicate that writing to the stream
has been finished.
|
void |
closeQueue(Throwable throwable)
Closes the byte buffer sink of this input stream to indicate that writing to the stream
has been finished due to a throwable.
|
boolean |
put(ByteBuffer src)
Put the
ByteBuffer to the internal queue to be available for reading from the stream. |
int |
read() |
int |
read(byte[] b,
int off,
int len) |
int |
tryRead()
Behaves mostly like
InputStream.read() . |
int |
tryRead(byte[] b)
Behaves mostly like
InputStream.read(byte[]) . |
int |
tryRead(byte[] b,
int off,
int len)
Behaves mostly like
InputStream.read(byte[], int, int) . |
mark, markSupported, read, reset, skip
public ByteBufferInputStream()
byte buffers
to be read.public int available() throws IOException
NonBlockingInputStream
Note that while some implementations of InputStream
will return
the total number of bytes in the stream, many will not. It is
never correct to use the return value of this method to allocate
a buffer intended to hold all data in this stream.
A subclass' implementation of this method may choose to throw an
IOException
if this input stream has been closed by
invoking the InputStream.close()
method.
The default implementation of this method in NonBlockingInputStream
throws an UnsupportedOperationException
. This method must be overridden
by subclasses. The overriding implementations must guarantee non-blocking behavior
of the method. The overriding implementation must also guarantee that a non-empty
stream does not return zero from the method. IOW, it must be possible to use the
method for empty check: stream.available() == 0
available
in class NonBlockingInputStream
0
when
it reaches the end of the input stream or the stream is empty.IOException
- if an I/O error occurs.public int read() throws IOException
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
read
in class InputStream
IOException
public int tryRead() throws IOException
NonBlockingInputStream
InputStream.read()
.
The main difference is that this method is non-blocking. In case there are no
data available to be read, the method returns NonBlockingInputStream.NOTHING
immediately.tryRead
in class NonBlockingInputStream
-1
if end of the stream has been reached or
NonBlockingInputStream.NOTHING
in case no data are available to be read at the moment.IOException
- if an I/O error occurs.public int tryRead(byte[] b) throws IOException
NonBlockingInputStream
InputStream.read(byte[])
.
The main difference is that this method is non-blocking. In case there are no
data available to be read, the method returns zero immediately.tryRead
in class NonBlockingInputStream
b
- the buffer into which the data is read.-1
if end of the
stream has been reached or 0
in case no data are available to be
read at the moment.IOException
- if an I/O error occurs.public int tryRead(byte[] b, int off, int len) throws IOException
NonBlockingInputStream
InputStream.read(byte[], int, int)
.
The main difference is that this method is non-blocking. In case there are no
data available to be read, the method returns zero immediately.tryRead
in class NonBlockingInputStream
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.-1
if end of the
stream has been reached or 0
in case no data are available to be
read at the moment.IOException
- if an I/O error occurs.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public boolean put(ByteBuffer src) throws InterruptedException
ByteBuffer
to the internal queue to be available for reading from the stream.
If the sink is open, the method puts the buffer
into an internal
byte buffer read queue , waiting if necessary for space to become available. Then the method returns
true
to indicate the buffer has been successfully queued. In case the internal read queue has been
closed
already, the method simply returns false
without registering
the buffer in the closed queue.
src
- the source buffer to be registered in the byte buffer read queue.true
if the byte buffer has been successfully put in the read queue,
false
if the read queue has been closed.InterruptedException
- in case the put operation has been interrupted.public void closeQueue()
If the sink has already been closed then this method returns immediately. Otherwise the sink is marked as closed and no more data can be written to it.
public void closeQueue(Throwable throwable)
If the sink has already been closed then this method only sets the throwable in the stream and then returns immediately. Otherwise the sink is also marked as closed and no more data can be written to it.
throwable
- throwable that is set in the stream. It will be thrown by the stream in case
an attempt to read more data or check available bytes is made.Copyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.