Interface IBuffer
- 
- All Known Subinterfaces:
- InternalBuffer
 
 public interface IBufferBasic unit of transport in Net4j.A buffer is well prepared for the usage with asynchronous IChannels but can also be used with pureSocketChannels. All methods ofIBufferare non-blocking.Usually buffers are obtained from a IBufferProvider. Buffers can be accessed, passed around and finallyreleasedto their original provider. The capacity of a buffer is determined by its provider.In addition to its payload data each buffer 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. 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 aSocketChanneland 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:
- IBufferProvider,- IChannel.sendBuffer(IBuffer),- IChannel.setReceiveHandler(IBufferHandler),- IBufferHandler.handleBuffer(IBuffer)
- No Implement
- This interface is not intended to be implemented by clients.
- No Extend
- This interface is not intended to be extended by clients.
 
- 
- 
Field SummaryFields Modifier and Type Field Description static intCHANNEL_ID_BYTESstatic intCHANNEL_ID_POSstatic shortHEADER_SIZEstatic shortMAX_CHANNELstatic shortMIN_CHANNELstatic shortNO_CHANNELPossible argument value ofstartPutting(short)and possible return value ofgetChannelID()that indicates that this buffer is not intended to be passed to aSocketChannel.static intPAYLOAD_SIZE_BYTESstatic intPAYLOAD_SIZE_POS
 - 
Method SummaryAll Methods Instance Methods Abstract Methods Modifier and Type Method Description voidclear()Turns the state of this buffer from any state intoINITIAL.voidcompact()voidflip()java.lang.StringformatContent(boolean showHeader)byteget()voidget(byte[] dst)IBufferProvidergetBufferProvider()Returns theIBufferProviderthat has provided this buffer and that this buffer will be returned to when itsrelease()method is called.java.nio.ByteBuffergetByteBuffer()Returns theByteBufferthat can be used for putting or getting data.shortgetCapacity()Returns the capacity of this buffer.shortgetChannelID()Returns the channel index value stored in the header of this buffer.org.eclipse.net4j.util.IErrorHandlergetErrorHandler()intgetInt()intgetLimit()intgetPosition()shortgetShort()BufferStategetState()Returns the internal state of this buffer.java.lang.StringgetString()booleanhasRemaining()booleanisCCAM()Returns the Close Channel After Me flag.booleanisEOS()Returns the End Of Stream flag to indicate whether this buffer is the last buffer in a stream (typically a signal) of buffers.voidput(byte b)voidput(byte[] src, int offset, int length)voidputShort(short value)voidrelease()Releases this buffer to its originalIBufferProvider.voidsetCCAM(boolean ccam)Sets the Close Channel After Me flag.voidsetEOS(boolean eos)Sets the End Of Stream flag to indicate whether this buffer is the last buffer in a stream (typically a signal) of buffers.voidsetErrorHandler(org.eclipse.net4j.util.IErrorHandler errorHandler)voidsetLimit(int limit)voidsetPosition(int position)java.nio.ByteBufferstartGetting(java.nio.channels.SocketChannel socketChannel)Tries to read aByteBufferfrom aSocketChannelthat can be used for getting data.java.nio.ByteBufferstartPutting(short channelID)Returns aByteBufferthat can be used for putting data.booleanwrite(java.nio.channels.SocketChannel socketChannel)Tries to write the data of this buffer to aSocketChannel.
 
- 
- 
- 
Field Detail- 
NO_CHANNELstatic final short NO_CHANNEL Possible argument value ofstartPutting(short)and possible return value ofgetChannelID()that indicates that this buffer is not intended to be passed to aSocketChannel.- See Also:
- Constant Field Values
 
 - 
MIN_CHANNELstatic final short MIN_CHANNEL - Since:
- 2.0
- See Also:
- Constant Field Values
 
 - 
MAX_CHANNELstatic final short MAX_CHANNEL - Since:
- 2.0
- See Also:
- Constant Field Values
 
 - 
CHANNEL_ID_BYTESstatic final int CHANNEL_ID_BYTES - Since:
- 4.8
- See Also:
- Constant Field Values
 
 - 
PAYLOAD_SIZE_BYTESstatic final int PAYLOAD_SIZE_BYTES - Since:
- 4.8
- See Also:
- Constant Field Values
 
 - 
HEADER_SIZEstatic final short HEADER_SIZE - See Also:
- Constant Field Values
 
 - 
CHANNEL_ID_POSstatic final int CHANNEL_ID_POS - Since:
- 4.10
- See Also:
- Constant Field Values
 
 - 
PAYLOAD_SIZE_POSstatic final int PAYLOAD_SIZE_POS - Since:
- 4.10
- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
getBufferProviderIBufferProvider getBufferProvider() Returns theIBufferProviderthat has provided this buffer and that this buffer will be returned to when itsrelease()method is called.
 - 
getChannelIDshort getChannelID() Returns the channel index value stored in the header of this buffer.- Since:
- 2.0
 
 - 
getCapacityshort getCapacity() Returns the capacity of this buffer.The capacity of this buffer is equal to the capacityof theIBufferProviderthat has provided this buffer.
 - 
getStateBufferState getState() Returns the internal state of this buffer.
 - 
startGettingjava.nio.ByteBuffer startGetting(java.nio.channels.SocketChannel socketChannel) throws java.lang.IllegalStateException, java.io.IOExceptionTries to read aByteBufferfrom aSocketChannelthat can be used for getting data.This method is non-blocking and it can be necessary to repeatedly call it. If it was not possible to read a complete header from the SocketChannelnullis returned and the state of this buffer isREADING_HEADER. If it was not possible to read a complete body from theSocketChannelnullis returned and the state of this buffer isREADING_BODY.If a ByteBufferis returned it may only be used for getting data. It is left to the responsibility of the caller that only the following methods of thatByteBufferare used:-  ByteBuffer.get()
-  ByteBuffer.get(byte[])
-  ByteBuffer.get(int)
-  ByteBuffer.get(byte[], int, int)
-  ByteBuffer.getChar()
-  ByteBuffer.getChar(int)
-  ByteBuffer.getDouble()
-  ByteBuffer.getDouble(int)
-  ByteBuffer.getFloat()
-  ByteBuffer.getFloat(int)
-  ByteBuffer.getInt()
-  ByteBuffer.getInt(int)
-  ByteBuffer.getLong()
-  ByteBuffer.getLong(int)
-  ByteBuffer.getShort()
-  ByteBuffer.getShort(int)
- all other methods that do not influence Buffer.position(),Buffer.limit()andBuffer.capacity()
 - Parameters:
- socketChannel- The- socketChannelto read the- ByteBufferfrom.
- Returns:
- A ByteBufferthat can be used for getting data if it was possible to completely read the data from the givenSocketChannel,nullotherwise.
- Throws:
- java.lang.IllegalStateException- If the state of this buffer is not- INITIAL,- READING_HEADERor- READING_BODY.
- java.io.IOException- If the- SocketChannelhas been closed or discovers other I/O problems.
 
-  
 - 
startPuttingjava.nio.ByteBuffer startPutting(short channelID) throws java.lang.IllegalStateExceptionReturns aByteBufferthat can be used for putting data.Turns the stateof this buffer intoPUTTING.The returned ByteBuffermay only be used for putting data. It is left to the responsibility of the caller that only the following methods of thatByteBufferare used:-  ByteBuffer.put(byte)
-  ByteBuffer.put(byte[])
-  ByteBuffer.put(ByteBuffer)
-  ByteBuffer.put(int, byte)
-  ByteBuffer.put(byte[], int, int)
-  ByteBuffer.putChar(char)
-  ByteBuffer.putChar(int, char)
-  ByteBuffer.putDouble(double)
-  ByteBuffer.putDouble(int, double)
-  ByteBuffer.putFloat(float)
-  ByteBuffer.putFloat(int, float)
-  ByteBuffer.putInt(int)
-  ByteBuffer.putInt(int, int)
-  ByteBuffer.putLong(long)
-  ByteBuffer.putLong(int, long)
-  ByteBuffer.putShort(short)
-  ByteBuffer.putShort(int, short)
- all other methods that do not influence Buffer.position(),Buffer.limit()andBuffer.capacity()
 - Parameters:
- channelID- The index of an- IChannelthat this buffer is intended to be passed to later or- NO_CHANNEL.
- Returns:
- A ByteBufferthat can be used for putting data.
- Throws:
- java.lang.IllegalStateException- If the state of this buffer is not- INITIAL(- PUTTINGis allowed but meaningless if and only if the given- channelIDis equal to the existing- channelIDof this buffer).
 
-  
 - 
writeboolean write(java.nio.channels.SocketChannel socketChannel) throws java.lang.IllegalStateException, java.io.IOExceptionTries to write the data of this buffer to aSocketChannel.This method is non-blocking and it can be necessary to repeatedly call it. If it was not possible to completely write the data to the SocketChannelfalseis returned and the state of this buffer remainsWRITING.- Parameters:
- socketChannel- The- socketChannelto write the data to.
- Returns:
- trueif it was possible to completely write the data to the- SocketChannel,- falseotherwise.
- Throws:
- java.lang.IllegalStateException- If the state of this buffer is not- PUTTINGor- WRITING.
- java.io.IOException- If the- SocketChannelhas been closed or discovers other I/O problems.
 
 - 
flipvoid flip() throws java.lang.IllegalStateException - Throws:
- java.lang.IllegalStateException- If the state of this buffer is not- PUTTING.
 
 - 
getByteBufferjava.nio.ByteBuffer getByteBuffer() throws java.lang.IllegalStateExceptionReturns theByteBufferthat can be used for putting or getting data.
 - 
isEOSboolean isEOS() Returns the End Of Stream flag to indicate whether this buffer is the last buffer in a stream (typically a signal) of buffers.
 - 
setEOSvoid setEOS(boolean eos) Sets the End Of Stream flag to indicate whether this buffer is the last buffer in a stream (typically a signal) of buffers.
 - 
isCCAMboolean isCCAM() Returns the Close Channel After Me flag.- Since:
- 4.4
 
 - 
setCCAMvoid setCCAM(boolean ccam) Sets the Close Channel After Me flag.- Since:
- 4.4
 
 - 
releasevoid release() Releases this buffer to its originalIBufferProvider.
 - 
clearvoid clear() Turns the state of this buffer from any state intoINITIAL.
 - 
formatContentjava.lang.String formatContent(boolean showHeader) 
 - 
getErrorHandlerorg.eclipse.net4j.util.IErrorHandler getErrorHandler() - Since:
- 2.0
 
 - 
setErrorHandlervoid setErrorHandler(org.eclipse.net4j.util.IErrorHandler errorHandler) - Since:
- 2.0
 
 - 
compactvoid compact() - Since:
- 4.7
 
 - 
getPositionint getPosition() - Since:
- 4.7
 
 - 
setPositionvoid setPosition(int position) - Since:
- 4.7
 
 - 
getLimitint getLimit() - Since:
- 4.7
 
 - 
setLimitvoid setLimit(int limit) - Since:
- 4.7
 
 - 
hasRemainingboolean hasRemaining() - Since:
- 4.7
 
 - 
getbyte get() - Since:
- 4.7
 
 - 
getvoid get(byte[] dst) - Since:
- 4.7
 
 - 
getShortshort getShort() - Since:
- 4.7
 
 - 
getIntint getInt() - Since:
- 4.7
 
 - 
getStringjava.lang.String getString() - Since:
- 4.7
 
 - 
putvoid put(byte b) - Since:
- 4.7
 
 - 
putvoid put(byte[] src, int offset, int length)- Since:
- 4.7
 
 - 
putShortvoid putShort(short value) - Since:
- 4.7
 
 
- 
 
-