Package org.apache.http.nio.util
Class ExpandableBuffer
- java.lang.Object
-
- org.apache.http.nio.util.ExpandableBuffer
-
- All Implemented Interfaces:
BufferInfo,BufferInfo
- Direct Known Subclasses:
SessionInputBufferImpl,SessionOutputBufferImpl,SharedInputBuffer,SharedOutputBuffer,SimpleInputBuffer,SimpleOutputBuffer
public class ExpandableBuffer extends java.lang.Object implements BufferInfo, BufferInfo
A buffer that expand its capacity on demand usingByteBufferAllocatorinterface. Internally, this class is backed by an instance ofByteBuffer.This class is not thread safe.
- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description private ByteBufferAllocatorallocatorprotected java.nio.ByteBufferbufferstatic intINPUT_MODEprivate intmodestatic intOUTPUT_MODE
-
Constructor Summary
Constructors Constructor Description ExpandableBuffer(int bufferSize, ByteBufferAllocator allocator)Allocates buffer of the given size using the given allocator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns available capacity of this buffer.intcapacity()Returns the total capacity of this buffer.protected voidclear()Clears buffer.protected voidensureCapacity(int requiredCapacity)Ensures the buffer can accommodate the required capacity.protected voidexpand()Expands buffer's capacity.private voidexpandCapacity(int capacity)protected intgetMode()Returns the current mode:booleanhasData()Determines if the buffer contains data.intlength()Returns the length of this buffer.protected voidsetInputMode()Sets input mode.protected voidsetOutputMode()Sets output mode.java.lang.StringtoString()
-
-
-
Field Detail
-
INPUT_MODE
public static final int INPUT_MODE
- See Also:
- Constant Field Values
-
OUTPUT_MODE
public static final int OUTPUT_MODE
- See Also:
- Constant Field Values
-
allocator
private final ByteBufferAllocator allocator
-
mode
private int mode
-
buffer
protected java.nio.ByteBuffer buffer
-
-
Constructor Detail
-
ExpandableBuffer
public ExpandableBuffer(int bufferSize, ByteBufferAllocator allocator)Allocates buffer of the given size using the given allocator.- Parameters:
bufferSize- the buffer size.allocator- allocator to be used to allocateByteBuffers.
-
-
Method Detail
-
getMode
protected int getMode()
Returns the current mode:INPUT_MODE: the buffer is in the input mode.OUTPUT_MODE: the buffer is in the output mode.- Returns:
- current input/output mode.
-
setOutputMode
protected void setOutputMode()
Sets output mode. The buffer can now be read from.
-
setInputMode
protected void setInputMode()
Sets input mode. The buffer can now be written into.
-
expandCapacity
private void expandCapacity(int capacity)
-
expand
protected void expand() throws java.nio.BufferOverflowExceptionExpands buffer's capacity.- Throws:
java.nio.BufferOverflowException- in case we get over the maximum allowed value
-
ensureCapacity
protected void ensureCapacity(int requiredCapacity)
Ensures the buffer can accommodate the required capacity.
-
capacity
public int capacity()
Returns the total capacity of this buffer.- Specified by:
capacityin interfaceBufferInfo- Specified by:
capacityin interfaceBufferInfo- Returns:
- total capacity.
-
hasData
public boolean hasData()
Determines if the buffer contains data.- Returns:
trueif there is data in the buffer,falseotherwise.
-
length
public int length()
Returns the length of this buffer.- Specified by:
lengthin interfaceBufferInfo- Specified by:
lengthin interfaceBufferInfo- Returns:
- buffer length.
-
available
public int available()
Returns available capacity of this buffer.- Specified by:
availablein interfaceBufferInfo- Specified by:
availablein interfaceBufferInfo- Returns:
- buffer length.
-
clear
protected void clear()
Clears buffer.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-