Class ByteBufferInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.glassfish.jersey.internal.util.collection.NonBlockingInputStream
-
- org.glassfish.jersey.internal.util.collection.ByteBufferInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class ByteBufferInputStream extends NonBlockingInputStream
AnInputStreambacked by a queue ofbyte buffersto be read.This input stream serves as a bridging inbound I/O component between a blocking upper I/O layer and an underlying non-blocking I/O layer. In addition to the blocking
InputStream.readoperations, this input stream provides the non-blockingtryReadcounterparts.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.BlockingQueue<java.nio.ByteBuffer>buffersQueue of byte buffers to be read.private java.util.concurrent.atomic.AtomicBooleanclosedClosed flag.private java.nio.ByteBuffercurrentCurrently read byte buffer.private booleaneofRead-side EOF flag.private static java.nio.ByteBufferEOFConstant buffer indicating EOF.private java.util.concurrent.atomic.AtomicReference<java.lang.Object>queueStatusContent represents the buffer queue status.-
Fields inherited from class org.glassfish.jersey.internal.util.collection.NonBlockingInputStream
NOTHING
-
-
Constructor Summary
Constructors Constructor Description ByteBufferInputStream()Create a new input stream that is backed by a a queue ofbyte buffersto be read.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 voidcheckNotClosed()private voidcheckThrowable()voidclose()voidcloseQueue()Closes the byte buffer sink of this input stream to indicate that writing to the stream has been finished.voidcloseQueue(java.lang.Throwable throwable)Closes the byte buffer sink of this input stream to indicate that writing to the stream has been finished due to a throwable.private booleanfetchChunk(boolean block)booleanput(java.nio.ByteBuffer src)Put theByteBufferto the internal queue to be available for reading from the stream.intread()intread(byte[] b, int off, int len)inttryRead()Behaves mostly likeInputStream.read().private inttryRead(boolean block)inttryRead(byte[] b)Behaves mostly likeInputStream.read(byte[]).inttryRead(byte[] b, int off, int len)Behaves mostly likeInputStream.read(byte[], int, int).private inttryRead(byte[] b, int off, int len, boolean block)
-
-
-
Field Detail
-
EOF
private static final java.nio.ByteBuffer EOF
Constant buffer indicating EOF.
-
eof
private boolean eof
Read-side EOF flag. Does not have to be volatile, it is transient and only accessed from the reader thread.
-
current
private java.nio.ByteBuffer current
Currently read byte buffer.
-
buffers
private final java.util.concurrent.BlockingQueue<java.nio.ByteBuffer> buffers
Queue of byte buffers to be read.
-
queueStatus
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> queueStatus
Content represents the buffer queue status. null = open, POISON_PILL = closed, Throwable = closed with error
-
closed
private final java.util.concurrent.atomic.AtomicBoolean closed
Closed flag.
-
-
Method Detail
-
fetchChunk
private boolean fetchChunk(boolean block) throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
checkNotClosed
private void checkNotClosed() throws java.io.IOException- Throws:
java.io.IOException
-
checkThrowable
private void checkThrowable() throws java.io.IOException- Throws:
java.io.IOException
-
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.
-
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
-
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.
-
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
-
put
public boolean put(java.nio.ByteBuffer src) throws java.lang.InterruptedExceptionPut theByteBufferto the internal queue to be available for reading from the stream.If the sink is open, the method
puts the bufferinto an internal byte buffer read queue , waiting if necessary for space to become available. Then the method returnstrueto indicate the buffer has been successfully queued. In case the internal read queue has beenclosedalready, the method simply returnsfalsewithout registering the buffer in the closed queue.- Parameters:
src- the source buffer to be registered in the byte buffer read queue.- Returns:
trueif the byte buffer has been successfully put in the read queue,falseif the read queue has been closed.- Throws:
java.lang.InterruptedException- in case the put operation has been interrupted.
-
closeQueue
public void closeQueue()
Closes the byte buffer sink of this input stream to indicate that writing to the stream has been finished.If the sink has already been closed then this method returns immediately. Otherwise the sink is marked as closed and no more data can be written to it.
-
closeQueue
public void closeQueue(java.lang.Throwable throwable)
Closes the byte buffer sink of this input stream to indicate that writing to the stream has been finished due to a throwable. The throwable set by this method will be propagated to the reader thread when a new attempt to read bytes is made.If the sink has already been closed then this method only sets the throwable in the stream and then returns immediately. Otherwise the sink is also marked as closed and no more data can be written to it.
- Parameters:
throwable- throwable that is set in the stream. It will be thrown by the stream in case an attempt to read more data or check available bytes is made.
-
tryRead
private int tryRead(byte[] b, int off, int len, boolean block) throws java.io.IOException- Throws:
java.io.IOException
-
tryRead
private int tryRead(boolean block) throws java.io.IOException- Throws:
java.io.IOException
-
-