Class AsynchronousBodyInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
BodyInputStream
for details.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enum
(package private) static interface
Internal listener, so that the connection pool knows when the body has been read, so it can reuse/close the connection. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
private Deque<ByteBuffer>
private static final ByteBuffer
private static final ByteBuffer
private ExecutorService
private AsynchronousBodyInputStream.Mode
private ReadListener
private ByteBufferInputStream
private Throwable
Fields inherited from class org.glassfish.jersey.internal.util.collection.NonBlockingInputStream
NOTHING
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
private void
private void
int
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
private void
private void
void
close()
private void
private byte
doRead()
private boolean
boolean
isReady()
Returns true if data can be read without blocking else returns false.(package private) void
(package private) void
notifyDataAvailable
(ByteBuffer availableData) (package private) void
int
read()
int
read
(byte[] b, int off, int len) (package private) void
setListenerExecutor
(ExecutorService listenerExecutor) void
setReadListener
(ReadListener readListener) Instructs the stream to invoke the providedReadListener
when it is possible to read.(package private) void
setStateChangeLister
(AsynchronousBodyInputStream.StateChangeLister stateChangeLister) long
skip
(long n) int
tryRead()
Behaves mostly likeInputStream.read()
.int
tryRead
(byte[] b) Behaves mostly likeInputStream.read(byte[])
.int
tryRead
(byte[] b, int off, int len) Behaves mostly likeInputStream.read(byte[], int, int)
.private void
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo
-
Field Details
-
EOF
-
ERROR
-
mode
-
readListener
-
callReadListener
private boolean callReadListener -
t
-
closedForInput
private boolean closedForInput -
listenerExecutor
-
stateChangeLister
-
synchronousStream
-
data
-
-
Constructor Details
-
AsynchronousBodyInputStream
AsynchronousBodyInputStream()
-
-
Method Details
-
setListenerExecutor
-
isReady
public boolean isReady()Description copied from class:BodyInputStream
Returns true if data can be read without blocking else returns false. If the stream is in ASYNCHRONOUS mode and the user attempts to read from it even though this method returns false, anIllegalStateException
is thrown.- Specified by:
isReady
in classBodyInputStream
- Returns:
true
if data can be obtained without blocking, otherwise returnsfalse
.
-
setReadListener
Description copied from class:BodyInputStream
Instructs the stream to invoke the providedReadListener
when it is possible to read. If the stream is in UNDECIDED state, invoking this method will commit the stream to ASYNCHRONOUS mode.- Specified by:
setReadListener
in classBodyInputStream
- Parameters:
readListener
- theReadListener
that should be notified when it's possible to read.
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
doRead
private byte doRead() -
available
Description copied from class:NonBlockingInputStream
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. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.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 theInputStream.close()
method.The default implementation of this method in
NonBlockingInputStream
throws anUnsupportedOperationException
. 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
- Overrides:
available
in classNonBlockingInputStream
- Returns:
- an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking or
0
when it reaches the end of the input stream or the stream is empty. - Throws:
IOException
- if an I/O error occurs.
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
tryRead
Description copied from class:NonBlockingInputStream
Behaves mostly likeInputStream.read()
. The main difference is that this method is non-blocking. In case there are no data available to be read, the method returnsNonBlockingInputStream.NOTHING
immediately.- Specified by:
tryRead
in classNonBlockingInputStream
- Returns:
- next byte of data,
-1
if end of the stream has been reached orNonBlockingInputStream.NOTHING
in case no data are available to be read at the moment. - Throws:
IOException
- if an I/O error occurs.
-
tryRead
Description copied from class:NonBlockingInputStream
Behaves mostly likeInputStream.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.- Specified by:
tryRead
in classNonBlockingInputStream
- Parameters:
b
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer or
-1
if end of the stream has been reached or0
in case no data are available to be read at the moment. - Throws:
IOException
- if an I/O error occurs.
-
tryRead
Description copied from class:NonBlockingInputStream
Behaves mostly likeInputStream.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.- Specified by:
tryRead
in classNonBlockingInputStream
- Parameters:
b
- the buffer into which the data is read.off
- the start offset in arrayb
at which the data is written.len
- the maximum number of bytes to read.- Returns:
- the total number of bytes read into the buffer or
-1
if end of the stream has been reached or0
in case no data are available to be read at the moment. - Throws:
IOException
- if an I/O error occurs.
-
notifyDataAvailable
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
notifyError
-
notifyAllDataRead
void notifyAllDataRead() -
commitToMode
private void commitToMode() -
assertAsynchronousOperation
private void assertAsynchronousOperation() -
assertSynchronousOperation
private void assertSynchronousOperation() -
validateState
private void validateState() -
assertClosedForInput
private void assertClosedForInput() -
hasDataToRead
private boolean hasDataToRead() -
callDataAvailable
private void callDataAvailable() -
callOnError
-
callOnAllDataRead
private void callOnAllDataRead() -
setStateChangeLister
-