class AsynchronousBodyInputStream extends BodyInputStream
BodyInputStream
for details.Modifier and Type | Class and Description |
---|---|
private static class |
AsynchronousBodyInputStream.Mode |
(package private) static interface |
AsynchronousBodyInputStream.StateChangeLister
Internal listener, so that the connection pool knows when the body has been read,
so it can reuse/close the connection.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
callReadListener |
private boolean |
closedForInput |
private java.util.Deque<java.nio.ByteBuffer> |
data |
private static java.nio.ByteBuffer |
EOF |
private static java.nio.ByteBuffer |
ERROR |
private java.util.concurrent.ExecutorService |
listenerExecutor |
private AsynchronousBodyInputStream.Mode |
mode |
private ReadListener |
readListener |
private AsynchronousBodyInputStream.StateChangeLister |
stateChangeLister |
private ByteBufferInputStream |
synchronousStream |
private java.lang.Throwable |
t |
NOTHING
Constructor and Description |
---|
AsynchronousBodyInputStream() |
Modifier and Type | Method and Description |
---|---|
private void |
assertAsynchronousOperation() |
private void |
assertClosedForInput() |
private void |
assertSynchronousOperation() |
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.
|
private void |
callDataAvailable() |
private void |
callOnAllDataRead() |
private void |
callOnError(java.lang.Throwable t) |
void |
close() |
private void |
commitToMode() |
private byte |
doRead() |
private boolean |
hasDataToRead() |
boolean |
isReady()
Returns true if data can be read without blocking else returns
false.
|
(package private) void |
notifyAllDataRead() |
(package private) void |
notifyDataAvailable(java.nio.ByteBuffer availableData) |
(package private) void |
notifyError(java.lang.Throwable t) |
int |
read() |
int |
read(byte[] b,
int off,
int len) |
(package private) void |
setListenerExecutor(java.util.concurrent.ExecutorService listenerExecutor) |
void |
setReadListener(ReadListener readListener)
Instructs the stream to invoke the provided
ReadListener when it is possible to read. |
(package private) void |
setStateChangeLister(AsynchronousBodyInputStream.StateChangeLister stateChangeLister) |
long |
skip(long n) |
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) . |
private void |
validateState() |
private static final java.nio.ByteBuffer EOF
private static final java.nio.ByteBuffer ERROR
private AsynchronousBodyInputStream.Mode mode
private ReadListener readListener
private boolean callReadListener
private java.lang.Throwable t
private boolean closedForInput
private java.util.concurrent.ExecutorService listenerExecutor
private AsynchronousBodyInputStream.StateChangeLister stateChangeLister
private ByteBufferInputStream synchronousStream
private java.util.Deque<java.nio.ByteBuffer> data
void setListenerExecutor(java.util.concurrent.ExecutorService listenerExecutor)
public boolean isReady()
BodyInputStream
IllegalStateException
is thrown.isReady
in class BodyInputStream
true
if data can be obtained without blocking,
otherwise returns false
.public void setReadListener(ReadListener readListener)
BodyInputStream
ReadListener
when it is possible to read.
If the stream is in UNDECIDED state, invoking this method will commit the stream to ASYNCHRONOUS mode.setReadListener
in class BodyInputStream
readListener
- the ReadListener
that should be notified
when it's possible to read.public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
private byte doRead()
public int available() throws java.io.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.java.io.IOException
- if an I/O error occurs.public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException
public int tryRead() throws java.io.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.java.io.IOException
- if an I/O error occurs.public int tryRead(byte[] b) throws java.io.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.java.io.IOException
- if an I/O error occurs.public int tryRead(byte[] b, int off, int len) throws java.io.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.java.io.IOException
- if an I/O error occurs.void notifyDataAvailable(java.nio.ByteBuffer availableData)
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
void notifyError(java.lang.Throwable t)
void notifyAllDataRead()
private void commitToMode()
private void assertAsynchronousOperation()
private void assertSynchronousOperation()
private void validateState()
private void assertClosedForInput()
private boolean hasDataToRead()
private void callDataAvailable()
private void callOnError(java.lang.Throwable t)
private void callOnAllDataRead()
void setStateChangeLister(AsynchronousBodyInputStream.StateChangeLister stateChangeLister)