Class BufferStub

  • All Implemented Interfaces:
    Buffer, BufferAccessor, Resource<Buffer>, AutoCloseable

    public class BufferStub
    extends Object
    implements Buffer
    A stub of a Buffer implementation that implements all buffer methods by delegating them to a wrapped buffer instance.

    This can be used when writing automated tests for code that integrates with Buffer, but should not be used in production code.

    • Field Detail

      • delegate

        protected final Buffer delegate
    • Constructor Detail

      • BufferStub

        public BufferStub​(Buffer delegate)
        Create a new buffer stub that delegates all calls to the given instance.
        Parameters:
        delegate - The buffer instance to delegate all method calls to.
    • Method Detail

      • capacity

        public int capacity()
        Description copied from interface: Buffer
        The capacity of this buffer, that is, the maximum number of bytes it can contain.
        Specified by:
        capacity in interface Buffer
        Returns:
        The capacity in bytes.
      • readerOffset

        public int readerOffset()
        Description copied from interface: Buffer
        Get the current reader offset. The next read will happen from this byte offset into the buffer.
        Specified by:
        readerOffset in interface Buffer
        Returns:
        The current reader offset.
      • skipReadableBytes

        public Buffer skipReadableBytes​(int delta)
        Description copied from interface: Buffer
        Move the reader offset forward by the given delta.
        Specified by:
        skipReadableBytes in interface Buffer
        Parameters:
        delta - to accumulate.
        Returns:
        This buffer instance.
      • readerOffset

        public Buffer 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 interface Buffer
        Parameters:
        offset - The reader offset to set.
        Returns:
        This Buffer.
      • writerOffset

        public int writerOffset()
        Description copied from interface: Buffer
        Get the current writer offset. The next write will happen at this byte offset into the buffer.
        Specified by:
        writerOffset in interface Buffer
        Returns:
        The current writer offset.
      • skipWritableBytes

        public Buffer skipWritableBytes​(int delta)
        Description copied from interface: Buffer
        Move the writer offset to ahead by the given delta.
        Specified by:
        skipWritableBytes in interface Buffer
        Parameters:
        delta - to accumulate.
        Returns:
        This buffer instance.
      • writerOffset

        public Buffer 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 interface Buffer
        Parameters:
        offset - The writer offset to set.
        Returns:
        This Buffer.
      • readableBytes

        public int readableBytes()
        Description copied from interface: Buffer
        Returns the number of readable bytes which is equal to (writerOffset() - readerOffset()).
        Specified by:
        readableBytes in interface Buffer
      • writableBytes

        public int writableBytes()
        Description copied from interface: Buffer
        Returns the number of writable bytes which is equal to (capacity() - writerOffset()).
        Specified by:
        writableBytes in interface Buffer
      • makeReadOnly

        public Buffer 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 interface Buffer
        Returns:
        This buffer instance.
      • readOnly

        public boolean readOnly()
        Description copied from interface: Buffer
        Queries if this buffer is read-only or not.
        Specified by:
        readOnly in interface Buffer
        Returns:
        true if this buffer is read-only, false otherwise.
      • isDirect

        public boolean isDirect()
        Description copied from interface: Buffer
        Queries if this buffer is backed by native memory, or not.
        Specified by:
        isDirect in interface Buffer
        Returns:
        true if this buffer is backed by native, off-heap, memory. Otherwise, false, if this buffer is backed by on-heap memory.
      • implicitCapacityLimit

        public Buffer implicitCapacityLimit​(int limit)
        Description copied from interface: Buffer
        Set an upper limit to the implicit capacity growth. Buffer write* 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 the write* 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 various split and copy methods will have the default limit set.

        The limit is not impacted by calls to split methods on this buffer. In other words, even though split methods reduce the capacity of this buffer, the set limit, if any, remains the same.

        Specified by:
        implicitCapacityLimit in interface Buffer
        Parameters:
        limit - The maximum size this buffers capacity will implicitly grow to via write* methods.
        Returns:
        This buffer instance.
      • implicitCapacityLimit

        public int implicitCapacityLimit()
        Description copied from interface: Buffer
        Returns the implicit capacity limit of the buffer. If none was set before via Buffer.implicitCapacityLimit(int) this method will return the default value.
        Specified by:
        implicitCapacityLimit in interface Buffer
        Returns:
        the limit.
      • copyInto

        public void copyInto​(int srcPos,
                             byte[] dest,
                             int destPos,
                             int length)
        Description copied from interface: Buffer
        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.

        This method does not read or modify the write offset or the read offset.

        Specified by:
        copyInto in interface Buffer
        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 the destPos index in the dest array.
        dest - The destination byte array.
        destPos - The index into the dest array 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: Buffer
        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.

        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 destPos or length.

        Specified by:
        copyInto in interface Buffer
        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 the destPos index in the dest array.
        dest - The destination byte buffer.
        destPos - The index into the dest array 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: Buffer
        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.

        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 destPos or length.

        Specified by:
        copyInto in interface Buffer
        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 the destPos index in the dest array.
        dest - The destination buffer.
        destPos - The index into the dest array 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: Buffer
        Read from this buffer and write to the given channel. The number of bytes actually written to the channel are returned. No more than the given length of 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:
        transferTo in interface Buffer
        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​(FileChannel channel,
                                long position,
                                int length)
                         throws IOException
        Description copied from interface: Buffer
        Read from the given channel starting from the given position 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 given length of bytes, or the number of writable bytes, will be read from the channel, whichever is smaller. The channel's position is not modified. The writer-offset of this buffer will likewise be advanced by the number of bytes read.
        Specified by:
        transferFrom in interface Buffer
        Parameters:
        channel - The channel to read from.
        position - The file position.
        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.
      • transferFrom

        public int transferFrom​(ReadableByteChannel channel,
                                int length)
                         throws IOException
        Description copied from interface: Buffer
        Read 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 given length of 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:
        transferFrom in interface Buffer
        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.
      • writeBytes

        public Buffer 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 interface Buffer
        Parameters:
        source - The buffer to read from.
        Returns:
        This buffer.
      • writeBytes

        public Buffer 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 interface Buffer
        Parameters:
        source - The byte array to read from.
        Returns:
        This buffer.
      • bytesBefore

        public int bytesBefore​(byte needle)
        Description copied from interface: Buffer
        Get the number of readable bytes, until the given needle is found in this buffer. If the needle is not found, -1 is returned.

        This method does not modify the reader-offset or the write-offset.

        Specified by:
        bytesBefore in interface Buffer
        Parameters:
        needle - The byte value to search for.
        Returns:
        The offset, relative to the current Buffer.readerOffset(), of the found value, or -1 if none was found.
      • bytesBefore

        public int bytesBefore​(Buffer needle)
        Description copied from interface: Buffer
        Get the number of readable bytes, until the given needle is found in this buffer. The found offset will be the offset into this buffer, relative to its reader-offset, of the first byte of a sequence that matches all readable bytes in the given needle buffer. If the needle is not found, -1 is returned.

        This method does not modify the reader-offset or the write-offset.

        Specified by:
        bytesBefore in interface Buffer
        Parameters:
        needle - The buffer value to search for.
        Returns:
        The offset, relative to the current Buffer.readerOffset(), of the found value, or -1 if none was found.
      • openCursor

        public ByteCursor openCursor()
        Description copied from interface: Buffer
        Opens 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:
        openCursor in interface Buffer
        Returns:
        A ByteCursor for iterating the readable bytes of this buffer.
      • openCursor

        public ByteCursor openCursor​(int fromOffset,
                                     int length)
        Description copied from interface: Buffer
        Opens 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:
        openCursor in interface Buffer
        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 ByteCursor for the given stretch of bytes of this buffer.
      • openReverseCursor

        public ByteCursor openReverseCursor()
        Description copied from interface: Buffer
        Opens a cursor to iterate the readable bytes of this buffer, in reverse. 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:
        openReverseCursor in interface Buffer
        Returns:
        A ByteCursor for the readable bytes of this buffer.
      • openReverseCursor

        public ByteCursor openReverseCursor​(int fromOffset,
                                            int length)
        Description copied from interface: Buffer
        Opens 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:
        openReverseCursor in interface Buffer
        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 ByteCursor for the given stretch of bytes of this buffer.
      • ensureWritable

        public Buffer 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 the BufferAllocator the buffer was created with. This method is the same as calling Buffer.ensureWritable(int, int, boolean) where allowCompaction is true.
        Specified by:
        ensureWritable in interface Buffer
        Parameters:
        size - The requested number of bytes of space that should be available for writing.
        Returns:
        This buffer instance.
      • ensureWritable

        public Buffer 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 is true, 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 the Buffer.compact() method, as the implementation may be able to make the requested bytes available with less effort than is strictly mandated by the Buffer.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 same BufferAllocator that this buffer was created with.
        • If allowCompaction is true, then the implementation may choose to do a combination of compaction and allocation.
        Specified by:
        ensureWritable in interface Buffer
        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, otherwise false.
        Returns:
        This buffer instance.
      • copy

        public Buffer 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 to buf.copy(buf.readerOffset(), buf.readableBytes()). This method does not modify Buffer.readerOffset() or Buffer.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 interface Buffer
        Returns:
        A new buffer instance, with independent Buffer.readerOffset() and Buffer.writerOffset(), that contains a copy of the readable region of this buffer.
      • copy

        public Buffer 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 modify Buffer.readerOffset() or Buffer.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 is true, and it will not be read-only if the argument is false. 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 interface Buffer
        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 desired Buffer.readOnly() state of the returned buffer.
        Returns:
        A new buffer instance, with independent Buffer.readerOffset() and Buffer.writerOffset(), that contains a copy of the given region of this buffer.
      • split

        public Buffer 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 its Buffer.writerOffset() and Buffer.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:

        
                 This buffer:
                  +------------------------------------------+
                 0|   |r/o                  |w/o             |cap
                  +---+---------------------+----------------+
                 /   /                     / \               \
                /   /                     /   \               \
               /   /                     /     \               \
              /   /                     /       \               \
             /   /                     /         \               \
            +---+---------------------+           +---------------+
            |   |r/o                  |w/o & cap  |r/o & w/o      |cap
            +---+---------------------+           +---------------+
            Returned buffer.                      This buffer.
         
        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.

        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 interface Buffer
        Returns:
        A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.
      • split

        public Buffer split​(int splitOffset)
        Description copied from interface: Buffer
        Splits the buffer into two, at the given splitOffset.

        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() and Buffer.writerOffset() of this buffer, but truncated to fit within the capacity dictated by the splitOffset.

        The memory region in the returned buffer will become inaccessible through this buffer. If the Buffer.readerOffset() or Buffer.writerOffset() of this buffer lie prior to the splitOffset, then those offsets will be moved forward, so they land on offset 0 after the split.

        Effectively, the following transformation takes place:

        
                 This buffer:
                  +--------------------------------+
                 0|               |splitOffset     |cap
                  +---------------+----------------+
                 /               / \               \
                /               /   \               \
               /               /     \               \
              /               /       \               \
             /               /         \               \
            +---------------+           +---------------+
            |               |cap        |               |cap
            +---------------+           +---------------+
            Returned buffer.            This buffer.
         
        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.

        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 interface Buffer
        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: Buffer
        Discards the read bytes, and moves the buffer contents to the beginning of the buffer.
        Specified by:
        compact in interface Buffer
        Returns:
        This buffer instance.
      • countComponents

        public int countComponents()
        Description copied from interface: Buffer
        Get 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:
        countComponents in interface Buffer
        Returns:
        The number of components in this buffer.
      • countReadableComponents

        public int countReadableComponents()
        Description copied from interface: Buffer
        Get the number of "components" in this buffer, that are readable. These are the components that would be processed by Buffer.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:
        countReadableComponents in interface Buffer
        Returns:
        The number of readable components in this buffer.
      • countWritableComponents

        public int countWritableComponents()
        Description copied from interface: Buffer
        Get the number of "components" in this buffer, that are writable. These are the components that would be processed by Buffer.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:
        countWritableComponents in interface Buffer
        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: Buffer
        Processes 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 ReadableComponent object 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 ReadableComponent instance 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 ByteBuffer instances 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 are Buffer.split(int), Buffer.split(), Buffer.readSplit(int), Buffer.writeSplit(int), Buffer.compact(), Buffer.ensureWritable(int), Buffer.ensureWritable(int, int, boolean), and Resource.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:
        forEachReadable in interface Buffer
        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) returned false. In any case, the number of components processed may be less than Buffer.countComponents().
        Throws:
        E extends Exception
      • forEachReadable

        public <T extends ReadableComponent & ComponentIterator.NextComponentIterator<T> forEachReadable()
        Description copied from interface: Buffer
        Create a component iterator for all readable components in this buffer.

        Unlike the Buffer.forEachReadable(int, ReadableComponentProcessor) method, this API permits external iteration of the components, while at the same time protecting the life-cycle of the buffer.

        The typical code pattern for using this API looks like the following:

        
              try (var iteration = buffer.forEachReadable()) {
                  for (var c = iteration.first(); c != null; c = c.next()) {
                      ByteBuffer componentBuffer = c.readableBuffer();
                      // ...
                  }
              }
         
        Note the use of the var keyword for local variables, which are required for correctly expressing the generic types used in the iteration. Following this code pattern will ensure that the components, and their parent buffer, will be correctly life-cycled.

        Note that the ReadableComponent instances exposed by the iterator could be reused for multiple calls, so the data must be extracted from the component in the context of the iteration.

        The ByteBuffer instances 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 are Buffer.split(int), Buffer.split(), Buffer.readSplit(int), Buffer.writeSplit(int), Buffer.compact(), Buffer.ensureWritable(int), Buffer.ensureWritable(int, int, boolean), and Resource.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:
        forEachReadable in interface Buffer
        Type Parameters:
        T - An intersection type that presents both the ReadableComponent interface, and the ability to progress the iteration via the ComponentIterator.Next.next() method.
        Returns:
        A component iterator of readable components.
      • forEachWritable

        public <E extends Exception> int forEachWritable​(int initialIndex,
                                                         WritableComponentProcessor<E> processor)
                                                  throws E extends Exception
        Description copied from interface: Buffer
        Process 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 WritableComponent object 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 WritableComponent instance 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 ByteBuffer instances 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 are Buffer.split(int), Buffer.split(), Buffer.readSplit(int), Buffer.writeSplit(int), Buffer.compact(), Buffer.ensureWritable(int), Buffer.ensureWritable(int, int, boolean), and Resource.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:
        forEachWritable in interface Buffer
        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) returned false. In any case, the number of components processed may be less than Buffer.countComponents().
        Throws:
        E extends Exception
      • forEachWritable

        public <T extends WritableComponent & ComponentIterator.NextComponentIterator<T> forEachWritable()
        Description copied from interface: Buffer
        Create a component iterator for all writable components in this buffer.

        Unlike the Buffer.forEachWritable(int, WritableComponentProcessor) method, this API permits external iteration of the components, while at the same time protecting the life-cycle of the buffer.

        The typical code pattern for using this API looks like the following:

        
              try (var iteration = buffer.forEachWritable()) {
                  for (var c = iteration.first(); c != null; c = c.next()) {
                      ByteBuffer componentBuffer = c.writableBuffer();
                      // ...
                  }
              }
         
        Note the use of the var keyword for local variables, which are required for correctly expressing the generic types used in the iteration. Following this code pattern will ensure that the components, and their parent buffer, will be correctly life-cycled.

        Note that the WritableComponent instances exposed by the iterator could be reused for multiple calls, so the data must be extracted from the component in the context of the iteration.

        The ByteBuffer instances 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 are Buffer.split(int), Buffer.split(), Buffer.readSplit(int), Buffer.writeSplit(int), Buffer.compact(), Buffer.ensureWritable(int), Buffer.ensureWritable(int, int, boolean), and Resource.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 beyond the respective array offset and length, or buffer position and limit. 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:
        forEachWritable in interface Buffer
        Type Parameters:
        T - An intersection type that presents both the ReadableComponent interface, and the ability to progress the iteration via the ComponentIterator.Next.next() method.
        Returns:
        A component iterator of readable components.
      • getByte

        public byte getByte​(int roff)
        Description copied from interface: BufferAccessor
        Get the byte value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a two's complement 8-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getByte in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The byte value at the given offset.
      • getUnsignedByte

        public int getUnsignedByte​(int roff)
        Description copied from interface: BufferAccessor
        Get the unsigned byte value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using an unsigned two's complement 8-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getUnsignedByte in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The unsigned byte value at the given offset.
      • setByte

        public Buffer setByte​(int woff,
                              byte value)
        Description copied from interface: BufferAccessor
        Set the given byte value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a two's complement 8-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setByte in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The byte value to write.
        Returns:
        This Buffer.
      • setUnsignedByte

        public Buffer setUnsignedByte​(int woff,
                                      int value)
        Description copied from interface: BufferAccessor
        Set the given unsigned byte value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using an unsigned two's complement 8-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setUnsignedByte in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The int value to write.
        Returns:
        This Buffer.
      • getChar

        public char getChar​(int roff)
        Description copied from interface: BufferAccessor
        Get the char value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a 2-byte UTF-16 encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getChar in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The char value at the given offset.
      • setChar

        public Buffer setChar​(int woff,
                              char value)
        Description copied from interface: BufferAccessor
        Set the given char value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a 2-byte UTF-16 encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setChar in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The char value to write.
        Returns:
        This Buffer.
      • getShort

        public short getShort​(int roff)
        Description copied from interface: BufferAccessor
        Get the short value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a two's complement 16-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getShort in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The short value at the given offset.
      • getUnsignedShort

        public int getUnsignedShort​(int roff)
        Description copied from interface: BufferAccessor
        Get the unsigned short value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using an unsigned two's complement 16-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getUnsignedShort in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The unsigned short value at the given offset.
      • setShort

        public Buffer setShort​(int woff,
                               short value)
        Description copied from interface: BufferAccessor
        Set the given short value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a two's complement 16-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setShort in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The short value to write.
        Returns:
        This Buffer.
      • setUnsignedShort

        public Buffer setUnsignedShort​(int woff,
                                       int value)
        Description copied from interface: BufferAccessor
        Set the given unsigned short value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using an unsigned two's complement 16-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setUnsignedShort in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The int value to write.
        Returns:
        This Buffer.
      • getMedium

        public int getMedium​(int roff)
        Description copied from interface: BufferAccessor
        Get the int value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a two's complement 24-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getMedium in interface BufferAccessor
        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: BufferAccessor
        Read the unsigned int value at the current Buffer.readerOffset(), and increases the reader offset by 3. The value is read using an unsigned two's complement 24-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        readUnsignedMedium in interface BufferAccessor
        Returns:
        The unsigned int value at the current reader offset.
      • getUnsignedMedium

        public int getUnsignedMedium​(int roff)
        Description copied from interface: BufferAccessor
        Get the unsigned int value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using an unsigned two's complement 24-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getUnsignedMedium in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The unsigned int value at the given offset.
      • setMedium

        public Buffer setMedium​(int woff,
                                int value)
        Description copied from interface: BufferAccessor
        Set the given int value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a two's complement 24-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setMedium in interface BufferAccessor
        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: BufferAccessor
        Write the given unsigned int value at the current Buffer.writerOffset(), and increase the writer offset by 3. The value is written using an unsigned two's complement 24-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        writeUnsignedMedium in interface BufferAccessor
        Parameters:
        value - The int value to write.
        Returns:
        This Buffer.
      • setUnsignedMedium

        public Buffer setUnsignedMedium​(int woff,
                                        int value)
        Description copied from interface: BufferAccessor
        Set the given unsigned int value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using an unsigned two's complement 24-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setUnsignedMedium in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The int value to write.
        Returns:
        This Buffer.
      • getInt

        public int getInt​(int roff)
        Description copied from interface: BufferAccessor
        Get the int value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a two's complement 32-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getInt in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The int value at the given offset.
      • getUnsignedInt

        public long getUnsignedInt​(int roff)
        Description copied from interface: BufferAccessor
        Get the unsigned int value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using an unsigned two's complement 32-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getUnsignedInt in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The unsigned int value at the given offset.
      • setInt

        public Buffer setInt​(int woff,
                             int value)
        Description copied from interface: BufferAccessor
        Set the given int value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a two's complement 32-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setInt in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The int value to write.
        Returns:
        This Buffer.
      • setUnsignedInt

        public Buffer setUnsignedInt​(int woff,
                                     long value)
        Description copied from interface: BufferAccessor
        Set the given unsigned int value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using an unsigned two's complement 32-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setUnsignedInt in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The long value to write.
        Returns:
        This Buffer.
      • getFloat

        public float getFloat​(int roff)
        Description copied from interface: BufferAccessor
        Get the float value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a 32-bit IEEE floating point encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getFloat in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The float value at the given offset.
      • setFloat

        public Buffer setFloat​(int woff,
                               float value)
        Description copied from interface: BufferAccessor
        Set the given float value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a 32-bit IEEE floating point encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setFloat in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The float value to write.
        Returns:
        This Buffer.
      • getLong

        public long getLong​(int roff)
        Description copied from interface: BufferAccessor
        Get the long value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a two's complement 64-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getLong in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The long value at the given offset.
      • setLong

        public Buffer setLong​(int woff,
                              long value)
        Description copied from interface: BufferAccessor
        Set the given long value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a two's complement 64-bit encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setLong in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The long value to write.
        Returns:
        This Buffer.
      • getDouble

        public double getDouble​(int roff)
        Description copied from interface: BufferAccessor
        Get the double value at the given reader offset. The Buffer.readerOffset() is not modified. The value is read using a 64-bit IEEE floating point encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        getDouble in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The double value at the given offset.
      • setDouble

        public Buffer setDouble​(int woff,
                                double value)
        Description copied from interface: BufferAccessor
        Set the given double value at the given write offset. The Buffer.writerOffset() is not modified. The value is written using a 64-bit IEEE floating point encoding, in ByteOrder.BIG_ENDIAN byte order.
        Specified by:
        setDouble in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The double value to write.
        Returns:
        This Buffer.
      • writeCharSequence

        public Buffer writeCharSequence​(CharSequence source,
                                        Charset charset)
        Description copied from interface: Buffer
        Writes into this buffer, all the bytes from the given source using the passed charset. This updates the write offset of this buffer.
        Specified by:
        writeCharSequence in interface Buffer
        Parameters:
        source - CharSequence to read from.
        charset - Charset to use for writing.
        Returns:
        This buffer.
      • writeBytes

        public Buffer 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 interface Buffer
        Parameters:
        source - The byte array to read from.
        srcPos - Position in the source from where bytes should be written to this buffer.
        length - The number of bytes to copy.
        Returns:
        This buffer.
      • readBytes

        public Buffer 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.
        Specified by:
        readBytes in interface Buffer
        Parameters:
        destination - The byte array to write into.
        destPos - Position in the destination to where bytes should be written from this buffer.
        length - The number of bytes to copy.
        Returns:
        This buffer.
      • copy

        public Buffer 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 modify Buffer.readerOffset() or Buffer.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 a false read-only argument.

        Specified by:
        copy in interface Buffer
        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() and Buffer.writerOffset(), that contains a copy of the given region of this buffer.
      • readSplit

        public Buffer readSplit​(int length)
        Description copied from interface: Buffer
        Splits the buffer into two, at length number of bytes from the current Buffer.readerOffset() reader offset} position.

        The region of this buffer that contain the previously read and readable bytes till the readerOffset() + length position, 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 change its Buffer.readerOffset() to readerOffset() + length, and have its Buffer.writerOffset() and Buffer.capacity() both set to the readerOffset() + length position.

        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, read offset will become zero and relative position of write offset will be preserved from the provided readerOffset() + length position, even though their position in memory remain unchanged.

        Effectively, the following transformation takes place:

        
                 This buffer, where offset = readerOffset() + length:
                  +------------------------------------------+
                 0|   |r/o    offset        |w/o             |cap
                  +---+---------+-----------+----------------+
                 /   /         / \          \                \
                /   /         /   \          \                \
               /   /         /     \          \                \
              /   /         /       \          \                \
             /   /         /         \          \                \
            +---+---------+           +----------+----------------+
            |   |r/o      |w/o & cap  |r/o      w/o               |cap
            +---+---------+           +---------------------------+
            Returned buffer.                   This buffer.
         
        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.

        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:
        readSplit in interface Buffer
        Returns:
        A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.
      • writeSplit

        public Buffer writeSplit​(int length)
        Description copied from interface: Buffer
        Splits the buffer into two, at length number of bytes from the current Buffer.writerOffset() writer offset} position.

        The region of this buffer that contain the previously read and readable bytes till the writerOffset() + length position, 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 change its Buffer.writerOffset() to writerOffset() + length, and have its Buffer.writerOffset() and Buffer.capacity() both set to the writerOffset() + length.

        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, read offset will become zero and relative position of write offset will be preserved from the provided writerOffset() + length position, even though their position in memory remain unchanged.

        Effectively, the following transformation takes place:

        
                 This buffer, where offset = writerOffset() + length:
                  +------------------------------------------+
                 0|   |r/o  |w/o  offset                     |cap
                  +---+----+-------+-------------------------+
                 /   /    /       / \                        \
                /   /    /       /   \                        \
               /   /    /       /     \                        \
              /   /    /       /       \                        \
             /   /    /       /         \                        \
            +---+----+-------+           +------------------------+
            |   |r/o  |w/o   | cap       |r/o & w/o               |cap
            +---+----+-------+           +------------------------+
            Returned buffer.                   This buffer.
         
        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.

        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:
        writeSplit in interface Buffer
        Returns:
        A new buffer with independent and exclusive ownership over the previously read and readable bytes from this buffer.
      • readBoolean

        public boolean readBoolean()
        Description copied from interface: BufferAccessor
        Read the boolean value at the current Buffer.readerOffset(), and increases the reader offset by Byte.BYTES. A boolean gets read as a byte from this buffer. All byte values which are not equal to zero are considered as the boolean value true, zero represents false.
        Specified by:
        readBoolean in interface BufferAccessor
        Returns:
        The boolean value at the current reader offset.
      • getBoolean

        public boolean getBoolean​(int roff)
        Description copied from interface: BufferAccessor
        Get the boolean value at the given reader offset. The Buffer.readerOffset() is not modified. A boolean gets read as a byte from this buffer. All byte values which are not equal to zero are considered as the boolean value true, zero represents false.
        Specified by:
        getBoolean in interface BufferAccessor
        Parameters:
        roff - The read offset, an absolute offset into this buffer, to read from.
        Returns:
        The boolean value at the given offset.
      • writeBoolean

        public Buffer writeBoolean​(boolean value)
        Description copied from interface: BufferAccessor
        Write the given boolean value at the current Buffer.writerOffset(), and increase the writer offset by Byte.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 value true, zero represents false.
        Specified by:
        writeBoolean in interface BufferAccessor
        Parameters:
        value - The boolean value to write.
        Returns:
        This Buffer.
      • setBoolean

        public Buffer setBoolean​(int woff,
                                 boolean value)
        Description copied from interface: BufferAccessor
        Set the given boolean value at the given write offset. The Buffer.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 value true, zero represents false.
        Specified by:
        setBoolean in interface BufferAccessor
        Parameters:
        woff - The write offset, an absolute offset into this buffer to write to.
        value - The boolean value to write.
        Returns:
        This Buffer.
      • writeBytes

        public Buffer writeBytes​(ByteBuffer source)
        Description copied from interface: Buffer
        Writes into this buffer from the source ByteBuffer. This updates the write offset of this buffer and also the position of the source ByteBuffer.

        Note: the behaviour is undefined if the given ByteBuffer is an alias for the memory in this buffer.

        Specified by:
        writeBytes in interface Buffer
        Parameters:
        source - The ByteBuffer to read from.
        Returns:
        This buffer.
      • readBytes

        public Buffer readBytes​(ByteBuffer destination)
        Description copied from interface: Buffer
        Read from this buffer, into the destination ByteBuffer This updates the read offset of this buffer and also the position of the destination ByteBuffer.

        Note: the behaviour is undefined if the given ByteBuffer is an alias for the memory in this buffer.

        Specified by:
        readBytes in interface Buffer
        Parameters:
        destination - The ByteBuffer to write into.
        Returns:
        This buffer.
      • copy

        public Buffer copy​(boolean readOnly)
        Description copied from interface: Buffer
        Returns a copy of this buffer's readable bytes, with the given read-only setting. Modifying the content of the returned buffer will not affect this buffers contents. The two buffers will maintain separate offsets. This method does not modify Buffer.readerOffset() or Buffer.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 is true, and it will not be read-only if the argument is false. 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 interface Buffer
        Parameters:
        readOnly - The desired Buffer.readOnly() state of the returned buffer.
        Returns:
        A new buffer instance, with independent Buffer.readerOffset() and Buffer.writerOffset(), that contains a copy of the given region of this buffer.
      • send

        public Send<Buffer> send()
        Description copied from interface: Resource
        Send this object instance to another Thread, transferring the ownership to the recipient.

        The object must be in a state where it can be sent, which includes at least being accessible.

        When sent, this instance will immediately become inaccessible, as if by closing it. All attempts at accessing an object that has been sent, even if that object has not yet been received, should cause an exception to be thrown.

        Calling Resource.close() on an object that has been sent will have no effect, so this method is safe to call within a try-with-resources statement.

        Specified by:
        send in interface Resource<Buffer>
      • close

        public void close()
        Description copied from interface: Resource
        Close the resource, making it inaccessible.

        Note, this method is not thread-safe unless otherwise specified.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Resource<Buffer>
      • isAccessible

        public boolean isAccessible()
        Description copied from interface: Resource
        Check if this object is accessible.
        Specified by:
        isAccessible in interface Resource<Buffer>
        Returns:
        true if this object is still valid and can be accessed, otherwise false if, for instance, this object has been dropped/deallocated, or been sent elsewhere.
      • touch

        public Buffer touch​(Object hint)
        Description copied from interface: Resource
        Record the current access location for debugging purposes. This information may be included if the resource throws a life-cycle related exception, or if it leaks. If this resource has already been closed, then this method has no effect.
        Specified by:
        touch in interface Resource<Buffer>
        Parameters:
        hint - An optional hint about this access and its context. May be null.
        Returns:
        This resource instance.
      • toString

        public String toString​(Charset charset)
        Description copied from interface: Buffer
        Decodes this buffer's readable bytes into a string with the specified Charset.

        This method does not modify the reader or writer offset of this buffer.

        Specified by:
        toString in interface Buffer
        Parameters:
        charset - used for decoding.
        Returns:
        Buffer's readable bytes as a string.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object