Package org.apache.hc.core5.http.impl.io
Class SessionInputBufferImpl
- java.lang.Object
-
- org.apache.hc.core5.http.impl.io.SessionInputBufferImpl
-
- All Implemented Interfaces:
SessionInputBuffer
public class SessionInputBufferImpl extends java.lang.Object implements SessionInputBuffer
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, InputStream)
method of this class treat a lone LF as valid line delimiters in addition to CR-LF required by the HTTP specification.- Since:
- 4.3
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]
buffer
private int
bufferLen
private int
bufferPos
private java.nio.CharBuffer
cbuf
private java.nio.charset.CharsetDecoder
decoder
private ByteArrayBuffer
lineBuffer
private int
maxLineLen
private BasicHttpTransportMetrics
metrics
private int
minChunkLimit
-
Constructor Summary
Constructors Constructor Description SessionInputBufferImpl(int bufferSize)
SessionInputBufferImpl(int bufferSize, int maxLineLen)
SessionInputBufferImpl(int bufferSize, java.nio.charset.CharsetDecoder decoder)
SessionInputBufferImpl(BasicHttpTransportMetrics metrics, int bufferSize)
SessionInputBufferImpl(BasicHttpTransportMetrics metrics, int bufferSize, int minChunkLimit, int maxLineLen, java.nio.charset.CharsetDecoder charDecoder)
Creates new instance of SessionInputBufferImpl.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
appendDecoded(CharArrayBuffer charbuffer, java.nio.ByteBuffer bbuf)
int
available()
Returns available space in the buffer.int
capacity()
Returns total capacity of the buffervoid
clear()
int
fillBuffer(java.io.InputStream inputStream)
HttpTransportMetrics
getMetrics()
ReturnsHttpTransportMetrics
for this session buffer.private int
handleDecodingResult(java.nio.charset.CoderResult result, CharArrayBuffer charBuffer)
boolean
hasBufferedData()
int
length()
Returns length data stored in the bufferprivate int
lineFromLineBuffer(CharArrayBuffer charBuffer)
Reads a complete line of characters up to a line delimiter from this session buffer.private int
lineFromReadBuffer(CharArrayBuffer charbuffer, int position)
int
read(byte[] b, int off, int len, java.io.InputStream inputStream)
Reads up tolen
bytes of data from the session buffer into an array of bytes.int
read(byte[] b, java.io.InputStream inputStream)
Reads some number of bytes from the session buffer and stores them into the buffer arrayb
.int
read(java.io.InputStream inputStream)
Reads the next byte of data from this session buffer.int
readLine(CharArrayBuffer charBuffer, java.io.InputStream inputStream)
Reads a complete line of characters up to a line delimiter from this session buffer into the given line buffer.
-
-
-
Field Detail
-
metrics
private final BasicHttpTransportMetrics metrics
-
buffer
private final byte[] buffer
-
lineBuffer
private final ByteArrayBuffer lineBuffer
-
minChunkLimit
private final int minChunkLimit
-
maxLineLen
private final int maxLineLen
-
decoder
private final java.nio.charset.CharsetDecoder decoder
-
bufferPos
private int bufferPos
-
bufferLen
private int bufferLen
-
cbuf
private java.nio.CharBuffer cbuf
-
-
Constructor Detail
-
SessionInputBufferImpl
public SessionInputBufferImpl(BasicHttpTransportMetrics metrics, int bufferSize, int minChunkLimit, int maxLineLen, 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.maxLineLen
- maximum line length.charDecoder
- charDecoder to be used for decoding HTTP protocol elements. Ifnull
simple type cast will be used for byte to char conversion.
-
SessionInputBufferImpl
public SessionInputBufferImpl(BasicHttpTransportMetrics metrics, int bufferSize)
-
SessionInputBufferImpl
public SessionInputBufferImpl(int bufferSize, int maxLineLen)
-
SessionInputBufferImpl
public SessionInputBufferImpl(int bufferSize, java.nio.charset.CharsetDecoder decoder)
-
SessionInputBufferImpl
public SessionInputBufferImpl(int bufferSize)
-
-
Method Detail
-
capacity
public int capacity()
Description copied from interface:SessionInputBuffer
Returns total capacity of the buffer- Specified by:
capacity
in interfaceSessionInputBuffer
- Returns:
- total capacity
-
length
public int length()
Description copied from interface:SessionInputBuffer
Returns length data stored in the buffer- Specified by:
length
in interfaceSessionInputBuffer
- Returns:
- data length
-
available
public int available()
Description copied from interface:SessionInputBuffer
Returns available space in the buffer.- Specified by:
available
in interfaceSessionInputBuffer
- Returns:
- available space.
-
fillBuffer
public int fillBuffer(java.io.InputStream inputStream) throws java.io.IOException
- Throws:
java.io.IOException
-
hasBufferedData
public boolean hasBufferedData()
-
clear
public void clear()
-
read
public int read(java.io.InputStream inputStream) throws java.io.IOException
Description copied from interface:SessionInputBuffer
Reads the next byte of data from this session buffer. The value byte is returned as anint
in the range0
to255
. If no byte is available because the end of the stream has been reached, the value-1
is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.- Specified by:
read
in interfaceSessionInputBuffer
- Parameters:
inputStream
- Input stream- Returns:
- the next byte of data, or
-1
if 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, java.io.InputStream inputStream) throws java.io.IOException
Description copied from interface:SessionInputBuffer
Reads up tolen
bytes of data from the session buffer into an array of bytes. An attempt is made to read as many aslen
bytes, 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
off
is negative, orlen
is negative, oroff+len
is greater than the length of the arrayb
, then anIndexOutOfBoundsException
is thrown.- Specified by:
read
in interfaceSessionInputBuffer
- Parameters:
b
- the buffer into which the data is read.off
- the start offset in arrayb
at which the data is written.len
- the maximum number of bytes to read.inputStream
- Input stream- Returns:
- the total number of bytes read into the buffer, or
-1
if 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, java.io.InputStream inputStream) throws java.io.IOException
Description copied from interface:SessionInputBuffer
Reads 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:
read
in interfaceSessionInputBuffer
- Parameters:
b
- the buffer into which the data is read.inputStream
- Input stream- Returns:
- the total number of bytes read into the buffer, or
-1
is 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, java.io.InputStream inputStream) 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-1
is 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:
readLine
in interfaceSessionInputBuffer
- Parameters:
charBuffer
- the line buffer, one line of characters upon returninputStream
- Input stream- Returns:
- the total number of bytes read into the buffer, or
-1
is there is no more data because the end of the stream has been reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
lineFromLineBuffer
private int lineFromLineBuffer(CharArrayBuffer charBuffer) throws java.io.IOException
Reads 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,null
is 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.
- Returns:
- HTTP line as a string
- Throws:
java.io.IOException
- if an I/O error occurs.
-
lineFromReadBuffer
private int lineFromReadBuffer(CharArrayBuffer charbuffer, int position) throws java.io.IOException
- Throws:
java.io.IOException
-
appendDecoded
private int appendDecoded(CharArrayBuffer charbuffer, java.nio.ByteBuffer bbuf) throws java.io.IOException
- Throws:
java.io.IOException
-
handleDecodingResult
private int handleDecodingResult(java.nio.charset.CoderResult result, CharArrayBuffer charBuffer) throws java.io.IOException
- Throws:
java.io.IOException
-
getMetrics
public HttpTransportMetrics getMetrics()
Description copied from interface:SessionInputBuffer
ReturnsHttpTransportMetrics
for this session buffer.- Specified by:
getMetrics
in interfaceSessionInputBuffer
- Returns:
- transport metrics.
-
-