Package org.apache.http.nio.util
Class SharedInputBuffer
- java.lang.Object
-
- org.apache.http.nio.util.ExpandableBuffer
-
- org.apache.http.nio.util.SharedInputBuffer
-
- All Implemented Interfaces:
BufferInfo,BufferInfo,ContentInputBuffer
@Contract(threading=SAFE_CONDITIONAL) public class SharedInputBuffer extends ExpandableBuffer implements ContentInputBuffer
Implementation of theContentInputBufferinterface that can be shared by multiple threads, usually the I/O dispatch of an I/O reactor and a worker thread.The I/O dispatch thread is expect to transfer data from
ContentDecoderto the buffer by callingconsumeContent(ContentDecoder).The worker thread is expected to read the data from the buffer by calling
read()orread(byte[], int, int)methods.In case of an abnormal situation or when no longer needed the buffer must be shut down using
shutdown()method.- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.locks.Conditionconditionprivate booleanendOfStreamprivate IOControlioControlprivate java.util.concurrent.locks.ReentrantLocklockprivate booleanshutdown-
Fields inherited from class org.apache.http.nio.util.ExpandableBuffer
buffer, INPUT_MODE, OUTPUT_MODE
-
-
Constructor Summary
Constructors Constructor Description SharedInputBuffer(int bufferSize)SharedInputBuffer(int bufferSize, IOControl ioControl, ByteBufferAllocator allocator)Deprecated.SharedInputBuffer(int bufferSize, ByteBufferAllocator allocator)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intavailable()Returns available capacity of this buffer.intcapacity()Returns the total capacity of this buffer.voidclose()intconsumeContent(ContentDecoder decoder)Deprecated.intconsumeContent(ContentDecoder decoder, IOControl ioControl)booleanhasData()Determines if the buffer contains data.protected booleanisEndOfStream()protected booleanisShutdown()intlength()Returns the length of this buffer.intread()Reads one byte from this buffer.intread(byte[] b)intread(byte[] b, int off, int len)Reads up tolenbytes of data from this buffer into an array of bytes.voidreset()Resets the buffer by clearing its state and stored content.voidshutdown()protected voidwaitForData()-
Methods inherited from class org.apache.http.nio.util.ExpandableBuffer
clear, ensureCapacity, expand, getMode, setInputMode, setOutputMode, toString
-
-
-
-
Field Detail
-
lock
private final java.util.concurrent.locks.ReentrantLock lock
-
condition
private final java.util.concurrent.locks.Condition condition
-
ioControl
private volatile IOControl ioControl
-
shutdown
private volatile boolean shutdown
-
endOfStream
private volatile boolean endOfStream
-
-
Constructor Detail
-
SharedInputBuffer
@Deprecated public SharedInputBuffer(int bufferSize, IOControl ioControl, ByteBufferAllocator allocator)Deprecated.
-
SharedInputBuffer
public SharedInputBuffer(int bufferSize, ByteBufferAllocator allocator)- Since:
- 4.3
-
SharedInputBuffer
public SharedInputBuffer(int bufferSize)
- Since:
- 4.3
-
-
Method Detail
-
reset
public void reset()
Description copied from interface:ContentInputBufferResets the buffer by clearing its state and stored content.- Specified by:
resetin interfaceContentInputBuffer
-
consumeContent
@Deprecated public int consumeContent(ContentDecoder decoder) throws java.io.IOException
Deprecated.Description copied from interface:ContentInputBufferReads content from the givenContentDecoderand stores it in this buffer.- Specified by:
consumeContentin interfaceContentInputBuffer- Parameters:
decoder- the content decoder.- Returns:
- number of bytes read.
- Throws:
java.io.IOException- in case of an I/O error.
-
consumeContent
public int consumeContent(ContentDecoder decoder, IOControl ioControl) throws java.io.IOException
- Throws:
java.io.IOException- Since:
- 4.3
-
hasData
public boolean hasData()
Description copied from class:ExpandableBufferDetermines if the buffer contains data.- Overrides:
hasDatain classExpandableBuffer- Returns:
trueif there is data in the buffer,falseotherwise.
-
available
public int available()
Description copied from class:ExpandableBufferReturns available capacity of this buffer.- Specified by:
availablein interfaceBufferInfo- Specified by:
availablein interfaceBufferInfo- Overrides:
availablein classExpandableBuffer- Returns:
- buffer length.
-
capacity
public int capacity()
Description copied from class:ExpandableBufferReturns the total capacity of this buffer.- Specified by:
capacityin interfaceBufferInfo- Specified by:
capacityin interfaceBufferInfo- Overrides:
capacityin classExpandableBuffer- Returns:
- total capacity.
-
length
public int length()
Description copied from class:ExpandableBufferReturns the length of this buffer.- Specified by:
lengthin interfaceBufferInfo- Specified by:
lengthin interfaceBufferInfo- Overrides:
lengthin classExpandableBuffer- Returns:
- buffer length.
-
waitForData
protected void waitForData() throws java.io.IOException- Throws:
java.io.IOException
-
close
public void close()
-
shutdown
public void shutdown()
-
isShutdown
protected boolean isShutdown()
-
isEndOfStream
protected boolean isEndOfStream()
-
read
public int read() throws java.io.IOExceptionDescription copied from interface:ContentInputBufferReads one byte from this buffer. If the buffer is empty this method can throw a runtime exception. The exact type of runtime exception thrown by this method depends on implementation. This method returns-1if the end of content stream has been reached.- Specified by:
readin interfaceContentInputBuffer- Returns:
- one byte
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionDescription copied from interface:ContentInputBufferReads up tolenbytes of data from this buffer into an array of bytes. The exact number of bytes read depends how many bytes are stored in the buffer.If
offis negative, orlenis negative, oroff+lenis greater than the length of the arrayb, this method can throw a runtime exception. The exact type of runtime exception thrown by this method depends on implementation. This method returns-1if the end of content stream has been reached.- Specified by:
readin interfaceContentInputBuffer- 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 there is no more data because the end of the stream has been reached. - Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOException- Throws:
java.io.IOException
-
-