-
- All Superinterfaces:
AutoCloseable
,Buffer
,BufferAccessor
,Resource<Buffer>
public interface CompositeBuffer extends Buffer
TheCompositeBuffer
is a concreteBuffer
implementation that make a number of other buffers appear as one. A composite buffer behaves the same as a normal, non-composite buffer in every way, so you normally don't need to handle them specially.A composite buffer is constructed using one of the
compose
methods:-
BufferAllocator.compose(Iterable)
creates a composite buffer from the buffers that are sent to it via the passed in send objects. SinceSend.receive()
transfers ownership, the resulting composite buffer will have ownership, because it is guaranteed that there are no other references to its constituent buffers. -
BufferAllocator.compose(Send)
creates a composite buffer with a single component. SinceSend.receive()
transfers ownership, the resulting composite buffer will have ownership, because it is guaranteed that there are no other references to its constituent buffer. -
BufferAllocator.compose()
creates an empty, zero capacity, composite buffer. Such empty buffers may change their read-only states when they gain their first component.
ensureWritable(int)
, or with externally allocated buffers, usingextendWith(Send)
.How buffers compose
A buffer can be thought of as having three distinct regions, in order:- Memory that have been read.
- Memory that is readable.
- Memory that can be written to.
For example, if we compose two buffers that both have non-zero read-offset, then the composite buffer will get the read-offset of the first buffer, followed by a concatenation of the readable memory from both. Similarly, the write-offset of the composite buffer will come from the last buffer with a non-zero write-offset, and any writable memory prior to the last readable memory region will be hidden:
First buffer Second buffer +----------------------+ +--------------------+ 0| |r/o |w/o | 0| |r/o |w/o | +----+---------+-------+ +----+--------+------+ \ \ \ ,____________/ ,___________/ \ \ \/ / +----+---------+--------+------+ 0| |r/o : |w/o | Composite buffer +------------------------------+
Components in the middle can have both their end-regions hidden in the same way, so only their readable memory is included in the composite buffer. Buffers that consist entirely of memory that has already been read, or memory that is writable, can also concatenate onto those regions at the ends. The final capacity of the composite buffer, will be the sum of all the visible regions.Reads and writes to the composite buffer that modifies the read or write offsets, will also modify the relevant offsets in the constituent buffers.
Constituent buffer requirements
The buffers that are being composed must all have the same writability. Either all components must be read-only, or they must all be writable.It is not a requirement that the buffers have the same size.
It is not a requirement that the buffers are allocated by this allocator, but if
Buffer.ensureWritable(int)
is called on the composed buffer, and the composed buffer needs to be expanded, then this allocator instance will be used for allocation the extra memory.Ownership and Send
Sending a composite buffer implies sending all of its constituent buffers. For sending to be possible, both the composite buffer itself, and all of its constituent buffers, must be in a state that permits them being sent. This should be the case by default, as it shouldn't be possible to create composite buffers that can't be sent.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description CompositeBuffer
compact()
Discards the read bytes, and moves the buffer contents to the beginning of the buffer.static CompositeBuffer
compose(BufferAllocator allocator)
Create an empty composite buffer, that has no components.default CompositeBuffer
copy()
Returns a copy of this buffer's readable bytes.default CompositeBuffer
copy(int offset, int length)
Returns a copy of the given region of this buffer.CompositeBuffer
copy(int offset, int length, boolean readOnly)
Returns a copy of the given region of this buffer.Buffer[]
decomposeBuffer()
Break a composite buffer into its constituent components.default CompositeBuffer
ensureWritable(int size)
Ensures that this buffer has at least the given number of bytes of available space for writing.CompositeBuffer
ensureWritable(int size, int minimumGrowth, boolean allowCompaction)
Ensures that this buffer has at least the given number of bytes of available space for writing.CompositeBuffer
extendWith(Send<Buffer> extension)
Extend this composite buffer with the given extension buffer.CompositeBuffer
fill(byte value)
Fills the buffer with the given byte value.CompositeBuffer
implicitCapacityLimit(int limit)
Set an upper limit to the implicit capacity growth.static boolean
isComposite(Buffer composite)
Check if the given buffer is a composite buffer or not.CompositeBuffer
makeReadOnly()
Makes this buffer read-only.default CompositeBuffer
readBytes(byte[] destination, int destPos, int length)
Read from this buffer, into the destination array, the given number of bytes.default CompositeBuffer
readBytes(ByteBuffer destination)
Read from this buffer, into the destinationByteBuffer
This updates the read offset of this buffer and also the position of the destinationByteBuffer
.CompositeBuffer
readerOffset(int offset)
Set the reader offset.default CompositeBuffer
resetOffsets()
Resets the read offset and the write offset on this buffer to zero, and return this buffer.default CompositeBuffer
setBoolean(int woff, boolean value)
Set the given boolean value at the given write offset.CompositeBuffer
setByte(int woff, byte value)
Set the given byte value at the given write offset.CompositeBuffer
setChar(int woff, char value)
Set the given char value at the given write offset.CompositeBuffer
setDouble(int woff, double value)
Set the given double value at the given write offset.CompositeBuffer
setFloat(int woff, float value)
Set the given float value at the given write offset.CompositeBuffer
setInt(int woff, int value)
Set the given int value at the given write offset.CompositeBuffer
setLong(int woff, long value)
Set the given long value at the given write offset.CompositeBuffer
setMedium(int woff, int value)
Set the given int value at the given write offset.CompositeBuffer
setShort(int woff, short value)
Set the given short value at the given write offset.CompositeBuffer
setUnsignedByte(int woff, int value)
Set the given unsigned byte value at the given write offset.CompositeBuffer
setUnsignedInt(int woff, long value)
Set the given unsigned int value at the given write offset.CompositeBuffer
setUnsignedMedium(int woff, int value)
Set the given unsigned int value at the given write offset.CompositeBuffer
setUnsignedShort(int woff, int value)
Set the given unsigned short value at the given write offset.default CompositeBuffer
skipReadableBytes(int delta)
Move the reader offset forward by the given delta.default CompositeBuffer
skipWritableBytes(int delta)
Move the writer offset to ahead by the given delta.default CompositeBuffer
split()
Splits the buffer into two, at the write offset position.CompositeBuffer
split(int splitOffset)
Splits the buffer into two, at the givensplitOffset
.CompositeBuffer
splitComponentsCeil(int splitOffset)
Split this buffer at a component boundary that is greater than or equal to the given offset.CompositeBuffer
splitComponentsFloor(int splitOffset)
Split this buffer at a component boundary that is less than or equal to the given offset.default CompositeBuffer
writeBoolean(boolean value)
Write the given boolean value at the currentBuffer.writerOffset()
, and increase the writer offset byByte.BYTES
.CompositeBuffer
writeByte(byte value)
Write the given byte value at the currentBuffer.writerOffset()
, and increase the writer offset byByte.BYTES
.default CompositeBuffer
writeBytes(byte[] source)
Writes into this buffer, all the bytes from the given byte array.default CompositeBuffer
writeBytes(byte[] source, int srcPos, int length)
Writes into this buffer, the given number of bytes from the byte array.default CompositeBuffer
writeBytes(Buffer source)
Writes into this buffer, all the readable bytes from the given buffer.default CompositeBuffer
writeBytes(ByteBuffer source)
Writes into this buffer from the sourceByteBuffer
.CompositeBuffer
writeChar(char value)
Write the given char value at the currentBuffer.writerOffset()
, and increase the writer offset by 2.default CompositeBuffer
writeCharSequence(CharSequence source, Charset charset)
Writes into this buffer, all the bytes from the givensource
using the passedcharset
.CompositeBuffer
writeDouble(double value)
Write the given double value at the currentBuffer.writerOffset()
, and increase the writer offset byDouble.BYTES
.CompositeBuffer
writeFloat(float value)
Write the given float value at the currentBuffer.writerOffset()
, and increase the writer offset byFloat.BYTES
.CompositeBuffer
writeInt(int value)
Write the given int value at the currentBuffer.writerOffset()
, and increase the writer offset byInteger.BYTES
.CompositeBuffer
writeLong(long value)
Write the given long value at the currentBuffer.writerOffset()
, and increase the writer offset byLong.BYTES
.CompositeBuffer
writeMedium(int value)
Write the given int value at the currentBuffer.writerOffset()
, and increase the writer offset by 3.CompositeBuffer
writerOffset(int offset)
Set the writer offset.CompositeBuffer
writeShort(short value)
Write the given short value at the currentBuffer.writerOffset()
, and increase the writer offset byShort.BYTES
.CompositeBuffer
writeUnsignedByte(int value)
Write the given unsigned byte value at the currentBuffer.writerOffset()
, and increase the writer offset byByte.BYTES
.CompositeBuffer
writeUnsignedInt(long value)
Write the given unsigned int value at the currentBuffer.writerOffset()
, and increase the writer offset byInteger.BYTES
.CompositeBuffer
writeUnsignedMedium(int value)
Write the given unsigned int value at the currentBuffer.writerOffset()
, and increase the writer offset by 3.CompositeBuffer
writeUnsignedShort(int value)
Write the given unsigned short value at the currentBuffer.writerOffset()
, and increase the writer offset byShort.BYTES
.-
Methods inherited from interface io.netty5.buffer.api.Buffer
bytesBefore, bytesBefore, capacity, copy, copyInto, copyInto, copyInto, countComponents, countReadableComponents, countWritableComponents, forEachReadable, forEachReadable, forEachWritable, forEachWritable, implicitCapacityLimit, isDirect, openCursor, openCursor, openReverseCursor, openReverseCursor, readableBytes, readCharSequence, readerOffset, readOnly, readSplit, toString, transferFrom, transferFrom, transferTo, writableBytes, writerOffset, writeSplit
-
Methods inherited from interface io.netty5.buffer.api.BufferAccessor
getBoolean, getByte, getChar, getDouble, getFloat, getInt, getLong, getMedium, getShort, getUnsignedByte, getUnsignedInt, getUnsignedMedium, getUnsignedShort, readBoolean, readByte, readChar, readDouble, readFloat, readInt, readLong, readMedium, readShort, readUnsignedByte, readUnsignedInt, readUnsignedMedium, readUnsignedShort
-
Methods inherited from interface io.netty5.util.Resource
close, isAccessible, send, touch
-
-
-
-
Method Detail
-
compose
static CompositeBuffer compose(BufferAllocator allocator)
Create an empty composite buffer, that has no components. The buffer can be extended with components using eitherensureWritable(int)
orextendWith(Send)
.- Parameters:
allocator
- The allocator for the composite buffer. This allocator will be used e.g. to serviceensureWritable(int)
calls.- Returns:
- A composite buffer that has no components, and has a capacity of zero.
-
isComposite
static boolean isComposite(Buffer composite)
Check if the given buffer is a composite buffer or not.- Parameters:
composite
- The buffer to check.- Returns:
true
if the given buffer was created withBufferAllocator.compose()
,BufferAllocator.compose(Send)
orBufferAllocator.compose(Iterable)
,false
otherwise.
-
extendWith
CompositeBuffer extendWith(Send<Buffer> extension)
Extend this composite buffer with the given extension buffer. This works as if the extension had originally been included at the end of the list of constituent buffers when the composite buffer was created. The extension buffer is added to the end of this composite buffer, which is modified in-place.- Parameters:
extension
- The buffer to extend the composite buffer with.- Returns:
- This composite buffer instance.
- See Also:
BufferAllocator.compose(Send)
-
splitComponentsFloor
CompositeBuffer splitComponentsFloor(int splitOffset)
Split this buffer at a component boundary that is less than or equal to the given offset.This method behaves the same as
split(int)
, except no components are split.- Parameters:
splitOffset
- The maximum split offset. The real split offset will be at a component boundary that is less than or equal to this offset.- Returns:
- A new buffer with independent and exclusive ownership over the bytes from the beginning to a component boundary less than or equal to the given offset of this buffer.
-
splitComponentsCeil
CompositeBuffer splitComponentsCeil(int splitOffset)
Split this buffer at a component boundary that is greater than or equal to the given offset.This method behaves the same as
split(int)
, except no components are split.- Parameters:
splitOffset
- The minimum split offset. The real split offset will be at a component boundary that is greater than or equal to this offset.- Returns:
- A new buffer with independent and exclusive ownership over the bytes from the beginning to a component boundary greater than or equal to the given offset of this buffer.
-
decomposeBuffer
Buffer[] decomposeBuffer()
Break a composite buffer into its constituent components.This "consumes" the composite buffer, leaving the composite buffer instance as if it had been closed. The buffers in the returned array are not closed, and become owned by the caller.
- Returns:
- An array of the constituent buffer components.
-
readerOffset
CompositeBuffer readerOffset(int offset)
Description copied from interface:Buffer
Set the reader offset. Make the next read happen from the given offset into the buffer.- Specified by:
readerOffset
in interfaceBuffer
- Parameters:
offset
- The reader offset to set.- Returns:
- This Buffer.
-
writerOffset
CompositeBuffer writerOffset(int offset)
Description copied from interface:Buffer
Set the writer offset. Make the next write happen at the given offset.- Specified by:
writerOffset
in interfaceBuffer
- Parameters:
offset
- The writer offset to set.- Returns:
- This Buffer.
-
skipReadableBytes
default CompositeBuffer skipReadableBytes(int delta)
Description copied from interface:Buffer
Move the reader offset forward by the given delta.- Specified by:
skipReadableBytes
in interfaceBuffer
- Parameters:
delta
- to accumulate.- Returns:
- This buffer instance.
-
skipWritableBytes
default CompositeBuffer skipWritableBytes(int delta)
Description copied from interface:Buffer
Move the writer offset to ahead by the given delta.- Specified by:
skipWritableBytes
in interfaceBuffer
- Parameters:
delta
- to accumulate.- Returns:
- This buffer instance.
-
fill
CompositeBuffer fill(byte value)
Description copied from interface:Buffer
Fills the buffer with the given byte value. This method does not respect theBuffer.readerOffset()
orBuffer.writerOffset()
, but copies the full capacity of the buffer. TheBuffer.readerOffset()
andBuffer.writerOffset()
are not modified.
-
makeReadOnly
CompositeBuffer makeReadOnly()
Description copied from interface:Buffer
Makes this buffer read-only. This is irreversible. This operation is also idempotent, so calling this method multiple times on the same buffer makes no difference.- Specified by:
makeReadOnly
in interfaceBuffer
- Returns:
- This buffer instance.
-
writeBytes
default CompositeBuffer writeBytes(Buffer source)
Description copied from interface:Buffer
Writes into this buffer, all the readable bytes from the given buffer. This updates the write offset of this buffer, and the reader offset of the given buffer.- Specified by:
writeBytes
in interfaceBuffer
- Parameters:
source
- The buffer to read from.- Returns:
- This buffer.
-
writeBytes
default CompositeBuffer writeBytes(byte[] source)
Description copied from interface:Buffer
Writes into this buffer, all the bytes from the given byte array. This updates the write offset of this buffer by the length of the array.- Specified by:
writeBytes
in interfaceBuffer
- Parameters:
source
- The byte array to read from.- Returns:
- This buffer.
-
writeBytes
default CompositeBuffer writeBytes(byte[] source, int srcPos, int length)
Description copied from interface:Buffer
Writes into this buffer, the given number of bytes from the byte array. This updates the write offset of this buffer by the length argument.- Specified by:
writeBytes
in interfaceBuffer
- Parameters:
source
- The byte array to read from.srcPos
- Position in thesource
from where bytes should be written to this buffer.length
- The number of bytes to copy.- Returns:
- This buffer.
-
writeCharSequence
default CompositeBuffer writeCharSequence(CharSequence source, Charset charset)
Description copied from interface:Buffer
Writes into this buffer, all the bytes from the givensource
using the passedcharset
. This updates the write offset of this buffer.- Specified by:
writeCharSequence
in interfaceBuffer
- Parameters:
source
-CharSequence
to read from.charset
-Charset
to use for writing.- Returns:
- This buffer.
-
readBytes
default CompositeBuffer readBytes(byte[] destination, int destPos, int length)
Description copied from interface:Buffer
Read from this buffer, into the destination array, the given number of bytes. This updates the read offset of this buffer by the length argument.
-
writeBoolean
default CompositeBuffer writeBoolean(boolean value)
Description copied from interface:BufferAccessor
Write the given boolean value at the currentBuffer.writerOffset()
, and increase the writer offset byByte.BYTES
. A boolean gets written as a byte to this buffer. All byte values which are not equal to zero are considered as the boolean valuetrue
, zero representsfalse
.- Specified by:
writeBoolean
in interfaceBufferAccessor
- Parameters:
value
- The boolean value to write.- Returns:
- This Buffer.
-
setBoolean
default CompositeBuffer setBoolean(int woff, boolean value)
Description copied from interface:BufferAccessor
Set the given boolean value at the given write offset. TheBuffer.writerOffset()
is not modified. A boolean gets written as a byte to this buffer. All byte values which are not equal to zero are considered as the boolean valuetrue
, zero representsfalse
.- Specified by:
setBoolean
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The boolean value to write.- Returns:
- This Buffer.
-
writeBytes
default CompositeBuffer writeBytes(ByteBuffer source)
Description copied from interface:Buffer
Writes into this buffer from the sourceByteBuffer
. This updates the write offset of this buffer and also the position of the sourceByteBuffer
.Note: the behaviour is undefined if the given
ByteBuffer
is an alias for the memory in this buffer.- Specified by:
writeBytes
in interfaceBuffer
- Parameters:
source
- TheByteBuffer
to read from.- Returns:
- This buffer.
-
readBytes
default CompositeBuffer readBytes(ByteBuffer destination)
Description copied from interface:Buffer
Read from this buffer, into the destinationByteBuffer
This updates the read offset of this buffer and also the position of the destinationByteBuffer
.Note: the behaviour is undefined if the given
ByteBuffer
is an alias for the memory in this buffer.- Specified by:
readBytes
in interfaceBuffer
- Parameters:
destination
- TheByteBuffer
to write into.- Returns:
- This buffer.
-
resetOffsets
default CompositeBuffer resetOffsets()
Description copied from interface:Buffer
Resets the read offset and the write offset on this buffer to zero, and return this buffer.- Specified by:
resetOffsets
in interfaceBuffer
- Returns:
- This buffer instance.
-
ensureWritable
default CompositeBuffer ensureWritable(int size)
Description copied from interface:Buffer
Ensures that this buffer has at least the given number of bytes of available space for writing. If this buffer already has the necessary space, then this method returns immediately. If this buffer does not already have the necessary space, then it will be expanded using theBufferAllocator
the buffer was created with. This method is the same as callingBuffer.ensureWritable(int, int, boolean)
whereallowCompaction
istrue
.- Specified by:
ensureWritable
in interfaceBuffer
- Parameters:
size
- The requested number of bytes of space that should be available for writing.- Returns:
- This buffer instance.
-
ensureWritable
CompositeBuffer ensureWritable(int size, int minimumGrowth, boolean allowCompaction)
Description copied from interface:Buffer
Ensures that this buffer has at least the given number of bytes of available space for writing. If this buffer already has the necessary space, then this method returns immediately. If this buffer does not already have the necessary space, then space will be made available in one or all of the following available ways:-
If
allowCompaction
istrue
, and sum of the read and writable bytes would be enough to satisfy the request, and it (depending on the buffer implementation) seems faster and easier to compact the existing buffer rather than allocation a new buffer, then the requested bytes will be made available that way. The compaction will not necessarily work the same way as theBuffer.compact()
method, as the implementation may be able to make the requested bytes available with less effort than is strictly mandated by theBuffer.compact()
method. -
Regardless of the value of the
allowCompaction
, the implementation may make more space available by just allocating more or larger buffers. This allocation would use the sameBufferAllocator
that this buffer was created with. -
If
allowCompaction
istrue
, then the implementation may choose to do a combination of compaction and allocation.
- Specified by:
ensureWritable
in interfaceBuffer
- Parameters:
size
- The requested number of bytes of space that should be available for writing.minimumGrowth
- The minimum number of bytes to grow by. If it is determined that memory should be allocated and copied, make sure that the new memory allocation is bigger than the old one by at least this many bytes. This way, the buffer can grow by more than what is immediately necessary, thus amortising the costs of allocating and copying.allowCompaction
-true
if the method is allowed to modify the reader offset and writer offset, otherwisefalse
.- Returns:
- This buffer instance.
-
If
-
copy
default CompositeBuffer copy()
Description copied from interface:Buffer
Returns a copy of this buffer's readable bytes. Modifying the content of the returned buffer will not affect this buffers contents. The two buffers will maintain separate offsets. This method is identical tobuf.copy(buf.readerOffset(), buf.readableBytes())
. This method does not modifyBuffer.readerOffset()
orBuffer.writerOffset()
of this buffer.The copy is created with a write offset equal to the length of the copied data, so that the entire contents of the copy is ready to be read.
The returned buffer will not be read-only, regardless of the read-only state of this buffer.
- Specified by:
copy
in interfaceBuffer
- Returns:
- A new buffer instance, with independent
Buffer.readerOffset()
andBuffer.writerOffset()
, that contains a copy of the readable region of this buffer.
-
copy
default CompositeBuffer copy(int offset, int length)
Description copied from interface:Buffer
Returns a copy of the given region of this buffer. Modifying the content of the returned buffer will not affect this buffers contents. The two buffers will maintain separate offsets. This method does not modifyBuffer.readerOffset()
orBuffer.writerOffset()
of this buffer.The copy is created with a write offset equal to the length of the copy, so that the entire contents of the copy is ready to be read.
The returned buffer will not be read-only, regardless of the read-only state of this buffer. This has the same effect as calling
Buffer.copy(int, int, boolean)
with afalse
read-only argument.- Specified by:
copy
in interfaceBuffer
- Parameters:
offset
- The offset where copying should start from. This is the offset of the first byte copied.length
- The number of bytes to copy, and the capacity of the returned buffer.- Returns:
- A new buffer instance, with independent
Buffer.readerOffset()
andBuffer.writerOffset()
, that contains a copy of the given region of this buffer.
-
copy
CompositeBuffer copy(int offset, int length, boolean readOnly)
Description copied from interface:Buffer
Returns a copy of the given region of this buffer. Modifying the content of the returned buffer will not affect this buffers contents. The two buffers will maintain separate offsets. This method does not modifyBuffer.readerOffset()
orBuffer.writerOffset()
of this buffer.The copy is created with a write offset equal to the length of the copy, so that the entire contents of the copy is ready to be read.
The returned buffer will be read-only if, and only if, the
readOnly
argument istrue
, and it will not be read-only if the argument isfalse
. This is the case regardless of the read-only state of this buffer.If this buffer is read-only, and a read-only copy is requested, then implementations may use structural sharing and have both buffers backed by the same underlying memory.
- Specified by:
copy
in interfaceBuffer
- Parameters:
offset
- The offset where copying should start from. This is the offset of the first byte copied.length
- The number of bytes to copy, and the capacity of the returned buffer.readOnly
- The desiredBuffer.readOnly()
state of the returned buffer.- Returns:
- A new buffer instance, with independent
Buffer.readerOffset()
andBuffer.writerOffset()
, that contains a copy of the given region of this buffer.
-
split
default CompositeBuffer split()
Description copied from interface:Buffer
Splits the buffer into two, at the write offset position.The region of this buffer that contain the previously read and readable bytes, will be captured and returned in a new buffer, that will hold its own ownership of that region. This allows the returned buffer to be independently sent to other threads.
The returned buffer will adopt the
Buffer.readerOffset()
of this buffer, and have itsBuffer.writerOffset()
andBuffer.capacity()
both set to the equal to the write-offset of this buffer.The memory region in the returned buffer will become inaccessible through this buffer. This buffer will have its capacity reduced by the capacity of the returned buffer, and the read and write offsets of this buffer will both become zero, even though their position in memory remain unchanged.
Effectively, the following transformation takes place:
When the buffers are in this state, both of the split parts retain an atomic reference count on the underlying memory. This means that shared underlying memory will not be deallocated or returned to a pool, until all the split parts have been closed.This buffer: +------------------------------------------+ 0| |r/o |w/o |cap +---+---------------------+----------------+ / / / \ \ / / / \ \ / / / \ \ / / / \ \ / / / \ \ +---+---------------------+ +---------------+ | |r/o |w/o & cap |r/o & w/o |cap +---+---------------------+ +---------------+ Returned buffer. This buffer.
Composite buffers have it a little easier, in that at most only one of the constituent buffers will actually be split. If the split point lands perfectly between two constituent buffers, then a composite buffer can simply split its internal array in two.
Split buffers support all operations that normal buffers do, including
Buffer.ensureWritable(int)
.See the Splitting buffers section for details.
-
split
CompositeBuffer split(int splitOffset)
Description copied from interface:Buffer
Splits the buffer into two, at the givensplitOffset
.The region of this buffer that precede the
splitOffset
, will be captured and returned in a new buffer, that will hold its own ownership of that region. This allows the returned buffer to be independently sent to other threads.The returned buffer will adopt the
Buffer.readerOffset()
andBuffer.writerOffset()
of this buffer, but truncated to fit within the capacity dictated by thesplitOffset
.The memory region in the returned buffer will become inaccessible through this buffer. If the
Buffer.readerOffset()
orBuffer.writerOffset()
of this buffer lie prior to thesplitOffset
, then those offsets will be moved forward, so they land on offset 0 after the split.Effectively, the following transformation takes place:
When the buffers are in this state, both of the split parts retain an atomic reference count on the underlying memory. This means that shared underlying memory will not be deallocated or returned to a pool, until all the split parts have been closed.This buffer: +--------------------------------+ 0| |splitOffset |cap +---------------+----------------+ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ +---------------+ +---------------+ | |cap | |cap +---------------+ +---------------+ Returned buffer. This buffer.
Composite buffers have it a little easier, in that at most only one of the constituent buffers will actually be split. If the split point lands perfectly between two constituent buffers, then a composite buffer can simply split its internal array in two.
Split buffers support all operations that normal buffers do, including
Buffer.ensureWritable(int)
.See the Splitting buffers section for details.
- Specified by:
split
in interfaceBuffer
- Parameters:
splitOffset
- The offset into this buffer where it should be split. After the split, the data at this offset will be at offset zero in this buffer.- Returns:
- A new buffer with independent and exclusive ownership over the bytes from the beginning to the given offset of this buffer.
-
compact
CompositeBuffer compact()
Description copied from interface:Buffer
Discards the read bytes, and moves the buffer contents to the beginning of the buffer.
-
writeByte
CompositeBuffer writeByte(byte value)
Description copied from interface:BufferAccessor
Write the given byte value at the currentBuffer.writerOffset()
, and increase the writer offset byByte.BYTES
. The value is written using a two's complement 8-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeByte
in interfaceBufferAccessor
- Parameters:
value
- The byte value to write.- Returns:
- This Buffer.
-
setByte
CompositeBuffer setByte(int woff, byte value)
Description copied from interface:BufferAccessor
Set the given byte value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a two's complement 8-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setByte
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The byte value to write.- Returns:
- This Buffer.
-
writeUnsignedByte
CompositeBuffer writeUnsignedByte(int value)
Description copied from interface:BufferAccessor
Write the given unsigned byte value at the currentBuffer.writerOffset()
, and increase the writer offset byByte.BYTES
. The value is written using an unsigned two's complement 8-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeUnsignedByte
in interfaceBufferAccessor
- Parameters:
value
- The int value to write.- Returns:
- This Buffer.
-
setUnsignedByte
CompositeBuffer setUnsignedByte(int woff, int value)
Description copied from interface:BufferAccessor
Set the given unsigned byte value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using an unsigned two's complement 8-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setUnsignedByte
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The int value to write.- Returns:
- This Buffer.
-
writeChar
CompositeBuffer writeChar(char value)
Description copied from interface:BufferAccessor
Write the given char value at the currentBuffer.writerOffset()
, and increase the writer offset by 2. The value is written using a 2-byte UTF-16 encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeChar
in interfaceBufferAccessor
- Parameters:
value
- The char value to write.- Returns:
- This Buffer.
-
setChar
CompositeBuffer setChar(int woff, char value)
Description copied from interface:BufferAccessor
Set the given char value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a 2-byte UTF-16 encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setChar
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The char value to write.- Returns:
- This Buffer.
-
writeShort
CompositeBuffer writeShort(short value)
Description copied from interface:BufferAccessor
Write the given short value at the currentBuffer.writerOffset()
, and increase the writer offset byShort.BYTES
. The value is written using a two's complement 16-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeShort
in interfaceBufferAccessor
- Parameters:
value
- The short value to write.- Returns:
- This Buffer.
-
setShort
CompositeBuffer setShort(int woff, short value)
Description copied from interface:BufferAccessor
Set the given short value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a two's complement 16-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setShort
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The short value to write.- Returns:
- This Buffer.
-
writeUnsignedShort
CompositeBuffer writeUnsignedShort(int value)
Description copied from interface:BufferAccessor
Write the given unsigned short value at the currentBuffer.writerOffset()
, and increase the writer offset byShort.BYTES
. The value is written using an unsigned two's complement 16-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeUnsignedShort
in interfaceBufferAccessor
- Parameters:
value
- The int value to write.- Returns:
- This Buffer.
-
setUnsignedShort
CompositeBuffer setUnsignedShort(int woff, int value)
Description copied from interface:BufferAccessor
Set the given unsigned short value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using an unsigned two's complement 16-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setUnsignedShort
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The int value to write.- Returns:
- This Buffer.
-
writeMedium
CompositeBuffer writeMedium(int value)
Description copied from interface:BufferAccessor
Write the given int value at the currentBuffer.writerOffset()
, and increase the writer offset by 3. The value is written using a two's complement 24-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeMedium
in interfaceBufferAccessor
- Parameters:
value
- The int value to write.- Returns:
- This Buffer.
-
setMedium
CompositeBuffer setMedium(int woff, int value)
Description copied from interface:BufferAccessor
Set the given int value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a two's complement 24-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setMedium
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The int value to write.- Returns:
- This Buffer.
-
writeUnsignedMedium
CompositeBuffer writeUnsignedMedium(int value)
Description copied from interface:BufferAccessor
Write the given unsigned int value at the currentBuffer.writerOffset()
, and increase the writer offset by 3. The value is written using an unsigned two's complement 24-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeUnsignedMedium
in interfaceBufferAccessor
- Parameters:
value
- The int value to write.- Returns:
- This Buffer.
-
setUnsignedMedium
CompositeBuffer setUnsignedMedium(int woff, int value)
Description copied from interface:BufferAccessor
Set the given unsigned int value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using an unsigned two's complement 24-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setUnsignedMedium
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The int value to write.- Returns:
- This Buffer.
-
writeInt
CompositeBuffer writeInt(int value)
Description copied from interface:BufferAccessor
Write the given int value at the currentBuffer.writerOffset()
, and increase the writer offset byInteger.BYTES
. The value is written using a two's complement 32-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeInt
in interfaceBufferAccessor
- Parameters:
value
- The int value to write.- Returns:
- This Buffer.
-
setInt
CompositeBuffer setInt(int woff, int value)
Description copied from interface:BufferAccessor
Set the given int value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a two's complement 32-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setInt
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The int value to write.- Returns:
- This Buffer.
-
writeUnsignedInt
CompositeBuffer writeUnsignedInt(long value)
Description copied from interface:BufferAccessor
Write the given unsigned int value at the currentBuffer.writerOffset()
, and increase the writer offset byInteger.BYTES
. The value is written using an unsigned two's complement 32-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeUnsignedInt
in interfaceBufferAccessor
- Parameters:
value
- The long value to write.- Returns:
- This Buffer.
-
setUnsignedInt
CompositeBuffer setUnsignedInt(int woff, long value)
Description copied from interface:BufferAccessor
Set the given unsigned int value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using an unsigned two's complement 32-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setUnsignedInt
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The long value to write.- Returns:
- This Buffer.
-
writeFloat
CompositeBuffer writeFloat(float value)
Description copied from interface:BufferAccessor
Write the given float value at the currentBuffer.writerOffset()
, and increase the writer offset byFloat.BYTES
. The value is written using a 32-bit IEEE floating point encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeFloat
in interfaceBufferAccessor
- Parameters:
value
- The float value to write.- Returns:
- This Buffer.
-
setFloat
CompositeBuffer setFloat(int woff, float value)
Description copied from interface:BufferAccessor
Set the given float value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a 32-bit IEEE floating point encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setFloat
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The float value to write.- Returns:
- This Buffer.
-
writeLong
CompositeBuffer writeLong(long value)
Description copied from interface:BufferAccessor
Write the given long value at the currentBuffer.writerOffset()
, and increase the writer offset byLong.BYTES
. The value is written using a two's complement 64-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeLong
in interfaceBufferAccessor
- Parameters:
value
- The long value to write.- Returns:
- This Buffer.
-
setLong
CompositeBuffer setLong(int woff, long value)
Description copied from interface:BufferAccessor
Set the given long value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a two's complement 64-bit encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setLong
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The long value to write.- Returns:
- This Buffer.
-
writeDouble
CompositeBuffer writeDouble(double value)
Description copied from interface:BufferAccessor
Write the given double value at the currentBuffer.writerOffset()
, and increase the writer offset byDouble.BYTES
. The value is written using a 64-bit IEEE floating point encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
writeDouble
in interfaceBufferAccessor
- Parameters:
value
- The double value to write.- Returns:
- This Buffer.
-
setDouble
CompositeBuffer setDouble(int woff, double value)
Description copied from interface:BufferAccessor
Set the given double value at the given write offset. TheBuffer.writerOffset()
is not modified. The value is written using a 64-bit IEEE floating point encoding, inByteOrder.BIG_ENDIAN
byte order.- Specified by:
setDouble
in interfaceBufferAccessor
- Parameters:
woff
- The write offset, an absolute offset into this buffer to write to.value
- The double value to write.- Returns:
- This Buffer.
-
implicitCapacityLimit
CompositeBuffer implicitCapacityLimit(int limit)
Description copied from interface:Buffer
Set an upper limit to the implicit capacity growth. Bufferwrite*
methods may implicitly grow the buffer capacity instead of throwing a bounds check exception. The implicit capacity limit restricts this growth so the buffer capacity does not automatically grow beyond the given limit. When the limit is reached, and there is no more writable space left, then thewrite*
methods will start throwing exceptions.The default limit is the maximum buffer size.
The limit is carried through
Resource.send()
calls, but the buffer instances returned from the varioussplit
andcopy
methods will have the default limit set.The limit is not impacted by calls to
split
methods on this buffer. In other words, even thoughsplit
methods reduce the capacity of this buffer, the set limit, if any, remains the same.- Specified by:
implicitCapacityLimit
in interfaceBuffer
- Parameters:
limit
- The maximum size this buffers capacity will implicitly grow to viawrite*
methods.- Returns:
- This buffer instance.
-
-