Class AsynchronousBodyInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.glassfish.jersey.internal.util.collection.NonBlockingInputStream
-
- org.glassfish.jersey.jdk.connector.internal.BodyInputStream
-
- org.glassfish.jersey.jdk.connector.internal.AsynchronousBodyInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
class AsynchronousBodyInputStream extends BodyInputStream
TODO Some of the operations added for async. support (e.g.) can be also supported in sync. mode Body stream that can operate either synchronously or asynchronously. SeeBodyInputStreamfor details.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classAsynchronousBodyInputStream.Mode(package private) static interfaceAsynchronousBodyInputStream.StateChangeListerInternal listener, so that the connection pool knows when the body has been read, so it can reuse/close the connection.
-
Field Summary
Fields Modifier and Type Field Description private booleancallReadListenerprivate booleanclosedForInputprivate java.util.Deque<java.nio.ByteBuffer>dataprivate static java.nio.ByteBufferEOFprivate static java.nio.ByteBufferERRORprivate java.util.concurrent.ExecutorServicelistenerExecutorprivate AsynchronousBodyInputStream.Modemodeprivate ReadListenerreadListenerprivate AsynchronousBodyInputStream.StateChangeListerstateChangeListerprivate ByteBufferInputStreamsynchronousStreamprivate java.lang.Throwablet-
Fields inherited from class org.glassfish.jersey.internal.util.collection.NonBlockingInputStream
NOTHING
-
-
Constructor Summary
Constructors Constructor Description AsynchronousBodyInputStream()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassertAsynchronousOperation()private voidassertClosedForInput()private voidassertSynchronousOperation()intavailable()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 voidcallDataAvailable()private voidcallOnAllDataRead()private voidcallOnError(java.lang.Throwable t)voidclose()private voidcommitToMode()private bytedoRead()private booleanhasDataToRead()booleanisReady()Returns true if data can be read without blocking else returns false.(package private) voidnotifyAllDataRead()(package private) voidnotifyDataAvailable(java.nio.ByteBuffer availableData)(package private) voidnotifyError(java.lang.Throwable t)intread()intread(byte[] b, int off, int len)(package private) voidsetListenerExecutor(java.util.concurrent.ExecutorService listenerExecutor)voidsetReadListener(ReadListener readListener)Instructs the stream to invoke the providedReadListenerwhen it is possible to read.(package private) voidsetStateChangeLister(AsynchronousBodyInputStream.StateChangeLister stateChangeLister)longskip(long n)inttryRead()Behaves mostly likeInputStream.read().inttryRead(byte[] b)Behaves mostly likeInputStream.read(byte[]).inttryRead(byte[] b, int off, int len)Behaves mostly likeInputStream.read(byte[], int, int).private voidvalidateState()
-
-
-
Field Detail
-
EOF
private static final java.nio.ByteBuffer EOF
-
ERROR
private static final java.nio.ByteBuffer ERROR
-
mode
private AsynchronousBodyInputStream.Mode mode
-
readListener
private ReadListener readListener
-
callReadListener
private boolean callReadListener
-
t
private java.lang.Throwable t
-
closedForInput
private boolean closedForInput
-
listenerExecutor
private java.util.concurrent.ExecutorService listenerExecutor
-
stateChangeLister
private AsynchronousBodyInputStream.StateChangeLister stateChangeLister
-
synchronousStream
private ByteBufferInputStream synchronousStream
-
data
private java.util.Deque<java.nio.ByteBuffer> data
-
-
Method Detail
-
setListenerExecutor
void setListenerExecutor(java.util.concurrent.ExecutorService listenerExecutor)
-
isReady
public boolean isReady()
Description copied from class:BodyInputStreamReturns 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, anIllegalStateExceptionis thrown.- Specified by:
isReadyin classBodyInputStream- Returns:
trueif data can be obtained without blocking, otherwise returnsfalse.
-
setReadListener
public void setReadListener(ReadListener readListener)
Description copied from class:BodyInputStreamInstructs the stream to invoke the providedReadListenerwhen it is possible to read. If the stream is in UNDECIDED state, invoking this method will commit the stream to ASYNCHRONOUS mode.- Specified by:
setReadListenerin classBodyInputStream- Parameters:
readListener- theReadListenerthat should be notified when it's possible to read.
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
doRead
private byte doRead()
-
available
public int available() throws java.io.IOExceptionDescription copied from class:NonBlockingInputStreamReturns 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
InputStreamwill 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
IOExceptionif this input stream has been closed by invoking theInputStream.close()method.The default implementation of this method in
NonBlockingInputStreamthrows 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:
availablein classNonBlockingInputStream- Returns:
- an estimate of the number of bytes that can be read (or skipped
over) from this input stream without blocking or
0when it reaches the end of the input stream or the stream is empty. - Throws:
java.io.IOException- if an I/O error occurs.
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
tryRead
public int tryRead() throws java.io.IOExceptionDescription copied from class:NonBlockingInputStreamBehaves 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.NOTHINGimmediately.- Specified by:
tryReadin classNonBlockingInputStream- Returns:
- next byte of data,
-1if end of the stream has been reached orNonBlockingInputStream.NOTHINGin case no data are available to be read at the moment. - Throws:
java.io.IOException- if an I/O error occurs.
-
tryRead
public int tryRead(byte[] b) throws java.io.IOExceptionDescription copied from class:NonBlockingInputStreamBehaves 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:
tryReadin classNonBlockingInputStream- Parameters:
b- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer or
-1if end of the stream has been reached or0in case no data are available to be read at the moment. - Throws:
java.io.IOException- if an I/O error occurs.
-
tryRead
public int tryRead(byte[] b, int off, int len) throws java.io.IOExceptionDescription copied from class:NonBlockingInputStreamBehaves 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:
tryReadin classNonBlockingInputStream- Parameters:
b- the buffer into which the data is read.off- the start offset in arraybat which the data is written.len- the maximum number of bytes to read.- Returns:
- the total number of bytes read into the buffer or
-1if end of the stream has been reached or0in case no data are available to be read at the moment. - Throws:
java.io.IOException- if an I/O error occurs.
-
notifyDataAvailable
void notifyDataAvailable(java.nio.ByteBuffer availableData)
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
notifyError
void notifyError(java.lang.Throwable t)
-
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
private void callOnError(java.lang.Throwable t)
-
callOnAllDataRead
private void callOnAllDataRead()
-
setStateChangeLister
void setStateChangeLister(AsynchronousBodyInputStream.StateChangeLister stateChangeLister)
-
-