Interface IBuffer

All Known Subinterfaces:
InternalBuffer

public interface IBuffer
Basic unit of transport in Net4j.

A buffer is well prepared for the usage with asynchronous IChannels but can also be used with pure SocketChannels. All methods of IBuffer are non-blocking.

Buffers may be obtained from an IBufferProvider, but implementations are not required to have been provided by one. The current owner must call release() when processing is complete; release relinquishes ownership and lets the implementation return storage to a provider, invoke an external release action, or free resources. Release does not promise that the same buffer will be pooled or reused. A released buffer must no longer be used through this interface. A previously returned ByteBuffer is a separate reference and is not revoked by a buffer state check, so callers must stop accessing it before releasing the buffer.

In addition to its payload data each Net4j frame contains an internal header of four bytes, two of them representing a channel identifier the other two of them denoting the length of the payload data. A transport that uses message boundaries must preserve this complete frame as the unit passed between the transport and Net4j; WebSocket message boundaries are transport-specific and do not replace the Net4j header. The payload data may be accessed through a ByteBuffer.

This interface is not intended to be implemented by clients.

An example for putting values into a buffer and writing it to a SocketChannel:

// Obtain a fresh buffer
Buffer buffer = bufferProvider.getBuffer(); // Start filling the buffer for channelID 4711 ByteBuffer byteBuffer =
buffer.startPutting(4711); byteBuffer.putDouble(15.47); // Write the contents of the Buffer to a // SocketChannel
without blocking while (!buffer.write(socketChannel)) { // Do something else }
An example for reading a buffer from a SocketChannel and getting values from it:

// Obtain a fresh buffer
Buffer buffer = bufferProvider.getBuffer();

// Read the contents of the Buffer from a SocketChannel without blocking
ByteBuffer byteBuffer;
while ((byteBuffer = buffer.startGetting(socketChannel)) == null)
{
  // Do something else
}

// Access the contents of the buffer and release it to its provider
double value = byteBuffer.getDouble();
buffer.release();
Author:
Eike Stepper
See Also:
No Implement
This interface is not intended to be implemented by clients.
No Extend
This interface is not intended to be extended by clients.