public class Buffer extends Object
Constructor and Description |
---|
Buffer(ByteBuffer byteBuffer)
Constructor.
|
Buffer(ByteBuffer byteBuffer,
BufferState byteBufferState)
Constructor.
|
Buffer(int bufferSize)
Constructor.
|
Buffer(int bufferSize,
boolean direct)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
beforeDrain()
Ensure that the buffer is ready to be drained, flipping it if necessary
only.
|
void |
beforeFill()
Ensure that the buffer is ready to be filled, flipping it if necessary
only.
|
boolean |
canCompact()
Indicates if a compacting operation can be beneficial.
|
boolean |
canDrain()
Indicates if more bytes can be drained.
|
boolean |
canFill()
Indicates if more bytes can be filled in.
|
int |
capacity()
Returns the maximum capacity of this buffer.
|
void |
clear()
Recycles the buffer so it can be reused.
|
void |
compact()
Compacts the bytes to be drained at the beginning of the buffer.
|
boolean |
couldDrain()
Indicates if bytes could be drained by flipping the buffer.
|
boolean |
couldFill()
Indicates if more bytes could be filled in.
|
int |
drain()
Drains the next byte in the buffer and returns it as an integer.
|
void |
drain(byte[] targetArray,
int offset,
int length)
Drains the buffer into a byte array.
|
int |
drain(ByteBuffer targetBuffer)
Drains the byte buffer by copying as many bytes as possible to the target
buffer, with no modification.
|
int |
drain(ByteBuffer targetBuffer,
long maxDrained)
Drains the byte buffer by copying as many bytes as possible to the target
buffer, with no modification.
|
BufferState |
drain(StringBuilder lineBuilder,
BufferState builderState)
Drains the buffer into a line builder (start line or header line).
|
int |
drain(WritableByteChannel wbc)
Drains the byte buffer by attempting to write as much as possible on the
given channel.
|
void |
fill(byte[] sourceBuffer)
Fills the byte buffer by copying as many bytes as possible from the
source buffer, with no modification.
|
int |
fill(ByteBuffer sourceBuffer)
Fills the byte buffer by copying as many bytes as possible from the
source buffer, with no modification.
|
int |
fill(ByteBuffer sourceBuffer,
long maxFilled)
Fills the byte buffer by copying as many bytes as possible from the
source buffer, with no modification.
|
int |
fill(ReadableByteChannel sourceChannel)
Refills the byte buffer.
|
void |
fill(String source)
Fills the byte buffer by copying as many bytes as possible from the
source string, using the default platform encoding.
|
void |
flip()
Flip from draining to filling or the other way around.
|
ByteBuffer |
getBytes()
Returns the byte buffer.
|
Object |
getLock()
Returns the lock on which multiple thread can synchronize to ensure safe
access to the underlying byte buffer which isn't thread safe.
|
BufferState |
getState()
Returns the byte buffer IO state.
|
boolean |
hasRemaining()
Indicates if the buffer has remaining bytes to be read or written.
|
boolean |
isDraining()
Indicates if the buffer state has the
BufferState.DRAINING value. |
boolean |
isEmpty()
Indicates if the buffer is empty in either filling or draining state.
|
boolean |
isFilling()
Indicates if the buffer state has the
BufferState.FILLING value. |
int |
process(BufferProcessor processor,
int maxDrained,
Object... args)
Processes as a loop the IO event by draining or filling the IO buffer.
|
int |
remaining()
Returns the number of bytes that can be read or written in the byte
buffer.
|
void |
setState(BufferState byteBufferState)
Sets the byte buffer IO state.
|
String |
toString() |
public Buffer(ByteBuffer byteBuffer)
byteBuffer
- The byte buffer wrapped.public Buffer(ByteBuffer byteBuffer, BufferState byteBufferState)
byteBuffer
- The byte buffer wrapped.byteBufferState
- The initial byte buffer state.public Buffer(int bufferSize)
bufferSize
- The byte buffer size.public Buffer(int bufferSize, boolean direct)
ByteBuffer.allocate(int)
or
ByteBuffer.allocateDirect(int)
methods.bufferSize
- The byte buffer size.direct
- Indicates if a direct NIO buffer should be created.public void beforeDrain()
public void beforeFill()
public boolean canCompact()
public boolean canDrain()
public boolean canFill()
public final int capacity()
public void clear()
public void compact()
public boolean couldDrain()
public boolean couldFill()
public int drain()
public void drain(byte[] targetArray, int offset, int length)
targetArray
- The target byte array.offset
- The target offset.length
- The number of bytes to drain.public int drain(ByteBuffer targetBuffer)
targetBuffer
- The target buffer.public int drain(ByteBuffer targetBuffer, long maxDrained)
targetBuffer
- The target buffer.maxDrained
- The maximum number of bytes drained by this call or 0 for
unlimited length.public BufferState drain(StringBuilder lineBuilder, BufferState builderState) throws IOException
lineBuilder
- The line builder to fill.builderState
- The builder state.IOException
public int drain(WritableByteChannel wbc) throws IOException
wbc
- The byte channel to write to.IOException
public void fill(byte[] sourceBuffer)
sourceBuffer
- The source buffer.public int fill(ByteBuffer sourceBuffer)
sourceBuffer
- The source buffer.public int fill(ByteBuffer sourceBuffer, long maxFilled)
sourceBuffer
- The source buffer.maxFilled
- The maximum number of bytes filled by this call or 0 for
unlimited length.public int fill(ReadableByteChannel sourceChannel) throws IOException
sourceChannel
- The byte channel to read from.IOException
public void fill(String source)
source
- The source string.public void flip()
public ByteBuffer getBytes()
public Object getLock()
public BufferState getState()
public final boolean hasRemaining()
public boolean isDraining()
BufferState.DRAINING
value.BufferState.DRAINING
value.public boolean isEmpty()
public boolean isFilling()
BufferState.FILLING
value.BufferState.FILLING
value.public int process(BufferProcessor processor, int maxDrained, Object... args) throws IOException
getLock()
object is
automatically made.processor
- The IO processor to callback.maxDrained
- The maximum number of bytes drained by this call or 0 for
unlimited length.args
- The optional arguments to pass back to the callbacks.IOException
public final int remaining()
public void setState(BufferState byteBufferState)
byteBufferState
- The byte buffer IO state.Copyright © 2005–2015. All rights reserved.