Package org.apache.http.impl.io
Class SessionInputBufferImpl
- java.lang.Object
-
- org.apache.http.impl.io.SessionInputBufferImpl
-
- All Implemented Interfaces:
BufferInfo,SessionInputBuffer
public class SessionInputBufferImpl extends java.lang.Object implements SessionInputBuffer, BufferInfo
Abstract base class for session input buffers that stream data from an arbitraryInputStream. This class buffers input data in an internal byte array for optimal input performance.readLine(CharArrayBuffer)andreadLine()methods of this class treat a lone LF as valid line delimiters in addition to CR-LF required by the HTTP specification.- Since:
- 4.3
-
-
Constructor Summary
Constructors Constructor Description SessionInputBufferImpl(HttpTransportMetricsImpl metrics, int buffersize)SessionInputBufferImpl(HttpTransportMetricsImpl metrics, int buffersize, int minChunkLimit, MessageConstraints constraints, java.nio.charset.CharsetDecoder chardecoder)Creates new instance of SessionInputBufferImpl.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns available space in the buffer.voidbind(java.io.InputStream instream)intcapacity()Returns total capacity of the buffervoidclear()intfillBuffer()HttpTransportMetricsgetMetrics()ReturnsHttpTransportMetricsfor this session buffer.booleanhasBufferedData()booleanisBound()booleanisDataAvailable(int timeout)Blocks until some data becomes available in the session buffer or the given timeout period in milliseconds elapses.intlength()Return length data stored in the bufferintread()Reads the next byte of data from this session buffer.intread(byte[] b)Reads some number of bytes from the session buffer and stores them into the buffer arrayb.intread(byte[] b, int off, int len)Reads up tolenbytes of data from the session buffer into an array of bytes.java.lang.StringreadLine()Reads a complete line of characters up to a line delimiter from this session buffer.intreadLine(CharArrayBuffer charbuffer)Reads a complete line of characters up to a line delimiter from this session buffer into the given line buffer.
-
-
-
Constructor Detail
-
SessionInputBufferImpl
public SessionInputBufferImpl(HttpTransportMetricsImpl metrics, int buffersize, int minChunkLimit, MessageConstraints constraints, java.nio.charset.CharsetDecoder chardecoder)
Creates new instance of SessionInputBufferImpl.- Parameters:
metrics- HTTP transport metrics.buffersize- buffer size. Must be a positive number.minChunkLimit- size limit below which data chunks should be buffered in memory in order to minimize native method invocations on the underlying network socket. The optimal value of this parameter can be platform specific and defines a trade-off between performance of memory copy operations and that of native method invocation. If negative default chunk limited will be used.constraints- Message constraints. IfnullMessageConstraints.DEFAULTwill be used.chardecoder- chardecoder to be used for decoding HTTP protocol elements. Ifnullsimple type cast will be used for byte to char conversion.
-
SessionInputBufferImpl
public SessionInputBufferImpl(HttpTransportMetricsImpl metrics, int buffersize)
-
-
Method Detail
-
bind
public void bind(java.io.InputStream instream)
-
isBound
public boolean isBound()
-
capacity
public int capacity()
Description copied from interface:BufferInfoReturns total capacity of the buffer- Specified by:
capacityin interfaceBufferInfo- Returns:
- total capacity
-
length
public int length()
Description copied from interface:BufferInfoReturn length data stored in the buffer- Specified by:
lengthin interfaceBufferInfo- Returns:
- data length
-
available
public int available()
Description copied from interface:BufferInfoReturns available space in the buffer.- Specified by:
availablein interfaceBufferInfo- Returns:
- available space.
-
fillBuffer
public int fillBuffer() throws java.io.IOException- Throws:
java.io.IOException
-
hasBufferedData
public boolean hasBufferedData()
-
clear
public void clear()
-
read
public int read() throws java.io.IOExceptionDescription copied from interface:SessionInputBufferReads the next byte of data from this session buffer. The value byte is returned as anintin the range0to255. If no byte is available because the end of the stream has been reached, the value-1is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.- Specified by:
readin interfaceSessionInputBuffer- Returns:
- the next byte of data, or
-1if the end of the stream is reached. - Throws:
java.io.IOException- if an I/O error occurs.
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionDescription copied from interface:SessionInputBufferReads up tolenbytes of data from the session buffer into an array of bytes. An attempt is made to read as many aslenbytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.This method blocks until input data is available, end of file is detected, or an exception is thrown.
If
offis negative, orlenis negative, oroff+lenis greater than the length of the arrayb, then anIndexOutOfBoundsExceptionis thrown.- Specified by:
readin interfaceSessionInputBuffer- 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.IOExceptionDescription copied from interface:SessionInputBufferReads some number of bytes from the session buffer and stores them into the buffer arrayb. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.- Specified by:
readin interfaceSessionInputBuffer- Parameters:
b- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or
-1is there is no more data because the end of the stream has been reached. - Throws:
java.io.IOException- if an I/O error occurs.
-
readLine
public int readLine(CharArrayBuffer charbuffer) throws java.io.IOException
Reads a complete line of characters up to a line delimiter from this session buffer into the given line buffer. The number of chars actually read is returned as an integer. The line delimiter itself is discarded. If no char is available because the end of the stream has been reached, the value-1is returned. This method blocks until input data is available, end of file is detected, or an exception is thrown.This method treats a lone LF as a valid line delimiters in addition to CR-LF required by the HTTP specification.
- Specified by:
readLinein interfaceSessionInputBuffer- Parameters:
charbuffer- the line buffer.- Returns:
- one line of characters
- Throws:
java.io.IOException- if an I/O error occurs.
-
readLine
public java.lang.String readLine() throws java.io.IOExceptionDescription copied from interface:SessionInputBufferReads a complete line of characters up to a line delimiter from this session buffer. The line delimiter itself is discarded. If no char is available because the end of the stream has been reached,nullis returned. This method blocks until input data is available, end of file is detected, or an exception is thrown.The choice of a char encoding and line delimiter sequence is up to the specific implementations of this interface.
- Specified by:
readLinein interfaceSessionInputBuffer- Returns:
- HTTP line as a string
- Throws:
java.io.IOException- if an I/O error occurs.
-
isDataAvailable
public boolean isDataAvailable(int timeout) throws java.io.IOExceptionDescription copied from interface:SessionInputBufferBlocks until some data becomes available in the session buffer or the given timeout period in milliseconds elapses. If the timeout value is0this method blocks indefinitely.- Specified by:
isDataAvailablein interfaceSessionInputBuffer- Parameters:
timeout- in milliseconds.- Returns:
trueif some data is available in the session buffer orfalseotherwise.- Throws:
java.io.IOException- if an I/O error occurs.
-
getMetrics
public HttpTransportMetrics getMetrics()
Description copied from interface:SessionInputBufferReturnsHttpTransportMetricsfor this session buffer.- Specified by:
getMetricsin interfaceSessionInputBuffer- Returns:
- transport metrics.
-
-