Class ByteBufBuffer
- java.lang.Object
-
- io.netty5.buffer.api.internal.ResourceSupport<Buffer,ByteBufBuffer>
-
- io.netty5.buffer.api.adaptor.ByteBufBuffer
-
- All Implemented Interfaces:
Buffer,BufferAccessor,Resource<Buffer>,AutoCloseable
public final class ByteBufBuffer extends ResourceSupport<Buffer,ByteBufBuffer> implements Buffer
An implementation of theBufferinterface, that wraps aByteBuf.This can be used for bridging
ByteBufbased andBufferbased handlers. It can also be used as a faithful and completeBufferimplementation, although some operations may incur some extra overhead compared to the other implementations.To create an instance, you can either use the regular
BufferandBufferAllocatorAPIs, or you can allocate aByteBufyourself, and then wrap it.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intbytesBefore(byte needle)Get the number of readable bytes, until the givenneedleis found in this buffer.intcapacity()The capacity of this buffer, that is, the maximum number of bytes it can contain.Buffercompact()Discards the read bytes, and moves the buffer contents to the beginning of the buffer.Buffercopy(int offset, int length)Returns a copy of the given region of this buffer.voidcopyInto(int srcPos, byte[] dest, int destPos, int length)Copies the given length of data from this buffer into the given destination array, beginning at the given source position in this buffer, and the given destination position in the destination array.voidcopyInto(int srcPos, Buffer dest, int destPos, int length)Copies the given length of data from this buffer into the given destination buffer, beginning at the given source position in this buffer, and the given destination position in the destination buffer.voidcopyInto(int srcPos, ByteBuffer dest, int destPos, int length)Copies the given length of data from this buffer into the given destination byte buffer, beginning at the given source position in this buffer, and the given destination position in the destination byte buffer.intcountComponents()Get the number of "components" in this buffer.intcountReadableComponents()Get the number of "components" in this buffer, that are readable.intcountWritableComponents()Get the number of "components" in this buffer, that are writable.protected RuntimeExceptioncreateResourceClosedException()BufferensureWritable(int size, int minimumGrowth, boolean allowCompaction)Ensures that this buffer has at least the given number of bytes of available space for writing.booleanequals(Object obj)Bufferfill(byte value)Fills the buffer with the given byte value.<E extends Exception>
intforEachReadable(int initialIndex, ReadableComponentProcessor<E> processor)Processes all readable components of this buffer, and return the number of components processed.<E extends Exception>
intforEachWritable(int initialIndex, WritableComponentProcessor<E> processor)Process all writable components of this buffer, and return the number of components processed.bytegetByte(int roff)Get the byte value at the given reader offset.chargetChar(int roff)Get the char value at the given reader offset.doublegetDouble(int roff)Get the double value at the given reader offset.floatgetFloat(int roff)Get the float value at the given reader offset.intgetInt(int roff)Get the int value at the given reader offset.longgetLong(int roff)Get the long value at the given reader offset.intgetMedium(int roff)Get the int value at the given reader offset.shortgetShort(int roff)Get the short value at the given reader offset.intgetUnsignedByte(int roff)Get the unsigned byte value at the given reader offset.longgetUnsignedInt(int roff)Get the unsigned int value at the given reader offset.intgetUnsignedMedium(int roff)Get the unsigned int value at the given reader offset.intgetUnsignedShort(int roff)Get the unsigned short value at the given reader offset.inthashCode()booleanisDirect()Queries if this buffer is backed by native memory, or not.protected voidmakeInaccessible()Called when this resource needs to be considered inaccessible.BuffermakeReadOnly()Makes this buffer read-only.ByteCursoropenCursor()Opens a cursor to iterate the readable bytes of this buffer.ByteCursoropenCursor(int fromOffset, int length)Opens a cursor to iterate the given number bytes of this buffer, starting at the given offset.ByteCursoropenReverseCursor(int fromOffset, int length)Opens a cursor to iterate the given number bytes of this buffer, in reverse, starting at the given offset.protected Owned<ByteBufBuffer>prepareSend()Prepare this instance for ownership transfer.intreadableBytes()Returns the number of readable bytes which is equal to(writerOffset() - readerOffset()).bytereadByte()Read the byte value at the currentBuffer.readerOffset(), and increases the reader offset byByte.BYTES.charreadChar()Read the char value at the currentBuffer.readerOffset(), and increases the reader offset by 2.doublereadDouble()Read the double value at the currentBuffer.readerOffset(), and increases the reader offset byDouble.BYTES.intreaderOffset()Get the current reader offset.BufferreaderOffset(int offset)Set the reader offset.floatreadFloat()Read the float value at the currentBuffer.readerOffset(), and increases the reader offset byFloat.BYTES.intreadInt()Read the int value at the currentBuffer.readerOffset(), and increases the reader offset byInteger.BYTES.longreadLong()Read the long value at the currentBuffer.readerOffset(), and increases the reader offset byLong.BYTES.intreadMedium()Read the int value at the currentBuffer.readerOffset(), and increases the reader offset by 3.booleanreadOnly()Queries if this buffer is read-only or not.shortreadShort()Read the short value at the currentBuffer.readerOffset(), and increases the reader offset byShort.BYTES.intreadUnsignedByte()Read the unsigned byte value at the currentBuffer.readerOffset(), and increases the reader offset byByte.BYTES.longreadUnsignedInt()Read the unsigned int value at the currentBuffer.readerOffset(), and increases the reader offset byInteger.BYTES.intreadUnsignedMedium()Read the unsigned int value at the currentBuffer.readerOffset(), and increases the reader offset by 3.intreadUnsignedShort()Read the unsigned short value at the currentBuffer.readerOffset(), and increases the reader offset byShort.BYTES.BuffersetByte(int woff, byte value)Set the given byte value at the given write offset.BuffersetChar(int woff, char value)Set the given char value at the given write offset.BuffersetDouble(int woff, double value)Set the given double value at the given write offset.BuffersetFloat(int woff, float value)Set the given float value at the given write offset.BuffersetInt(int woff, int value)Set the given int value at the given write offset.BuffersetLong(int woff, long value)Set the given long value at the given write offset.BuffersetMedium(int woff, int value)Set the given int value at the given write offset.BuffersetShort(int woff, short value)Set the given short value at the given write offset.BuffersetUnsignedByte(int woff, int value)Set the given unsigned byte value at the given write offset.BuffersetUnsignedInt(int woff, long value)Set the given unsigned int value at the given write offset.BuffersetUnsignedMedium(int woff, int value)Set the given unsigned int value at the given write offset.BuffersetUnsignedShort(int woff, int value)Set the given unsigned short value at the given write offset.voidskipReadable(int delta)Set the reader offset toreaderOffset() + delta.voidskipWritable(int delta)Set the write offset towriterOffset() + delta.Buffersplit(int splitOffset)Splits the buffer into two, at the givensplitOffset.StringtoString()inttransferFrom(ReadableByteChannel channel, int length)Read from the given channel and write to this buffer.inttransferTo(WritableByteChannel channel, int length)Read from this buffer and write to the given channel.ByteBufunwrapAndClose()static Bufferwrap(ByteBuf byteBuf)intwritableBytes()Returns the number of writable bytes which is equal to(capacity() - writerOffset()).BufferwriteByte(byte value)Write the given byte value at the currentBuffer.writerOffset(), and increase the writer offset byByte.BYTES.BufferwriteChar(char value)Write the given char value at the currentBuffer.writerOffset(), and increase the writer offset by 2.BufferwriteDouble(double value)Write the given double value at the currentBuffer.writerOffset(), and increase the writer offset byDouble.BYTES.BufferwriteFloat(float value)Write the given float value at the currentBuffer.writerOffset(), and increase the writer offset byFloat.BYTES.BufferwriteInt(int value)Write the given int value at the currentBuffer.writerOffset(), and increase the writer offset byInteger.BYTES.BufferwriteLong(long value)Write the given long value at the currentBuffer.writerOffset(), and increase the writer offset byLong.BYTES.BufferwriteMedium(int value)Write the given int value at the currentBuffer.writerOffset(), and increase the writer offset by 3.intwriterOffset()Get the current writer offset.BufferwriterOffset(int offset)Set the writer offset.BufferwriteShort(short value)Write the given short value at the currentBuffer.writerOffset(), and increase the writer offset byShort.BYTES.BufferwriteUnsignedByte(int value)Write the given unsigned byte value at the currentBuffer.writerOffset(), and increase the writer offset byByte.BYTES.BufferwriteUnsignedInt(long value)Write the given unsigned int value at the currentBuffer.writerOffset(), and increase the writer offset byInteger.BYTES.BufferwriteUnsignedMedium(int value)Write the given unsigned int value at the currentBuffer.writerOffset(), and increase the writer offset by 3.BufferwriteUnsignedShort(int value)Write the given unsigned short value at the currentBuffer.writerOffset(), and increase the writer offset byShort.BYTES.-
Methods inherited from class io.netty5.buffer.api.internal.ResourceSupport
acquire, attachTrace, close, countBorrows, isAccessible, isOwned, notSendableException, send, touch, unsafeGetDrop, unsafeSetDrop
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty5.buffer.api.Buffer
copy, ensureWritable, openReverseCursor, readBytes, readCharSequence, readSplit, resetOffsets, split, toString, writeBytes, writeBytes, writeBytes, writeCharSequence, writeSplit
-
Methods inherited from interface io.netty5.buffer.api.Resource
close, isAccessible, send, touch
-
-
-
-
Method Detail
-
wrap
public static Buffer wrap(ByteBuf byteBuf)
Wrap the givenByteBufinstance in aBufferimplementation.Note: This can be used to create aliased buffers, which violates the
Bufferinterface contract! AByteBufshould only be wrapped once, and not shared with anything else.Note: This method does not increase the reference count of the given
ByteBuf.
-
unwrapAndClose
public ByteBuf unwrapAndClose()
Unwrap thisBufferinstance by returning the innerByteBufinstance.This method destroys this
Bufferinstance, as ifResourceSupport.close()is called upon it, but without harm to the returnedByteBufinstance.- Returns:
- The inner
ByteBufinstance.
-
createResourceClosedException
protected RuntimeException createResourceClosedException()
- Specified by:
createResourceClosedExceptionin classResourceSupport<Buffer,ByteBufBuffer>
-
prepareSend
protected Owned<ByteBufBuffer> prepareSend()
Description copied from class:ResourceSupportPrepare this instance for ownership transfer. This method is called fromResourceSupport.send()in the sending thread. This method should put this resource in a deactivated state where it is no longer accessible from the currently owning thread. In this state, the resource instance should only allow a call toOwned.transferOwnership(Drop)in the recipient thread.- Specified by:
prepareSendin classResourceSupport<Buffer,ByteBufBuffer>- Returns:
- This resource instance in a deactivated state.
-
makeInaccessible
protected void makeInaccessible()
Description copied from class:ResourceSupportCalled when this resource needs to be considered inaccessible. This is called at the correct points, by theResourceSupportclass, when the resource is being closed or sent, and can be used to set further traps for accesses that makes accessibility checks cheap. There would normally not be any reason to call this directly from a sub-class.- Overrides:
makeInaccessiblein classResourceSupport<Buffer,ByteBufBuffer>
-
capacity
public int capacity()
Description copied from interface:BufferThe capacity of this buffer, that is, the maximum number of bytes it can contain.
-
readerOffset
public int readerOffset()
Description copied from interface:BufferGet the current reader offset. The next read will happen from this byte offset into the buffer.- Specified by:
readerOffsetin interfaceBuffer- Returns:
- The current reader offset.
-
skipReadable
public void skipReadable(int delta)
Description copied from interface:BufferSet the reader offset toreaderOffset() + delta.- Specified by:
skipReadablein interfaceBuffer- Parameters:
delta- to accumulate.
-
readerOffset
public Buffer readerOffset(int offset)
Description copied from interface:BufferSet the reader offset. Make the next read happen from the given offset into the buffer.- Specified by:
readerOffsetin interfaceBuffer- Parameters:
offset- The reader offset to set.- Returns:
- This Buffer.
-
writerOffset
public int writerOffset()
Description copied from interface:BufferGet the current writer offset. The next write will happen at this byte offset into the buffer.- Specified by:
writerOffsetin interfaceBuffer- Returns:
- The current writer offset.
-
skipWritable
public void skipWritable(int delta)
Description copied from interface:BufferSet the write offset towriterOffset() + delta.- Specified by:
skipWritablein interfaceBuffer- Parameters:
delta- to accumulate.
-
writerOffset
public Buffer writerOffset(int offset)
Description copied from interface:BufferSet the writer offset. Make the next write happen at the given offset.- Specified by:
writerOffsetin interfaceBuffer- Parameters:
offset- The writer offset to set.- Returns:
- This Buffer.
-
readableBytes
public int readableBytes()
Description copied from interface:BufferReturns the number of readable bytes which is equal to(writerOffset() - readerOffset()).- Specified by:
readableBytesin interfaceBuffer
-
writableBytes
public int writableBytes()
Description copied from interface:BufferReturns the number of writable bytes which is equal to(capacity() - writerOffset()).- Specified by:
writableBytesin interfaceBuffer
-
fill
public Buffer fill(byte value)
Description copied from interface:BufferFills 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
public Buffer makeReadOnly()
Description copied from interface:BufferMakes 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:
makeReadOnlyin interfaceBuffer- Returns:
- This buffer instance.
-
readOnly
public boolean readOnly()
Description copied from interface:BufferQueries if this buffer is read-only or not.
-
isDirect
public boolean isDirect()
Description copied from interface:BufferQueries if this buffer is backed by native memory, or not.
-
copyInto
public void copyInto(int srcPos, byte[] dest, int destPos, int length)Description copied from interface:BufferCopies the given length of data from this buffer into the given destination array, beginning at the given source position in this buffer, and the given destination position in the destination array.This method does not read or modify the write offset or the read offset.
- Specified by:
copyIntoin interfaceBuffer- Parameters:
srcPos- The byte offset into this buffer from where the copying should start; the byte at this offset in this buffer will be copied to thedestPosindex in thedestarray.dest- The destination byte array.destPos- The index into thedestarray from where the copying should start.length- The number of bytes to copy.
-
copyInto
public void copyInto(int srcPos, ByteBuffer dest, int destPos, int length)Description copied from interface:BufferCopies the given length of data from this buffer into the given destination byte buffer, beginning at the given source position in this buffer, and the given destination position in the destination byte buffer.This method does not read or modify the write offset or the read offset, nor is the position of the destination buffer changed.
The position and limit of the destination byte buffer are also ignored, and do not influence
destPosorlength.- Specified by:
copyIntoin interfaceBuffer- Parameters:
srcPos- The byte offset into this buffer from where the copying should start; the byte at this offset in this buffer will be copied to thedestPosindex in thedestarray.dest- The destination byte buffer.destPos- The index into thedestarray from where the copying should start.length- The number of bytes to copy.
-
copyInto
public void copyInto(int srcPos, Buffer dest, int destPos, int length)Description copied from interface:BufferCopies the given length of data from this buffer into the given destination buffer, beginning at the given source position in this buffer, and the given destination position in the destination buffer.This method does not read or modify the write offset or the read offset on this buffer, nor on the destination buffer.
The read and write offsets of the destination buffer are also ignored, and do not influence
destPosorlength.- Specified by:
copyIntoin interfaceBuffer- Parameters:
srcPos- The byte offset into this buffer from where the copying should start; the byte at this offset in this buffer will be copied to thedestPosindex in thedestarray.dest- The destination buffer.destPos- The index into thedestarray from where the copying should start.length- The number of bytes to copy.
-
transferTo
public int transferTo(WritableByteChannel channel, int length) throws IOException
Description copied from interface:BufferRead from this buffer and write to the given channel. The number of bytes actually written to the channel are returned. No more than the givenlengthof bytes, or the number of readable bytes, will be written to the channel, whichever is smaller. If the channel has a position, then it will be advanced by the number of bytes written. The reader-offset of this buffer will likewise be advanced by the number of bytes written.- Specified by:
transferToin interfaceBuffer- Parameters:
channel- The channel to write to.length- The maximum number of bytes to write.- Returns:
- The actual number of bytes written, possibly zero.
- Throws:
IOException- If the write-operation on the channel failed for some reason.
-
transferFrom
public int transferFrom(ReadableByteChannel channel, int length) throws IOException
Description copied from interface:BufferRead from the given channel and write to this buffer. The number of bytes actually read from the channel are returned, or -1 is returned if the channel has reached the end-of-stream. No more than the givenlengthof bytes, or the number of writable bytes, will be read from the channel, whichever is smaller. If the channel has a position, then it will be advanced by the number of bytes read. The writer-offset of this buffer will likewise be advanced by the number of bytes read.- Specified by:
transferFromin interfaceBuffer- Parameters:
channel- The channel to read from.length- The maximum number of bytes to read.- Returns:
- The actual number of bytes read, possibly zero, or -1 if the end-of-stream has been reached.
- Throws:
IOException- If the read-operation on the channel failed for some reason.
-
bytesBefore
public int bytesBefore(byte needle)
Description copied from interface:BufferGet the number of readable bytes, until the givenneedleis found in this buffer. If the needle is not found,-1is returned.This method does not modify the reader-offset or the write-offset.
- Specified by:
bytesBeforein interfaceBuffer- Parameters:
needle- The byte value to search for.- Returns:
- The offset, relative to the current
Buffer.readerOffset(), of the found value, or-1if none was found.
-
openCursor
public ByteCursor openCursor()
Description copied from interface:BufferOpens a cursor to iterate the readable bytes of this buffer. The reader offset and writer offset are not modified by the cursor.Care should be taken to ensure that the buffer's lifetime extends beyond the cursor and the iteration, and that the reader offset and writer offset are not modified while the iteration takes place. Otherwise, unpredictable behaviour might result.
- Specified by:
openCursorin interfaceBuffer- Returns:
- A
ByteCursorfor iterating the readable bytes of this buffer.
-
openCursor
public ByteCursor openCursor(int fromOffset, int length)
Description copied from interface:BufferOpens a cursor to iterate the given number bytes of this buffer, starting at the given offset. The reader offset and writer offset are not modified by the cursor.Care should be taken to ensure that the buffer's lifetime extends beyond the cursor and the iteration, and that the reader offset and writer offset are not modified while the iteration takes place. Otherwise, unpredictable behaviour might result.
- Specified by:
openCursorin interfaceBuffer- Parameters:
fromOffset- The offset into the buffer where iteration should start. The first byte read from the iterator will be the byte at this offset.length- The number of bytes to iterate.- Returns:
- A
ByteCursorfor the given stretch of bytes of this buffer.
-
openReverseCursor
public ByteCursor openReverseCursor(int fromOffset, int length)
Description copied from interface:BufferOpens a cursor to iterate the given number bytes of this buffer, in reverse, starting at the given offset. The reader offset and writer offset are not modified by the cursor.Care should be taken to ensure that the buffer's lifetime extends beyond the cursor and the iteration, and that the reader offset and writer offset are not modified while the iteration takes place. Otherwise, unpredictable behaviour might result.
- Specified by:
openReverseCursorin interfaceBuffer- Parameters:
fromOffset- The offset into the buffer where iteration should start. The first byte read from the iterator will be the byte at this offset.length- The number of bytes to iterate.- Returns:
- A
ByteCursorfor the given stretch of bytes of this buffer.
-
ensureWritable
public Buffer ensureWritable(int size, int minimumGrowth, boolean allowCompaction)
Description copied from interface:BufferEnsures 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
allowCompactionistrue, 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 sameBufferAllocatorthat this buffer was created with. -
If
allowCompactionistrue, then the implementation may choose to do a combination of compaction and allocation.
- Specified by:
ensureWritablein 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-trueif the method is allowed to modify the reader offset and writer offset, otherwisefalse.- Returns:
- This buffer instance.
-
If
-
copy
public Buffer copy(int offset, int length)
Description copied from interface:BufferReturns 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.
- Specified by:
copyin 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.
-
split
public Buffer split(int splitOffset)
Description copied from interface:BufferSplits 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:
splitin 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
public Buffer compact()
Description copied from interface:BufferDiscards the read bytes, and moves the buffer contents to the beginning of the buffer.
-
countComponents
public int countComponents()
Description copied from interface:BufferGet the number of "components" in this buffer. For composite buffers, this is the number of transitive constituent buffers, while non-composite buffers only have one component.- Specified by:
countComponentsin interfaceBuffer- Returns:
- The number of components in this buffer.
-
countReadableComponents
public int countReadableComponents()
Description copied from interface:BufferGet the number of "components" in this buffer, that are readable. These are the components that would be processed byBuffer.forEachReadable(int, ReadableComponentProcessor). For composite buffers, this is the number of transitive constituent buffers that are readable, while non-composite buffers only have at most one readable component.The number of readable components may be less than the
component count, if not all of them have readable data.- Specified by:
countReadableComponentsin interfaceBuffer- Returns:
- The number of readable components in this buffer.
-
countWritableComponents
public int countWritableComponents()
Description copied from interface:BufferGet the number of "components" in this buffer, that are writable. These are the components that would be processed byBuffer.forEachWritable(int, WritableComponentProcessor). For composite buffers, this is the number of transitive constituent buffers that are writable, while non-composite buffers only have at most one writable component.The number of writable components may be less than the
component count, if not all of them have space for writing.- Specified by:
countWritableComponentsin interfaceBuffer- Returns:
- The number of writable components in this buffer.
-
forEachReadable
public <E extends Exception> int forEachReadable(int initialIndex, ReadableComponentProcessor<E> processor) throws E extends Exception
Description copied from interface:BufferProcesses all readable components of this buffer, and return the number of components processed.The given processor is called for each readable component in this buffer, and passed a component index, for the given component in the iteration, and a
ReadableComponentobject for accessing the data within the given component.The component index is specific to the particular invocation of this method. The first call to the consumer will be passed the given initial index, and the next call will be passed the initial index plus one, and so on.
The component processor may stop the iteration at any time by returning
false. This will cause the number of components processed to be returned as a negative number (to signal early return), and the number of components processed may then be less than the readable component count.Note that the
ReadableComponentinstance passed to the consumer could be reused for multiple calls, so the data must be extracted from the component in the context of the iteration.The
ByteBufferinstances obtained from the component, share lifetime with that internal component. This means they can be accessed as long as the internal memory store remain unchanged. Methods that may cause such changes areBuffer.split(int),Buffer.split(),Buffer.compact(),Buffer.ensureWritable(int),Buffer.ensureWritable(int, int, boolean), andResource.send().The best way to ensure this doesn't cause any trouble, is to use the buffers directly as part of the iteration.
Note that the arrays, memory addresses, and byte buffers exposed as components by this method, should not be used for changing the buffer contents. Doing so may cause undefined behaviour.
Changes to position and limit of the byte buffers exposed via the processed components, are not reflected back to this buffer instance.
- Specified by:
forEachReadablein interfaceBuffer- Parameters:
initialIndex- The initial index of the component for iteration, and the index that will be passed to the first call to the processor.processor- The processor that will be used to process the buffer components.- Returns:
- The number of readable components processed, as a positive number if all readable components were
processed, or as a negative number if the iteration was stopped because
ReadableComponentProcessor.process(int, ReadableComponent)returnedfalse. In any case, the number of components processed may be less thanBuffer.countComponents(). - Throws:
E extends Exception
-
forEachWritable
public <E extends Exception> int forEachWritable(int initialIndex, WritableComponentProcessor<E> processor) throws E extends Exception
Description copied from interface:BufferProcess all writable components of this buffer, and return the number of components processed.The given processor is called for each writable component in this buffer, and passed a component index, for the given component in the iteration, and a
WritableComponentobject for accessing the data within the given component.The component index is specific to the particular invocation of this method. The first call to the consumer will be passed the given initial index, and the next call will be passed the initial index plus one, and so on.
The
component processormay stop the iteration at any time by returningfalse. This will cause the number of components processed to be returned as a negative number (to signal early return), and the number of components processed may then be less than the readable component count.Note that the
WritableComponentinstance passed to the consumer could be reused for multiple calls, so the data must be extracted from the component in the context of the iteration.The
ByteBufferinstances obtained from the component, share lifetime with that internal component. This means they can be accessed as long as the internal memory store remain unchanged. Methods that may cause such changes areBuffer.split(int),Buffer.split(),Buffer.compact(),Buffer.ensureWritable(int),Buffer.ensureWritable(int, int, boolean), andResource.send().The best way to ensure this doesn't cause any trouble, is to use the buffers directly as part of the iteration.
Changes to position and limit of the byte buffers exposed via the processed components, are not reflected back to this buffer instance.
- Specified by:
forEachWritablein interfaceBuffer- Parameters:
initialIndex- The initial index of the component for iteration, and the index that will be passed to the first call to the processor.processor- The processor that will be used to process the buffer components.- Returns:
- The number of writable components processed, as a positive number if all writable components were
processed, or as a negative number if the iteration was stopped because
WritableComponentProcessor.process(int, WritableComponent)returnedfalse. In any case, the number of components processed may be less thanBuffer.countComponents(). - Throws:
E extends Exception
-
readByte
public byte readByte()
Description copied from interface:BufferAccessorRead the byte value at the currentBuffer.readerOffset(), and increases the reader offset byByte.BYTES. The value is read using a two's complement 8-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readBytein interfaceBufferAccessor- Returns:
- The byte value at the current reader offset.
-
getByte
public byte getByte(int roff)
Description copied from interface:BufferAccessorGet the byte value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a two's complement 8-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getBytein interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The byte value at the given offset.
-
readUnsignedByte
public int readUnsignedByte()
Description copied from interface:BufferAccessorRead the unsigned byte value at the currentBuffer.readerOffset(), and increases the reader offset byByte.BYTES. The value is read using an unsigned two's complement 8-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readUnsignedBytein interfaceBufferAccessor- Returns:
- The unsigned byte value at the current reader offset.
-
getUnsignedByte
public int getUnsignedByte(int roff)
Description copied from interface:BufferAccessorGet the unsigned byte value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using an unsigned two's complement 8-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getUnsignedBytein interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The unsigned byte value at the given offset.
-
writeByte
public Buffer writeByte(byte value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeBytein interfaceBufferAccessor- Parameters:
value- The byte value to write.- Returns:
- This Buffer.
-
setByte
public Buffer setByte(int woff, byte value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setBytein 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
public Buffer writeUnsignedByte(int value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeUnsignedBytein interfaceBufferAccessor- Parameters:
value- The int value to write.- Returns:
- This Buffer.
-
setUnsignedByte
public Buffer setUnsignedByte(int woff, int value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setUnsignedBytein interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The int value to write.- Returns:
- This Buffer.
-
readChar
public char readChar()
Description copied from interface:BufferAccessorRead the char value at the currentBuffer.readerOffset(), and increases the reader offset by 2. The value is read using a 2-byte UTF-16 encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readCharin interfaceBufferAccessor- Returns:
- The char value at the current reader offset.
-
getChar
public char getChar(int roff)
Description copied from interface:BufferAccessorGet the char value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a 2-byte UTF-16 encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getCharin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The char value at the given offset.
-
writeChar
public Buffer writeChar(char value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeCharin interfaceBufferAccessor- Parameters:
value- The char value to write.- Returns:
- This Buffer.
-
setChar
public Buffer setChar(int woff, char value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setCharin interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The char value to write.- Returns:
- This Buffer.
-
readShort
public short readShort()
Description copied from interface:BufferAccessorRead the short value at the currentBuffer.readerOffset(), and increases the reader offset byShort.BYTES. The value is read using a two's complement 16-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readShortin interfaceBufferAccessor- Returns:
- The short value at the current reader offset.
-
getShort
public short getShort(int roff)
Description copied from interface:BufferAccessorGet the short value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a two's complement 16-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getShortin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The short value at the given offset.
-
readUnsignedShort
public int readUnsignedShort()
Description copied from interface:BufferAccessorRead the unsigned short value at the currentBuffer.readerOffset(), and increases the reader offset byShort.BYTES. The value is read using an unsigned two's complement 16-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readUnsignedShortin interfaceBufferAccessor- Returns:
- The unsigned short value at the current reader offset.
-
getUnsignedShort
public int getUnsignedShort(int roff)
Description copied from interface:BufferAccessorGet the unsigned short value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using an unsigned two's complement 16-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getUnsignedShortin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The unsigned short value at the given offset.
-
writeShort
public Buffer writeShort(short value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeShortin interfaceBufferAccessor- Parameters:
value- The short value to write.- Returns:
- This Buffer.
-
setShort
public Buffer setShort(int woff, short value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setShortin 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
public Buffer writeUnsignedShort(int value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeUnsignedShortin interfaceBufferAccessor- Parameters:
value- The int value to write.- Returns:
- This Buffer.
-
setUnsignedShort
public Buffer setUnsignedShort(int woff, int value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setUnsignedShortin interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The int value to write.- Returns:
- This Buffer.
-
readMedium
public int readMedium()
Description copied from interface:BufferAccessorRead the int value at the currentBuffer.readerOffset(), and increases the reader offset by 3. The value is read using a two's complement 24-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readMediumin interfaceBufferAccessor- Returns:
- The int value at the current reader offset.
-
getMedium
public int getMedium(int roff)
Description copied from interface:BufferAccessorGet the int value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a two's complement 24-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getMediumin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The int value at the given offset.
-
readUnsignedMedium
public int readUnsignedMedium()
Description copied from interface:BufferAccessorRead the unsigned int value at the currentBuffer.readerOffset(), and increases the reader offset by 3. The value is read using an unsigned two's complement 24-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readUnsignedMediumin interfaceBufferAccessor- Returns:
- The unsigned int value at the current reader offset.
-
getUnsignedMedium
public int getUnsignedMedium(int roff)
Description copied from interface:BufferAccessorGet the unsigned int value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using an unsigned two's complement 24-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getUnsignedMediumin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The unsigned int value at the given offset.
-
writeMedium
public Buffer writeMedium(int value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeMediumin interfaceBufferAccessor- Parameters:
value- The int value to write.- Returns:
- This Buffer.
-
setMedium
public Buffer setMedium(int woff, int value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setMediumin 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
public Buffer writeUnsignedMedium(int value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeUnsignedMediumin interfaceBufferAccessor- Parameters:
value- The int value to write.- Returns:
- This Buffer.
-
setUnsignedMedium
public Buffer setUnsignedMedium(int woff, int value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setUnsignedMediumin interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The int value to write.- Returns:
- This Buffer.
-
readInt
public int readInt()
Description copied from interface:BufferAccessorRead the int value at the currentBuffer.readerOffset(), and increases the reader offset byInteger.BYTES. The value is read using a two's complement 32-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readIntin interfaceBufferAccessor- Returns:
- The int value at the current reader offset.
-
getInt
public int getInt(int roff)
Description copied from interface:BufferAccessorGet the int value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a two's complement 32-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getIntin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The int value at the given offset.
-
readUnsignedInt
public long readUnsignedInt()
Description copied from interface:BufferAccessorRead the unsigned int value at the currentBuffer.readerOffset(), and increases the reader offset byInteger.BYTES. The value is read using an unsigned two's complement 32-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readUnsignedIntin interfaceBufferAccessor- Returns:
- The unsigned int value at the current reader offset.
-
getUnsignedInt
public long getUnsignedInt(int roff)
Description copied from interface:BufferAccessorGet the unsigned int value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using an unsigned two's complement 32-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getUnsignedIntin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The unsigned int value at the given offset.
-
writeInt
public Buffer writeInt(int value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeIntin interfaceBufferAccessor- Parameters:
value- The int value to write.- Returns:
- This Buffer.
-
setInt
public Buffer setInt(int woff, int value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setIntin 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
public Buffer writeUnsignedInt(long value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeUnsignedIntin interfaceBufferAccessor- Parameters:
value- The long value to write.- Returns:
- This Buffer.
-
setUnsignedInt
public Buffer setUnsignedInt(int woff, long value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setUnsignedIntin interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The long value to write.- Returns:
- This Buffer.
-
readFloat
public float readFloat()
Description copied from interface:BufferAccessorRead the float value at the currentBuffer.readerOffset(), and increases the reader offset byFloat.BYTES. The value is read using a 32-bit IEEE floating point encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readFloatin interfaceBufferAccessor- Returns:
- The float value at the current reader offset.
-
getFloat
public float getFloat(int roff)
Description copied from interface:BufferAccessorGet the float value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a 32-bit IEEE floating point encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getFloatin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The float value at the given offset.
-
writeFloat
public Buffer writeFloat(float value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeFloatin interfaceBufferAccessor- Parameters:
value- The float value to write.- Returns:
- This Buffer.
-
setFloat
public Buffer setFloat(int woff, float value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setFloatin interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The float value to write.- Returns:
- This Buffer.
-
readLong
public long readLong()
Description copied from interface:BufferAccessorRead the long value at the currentBuffer.readerOffset(), and increases the reader offset byLong.BYTES. The value is read using a two's complement 64-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readLongin interfaceBufferAccessor- Returns:
- The long value at the current reader offset.
-
getLong
public long getLong(int roff)
Description copied from interface:BufferAccessorGet the long value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a two's complement 64-bit encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getLongin interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The long value at the given offset.
-
writeLong
public Buffer writeLong(long value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeLongin interfaceBufferAccessor- Parameters:
value- The long value to write.- Returns:
- This Buffer.
-
setLong
public Buffer setLong(int woff, long value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setLongin interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The long value to write.- Returns:
- This Buffer.
-
readDouble
public double readDouble()
Description copied from interface:BufferAccessorRead the double value at the currentBuffer.readerOffset(), and increases the reader offset byDouble.BYTES. The value is read using a 64-bit IEEE floating point encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
readDoublein interfaceBufferAccessor- Returns:
- The double value at the current reader offset.
-
getDouble
public double getDouble(int roff)
Description copied from interface:BufferAccessorGet the double value at the given reader offset. TheBuffer.readerOffset()is not modified. The value is read using a 64-bit IEEE floating point encoding, inByteOrder.BIG_ENDIANbyte order.- Specified by:
getDoublein interfaceBufferAccessor- Parameters:
roff- The read offset, an absolute offset into this buffer, to read from.- Returns:
- The double value at the given offset.
-
writeDouble
public Buffer writeDouble(double value)
Description copied from interface:BufferAccessorWrite 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_ENDIANbyte order.- Specified by:
writeDoublein interfaceBufferAccessor- Parameters:
value- The double value to write.- Returns:
- This Buffer.
-
setDouble
public Buffer setDouble(int woff, double value)
Description copied from interface:BufferAccessorSet 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_ENDIANbyte order.- Specified by:
setDoublein interfaceBufferAccessor- Parameters:
woff- The write offset, an absolute offset into this buffer to write to.value- The double value to write.- Returns:
- This Buffer.
-
-