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
byte s 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 byte s read,
or -1 if no byte s 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, skip
public 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 null
NullPointerException
- if byteBufAllocator
is
null
addByteBuf(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 Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
- 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 InputStream
0
or a positive int
IOException
- 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 InputStream
targetBytes
- the byte
array into which read byte
s will be written, beginning at index 0
; must not be
null
byte
s 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 null
IOException
- if this TerminableByteBufInputStream
has been closed
read(byte[], int, int)
,
terminate()
,
close()
,
addByteBuf(ByteBuf)
public final int read(byte[] targetByteArray, int offset, int length) throws IOException
byte
s 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 byte
s read,
or -1
if no byte
s 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 InputStream
targetByteArray
- an array of byte
s to which read
byte
s will be written, beginning at the element
identified by the supplied offset
; must not be null
offset
- 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 targetByteArray
length
- the number of byte
s 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) offset
byte
s 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
null
IndexOutOfBoundsException
- 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
offset
IOException
- if this TerminableByteBufInputStream
has been closed
terminate()
,
close()
,
addByteBuf(ByteBuf)
public final void addByteBuf(ByteBuf byteBuf)
TerminableByteBufInputStream
to
read.byteBuf
- a ByteBuf
; must not be null
and
must be (initially) readable
NullPointerException
- if byteBuf
is null
IllegalArgumentException
- if byteBuf
is
not readableIllegalStateException
- if this TerminableByteBufInputStream
is closed
or
terminatedterminate()
,
close()
,
read(byte[], int, int)
Copyright © 2019–2020, microBean™. All rights reserved.