public final class TerminableByteBufInputStream extends InputStream
InputStream implementation that reads from a CompositeByteBuf
whose contents are updated via the addByteBuf(ByteBuf)
method.
This class is designed to bridge the gap between a Netty
I/O-focused event loop and a thread where Jersey will be reading
content. Reads must block on the Jersey thread, but must not
impact the Netty event loop. Instances of this class are used by
AbstractContainerRequestDecoder implementations, when they
need to supply an InputStream to Jersey so that Jersey can
read any incoming entity payloads.
The AbstractContainerRequestDecoder implementation will
typically call addByteBuf(ByteBuf) on the Netty event
loop, and then will call terminate() when it is done.
Meanwhile, Jersey may call the read(byte[], int, int)
method on its own thread, and such a call may block at any point
until Netty supplies more content.
| Constructor and Description |
|---|
TerminableByteBufInputStream(ByteBufAllocator byteBufAllocator)
Creates a new
TerminableByteBufInputStream. |
| Modifier and Type | Method and Description |
|---|---|
void |
addByteBuf(ByteBuf byteBuf)
Adds content for this
TerminableByteBufInputStream to
read. |
int |
available()
Returns an estimate of the number of bytes that may be read
without blocking.
|
void |
close()
Closes this
TerminableByteBufInputStream and releases its underlying
CompositeByteBuf. |
int |
read()
Reads a single byte from this
TerminableByteBufInputStream. |
int |
read(byte[] targetBytes)
Calls the
read(byte[], int, int) method with the
supplied targetBytes array, 0 and targetBytes.length as arguments and returns its result. |
int |
read(byte[] targetByteArray,
int offset,
int length)
Attempts to read the desired number of
bytes as indicated
by the supplied length parameter into the supplied targetByte array, beginning the write at the element in the
supplied targetByteArray designated by the offset
parameter, and returns the actual number of bytes read,
or -1 if no bytes were read and the end of the
stream was reached, in which case terminate() must have
been called in the past. |
void |
terminate()
Irrevocably disables the
addByteBuf(ByteBuf) method such
that calling it will result in an IllegalStateException
being thrown. |
mark, markSupported, reset, skippublic TerminableByteBufInputStream(ByteBufAllocator byteBufAllocator)
TerminableByteBufInputStream.byteBufAllocator - a ByteBufAllocator that will
allocate a new
CompositeByteBuf from which content may be read by another thread;
must not be nullNullPointerException - if byteBufAllocator is
nulladdByteBuf(ByteBuf),
read(byte[], int, int)public final void close() throws IOException
TerminableByteBufInputStream and releases its underlying
CompositeByteBuf.
If this method has been called previously, no further action is taken.
close in interface Closeableclose in interface AutoCloseableclose in class InputStreamIOException - if an error occurspublic final void terminate()
addByteBuf(ByteBuf) method such
that calling it will result in an IllegalStateException
being thrown.
If this method has been called before, or if close()
has been called before, no action is taken.
addByteBuf(ByteBuf),
close()public final int available() throws IOException
available in class InputStream0 or a positive intIOException - if this TerminableByteBufInputStream has been closedclose()public final int read() throws IOException
TerminableByteBufInputStream.read in class InputStream-1 if the end of the stream has
been reached (for this to happen terminate() must have
already been called at some point in the past)IOException - if this TerminableByteBufInputStream has been closedterminate(),
close()public final int read(byte[] targetBytes) throws IOException
read(byte[], int, int) method with the
supplied targetBytes array, 0 and targetBytes.length as arguments and returns its result.read in class InputStreamtargetBytes - the byte array into which read bytes will be written, beginning at index 0; must not be
nullbytes actually read, or -1
if the end of the stream has been reached, in which case terminate() must have been called in the pastNullPointerException - if targetBytes is nullIOException - if this TerminableByteBufInputStream has been closedread(byte[], int, int),
terminate(),
close(),
addByteBuf(ByteBuf)public final int read(byte[] targetByteArray, int offset, int length) throws IOException
bytes as indicated
by the supplied length parameter into the supplied targetByte array, beginning the write at the element in the
supplied targetByteArray designated by the offset
parameter, and returns the actual number of bytes read,
or -1 if no bytes were read and the end of the
stream was reached, in which case terminate() must have
been called in the past.
Content to read is supplied by means of the addByteBuf(ByteBuf) method.
read in class InputStreamtargetByteArray - an array of bytes to which read
bytes will be written, beginning at the element
identified by the supplied offset; must not be nulloffset - the zero-based index of the element within the
supplied targetByteArray that will hold the first byte read; must be 0 or greater and less than the length
property of the supplied targetByteArraylength - the number of bytes to read; must be zero or greater and must be less than or equal to the length
property of the supplied targetByteArray minus the
supplied (valid) offsetbytes actually read, or -1
if the end of the stream has been reached, in which case terminate() must have been called in the pastNullPointerException - if targetByteArray is
nullIndexOutOfBoundsException - if offset is less
than 0, or if length is less than 0, or
if length is greater than the length property of the
supplied targetByteArray parameter minus the supplied
offsetIOException - if this TerminableByteBufInputStream has been closedterminate(),
close(),
addByteBuf(ByteBuf)public final void addByteBuf(ByteBuf byteBuf)
TerminableByteBufInputStream to
read.byteBuf - a ByteBuf; must not be null and
must be (initially) readableNullPointerException - if byteBuf is nullIllegalArgumentException - if byteBuf is
not readableIllegalStateException - if this TerminableByteBufInputStream is closed or
terminatedterminate(),
close(),
read(byte[], int, int)Copyright © 2019–2020, microBean™. All rights reserved.