org.restlet.engine.io
Class Buffer

java.lang.Object
  extended by org.restlet.engine.io.Buffer

public class Buffer
extends Object

Wrapper around a byte buffer and its state.

Author:
Jerome Louvel

Constructor Summary
Buffer(ByteBuffer byteBuffer)
          Constructor.
Buffer(ByteBuffer byteBuffer, BufferState byteBufferState)
          Constructor.
Buffer(int bufferSize)
          Constructor.
Buffer(int bufferSize, boolean direct)
          Constructor.
 
Method Summary
 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()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Buffer

public Buffer(ByteBuffer byteBuffer)
Constructor.

Parameters:
byteBuffer - The byte buffer wrapped.

Buffer

public Buffer(ByteBuffer byteBuffer,
              BufferState byteBufferState)
Constructor.

Parameters:
byteBuffer - The byte buffer wrapped.
byteBufferState - The initial byte buffer state.

Buffer

public Buffer(int bufferSize)
Constructor. Allocates a new non-direct byte buffer.

Parameters:
bufferSize - The byte buffer size.

Buffer

public Buffer(int bufferSize,
              boolean direct)
Constructor. Allocates a new byte buffer using ByteBuffer.allocate(int) or ByteBuffer.allocateDirect(int) methods.

Parameters:
bufferSize - The byte buffer size.
direct - Indicates if a direct NIO buffer should be created.
Method Detail

beforeDrain

public void beforeDrain()
Ensure that the buffer is ready to be drained, flipping it if necessary only.


beforeFill

public void beforeFill()
Ensure that the buffer is ready to be filled, flipping it if necessary only.


canCompact

public boolean canCompact()
Indicates if a compacting operation can be beneficial.

Returns:
True if a compacting operation can be beneficial.

canDrain

public boolean canDrain()
Indicates if more bytes can be drained.

Returns:
True if more bytes can be drained.

canFill

public boolean canFill()
Indicates if more bytes can be filled in.

Returns:
True if more bytes can be filled in.

capacity

public final int capacity()
Returns the maximum capacity of this buffer.

Returns:
The maximum capacity of this buffer.

clear

public void clear()
Recycles the buffer so it can be reused.


compact

public void compact()
Compacts the bytes to be drained at the beginning of the buffer.


couldDrain

public boolean couldDrain()
Indicates if bytes could be drained by flipping the buffer.

Returns:
True if bytes could be drained.

couldFill

public boolean couldFill()
Indicates if more bytes could be filled in.

Returns:
True if more bytes could be filled in.

drain

public int drain()
Drains the next byte in the buffer and returns it as an integer.

Returns:
The next byte in the buffer;

drain

public void drain(byte[] targetArray,
                  int offset,
                  int length)
Drains the buffer into a byte array.

Parameters:
targetArray - The target byte array.
offset - The target offset.
length - The number of bytes to drain.

drain

public int drain(ByteBuffer targetBuffer)
Drains the byte buffer by copying as many bytes as possible to the target buffer, with no modification.

Parameters:
targetBuffer - The target buffer.
Returns:
The number of bytes added to the target buffer.

drain

public int drain(ByteBuffer targetBuffer,
                 long maxDrained)
Drains the byte buffer by copying as many bytes as possible to the target buffer, with no modification.

Parameters:
targetBuffer - The target buffer.
maxDrained - The maximum number of bytes drained by this call or 0 for unlimited length.
Returns:
The number of bytes added to the target buffer.

drain

public BufferState drain(StringBuilder lineBuilder,
                         BufferState builderState)
                  throws IOException
Drains the buffer into a line builder (start line or header line).

Parameters:
lineBuilder - The line builder to fill.
builderState - The builder state.
Returns:
The new builder state.
Throws:
IOException

drain

public int drain(WritableByteChannel wbc)
          throws IOException
Drains the byte buffer by attempting to write as much as possible on the given channel.

Parameters:
wbc - The byte channel to write to.
Returns:
The number of bytes written.
Throws:
IOException

fill

public void fill(byte[] sourceBuffer)
Fills the byte buffer by copying as many bytes as possible from the source buffer, with no modification.

Parameters:
sourceBuffer - The source buffer.

fill

public int fill(ByteBuffer sourceBuffer)
Fills the byte buffer by copying as many bytes as possible from the source buffer, with no modification.

Parameters:
sourceBuffer - The source buffer.
Returns:
The number of bytes added from the source buffer.

fill

public int fill(ByteBuffer sourceBuffer,
                long maxFilled)
Fills the byte buffer by copying as many bytes as possible from the source buffer, with no modification.

Parameters:
sourceBuffer - The source buffer.
maxFilled - The maximum number of bytes filled by this call or 0 for unlimited length.
Returns:
The number of bytes added from the source buffer.

fill

public int fill(ReadableByteChannel sourceChannel)
         throws IOException
Refills the byte buffer.

Parameters:
sourceChannel - The byte channel to read from.
Returns:
The number of bytes read and added or -1 if end of the source channel reached.
Throws:
IOException

fill

public void fill(String source)
Fills the byte buffer by copying as many bytes as possible from the source string, using the default platform encoding.

Parameters:
source - The source string.

flip

public void flip()
Flip from draining to filling or the other way around.


getBytes

public ByteBuffer getBytes()
Returns the byte buffer.

Returns:
The byte buffer.

getLock

public 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.

Returns:
The lock on which multiple thread can synchronize.

getState

public BufferState getState()
Returns the byte buffer IO state.

Returns:
The byte buffer IO state.

hasRemaining

public final boolean hasRemaining()
Indicates if the buffer has remaining bytes to be read or written.

Returns:
True if the buffer has remaining bytes to be read or written.

isDraining

public boolean isDraining()
Indicates if the buffer state has the BufferState.DRAINING value.

Returns:
True if the buffer state has the BufferState.DRAINING value.

isEmpty

public boolean isEmpty()
Indicates if the buffer is empty in either filling or draining state.

Returns:
True if the buffer is empty.

isFilling

public boolean isFilling()
Indicates if the buffer state has the BufferState.FILLING value.

Returns:
True if the buffer state has the BufferState.FILLING value.

process

public int process(BufferProcessor processor,
                   int maxDrained,
                   Object... args)
            throws IOException
Processes as a loop the IO event by draining or filling the IO buffer. Note that synchronization of the getLock() object is automatically made.

Parameters:
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.
Returns:
The number of bytes drained or -1 if the filling source has ended.
Throws:
IOException

remaining

public final int remaining()
Returns the number of bytes that can be read or written in the byte buffer.

Returns:
The number of bytes that can be read or written.

setState

public void setState(BufferState byteBufferState)
Sets the byte buffer IO state.

Parameters:
byteBufferState - The byte buffer IO state.

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2005-2013 Restlet.