Class ByteBufDataChunk

  • All Implemented Interfaces:
    DataChunk, Iterable<ByteBuffer>

    public class ByteBufDataChunk
    extends Object
    implements DataChunk
    A special DataChunk implementation based on Netty's buffers. This is used by our Jersey SPI implementation to take advantage of Netty's buffer pooling.
    • Method Detail

      • create

        public static ByteBufDataChunk create​(boolean flush,
                                              boolean readOnly,
                                              io.netty.buffer.ByteBuf... byteBufs)
        Creates an instance given an array of ByteBuf's.
        Parameters:
        flush - a signal that this chunk should be written and flushed from any cache if possible
        readOnly - marks this buffer as read only
        byteBufs - the data for this chunk. Should not be reused until releaseCallback is used
        Returns:
        new chunk
      • create

        public static ByteBufDataChunk create​(boolean flush,
                                              boolean readOnly,
                                              Runnable releaseCallback,
                                              io.netty.buffer.ByteBuf... byteBufs)
        Creates an instance given an array of ByteBuf's.
        Parameters:
        flush - a signal that this chunk should be written and flushed from any cache if possible
        readOnly - marks this buffer as read only
        releaseCallback - a callback which is called when this chunk is completely processed
        byteBufs - the data for this chunk. Should not be reused until releaseCallback is used
        Returns:
        new chunk
      • isBackedBy

        public <T> boolean isBackedBy​(Class<T> clazz)
        Description copied from interface: DataChunk
        Checks if this instance is backed by buffers of a certain kind.
        Specified by:
        isBackedBy in interface DataChunk
        Type Parameters:
        T - the buffer type
        Parameters:
        clazz - a buffer class instance
        Returns:
        outcome of test
      • data

        public <T> T[] data​(Class<T> clazz)
        Description copied from interface: DataChunk
        Returns a representation of this chunk as an array of T's.
        Specified by:
        data in interface DataChunk
        Type Parameters:
        T - the buffer type
        Parameters:
        clazz - class of return type
        Returns:
        an array of T's
      • isReleased

        public boolean isReleased()
        Description copied from interface: DataChunk
        Whether this chunk is released and the associated data structures returned by methods (such as DataChunk.iterator() or DataChunk.bytes()) should not be used. The implementations may choose to not implement this optimization and to never mutate the underlying memory; in such case this method does no-op.

        Note that the methods of this instance are expected to be called by a single thread; if not, external synchronization must be used.

        Specified by:
        isReleased in interface DataChunk
        Returns:
        whether this chunk has been released, defaults to false
      • flush

        public boolean flush()
        Description copied from interface: DataChunk
        Returns true if all caches are requested to flush when this chunk is written. This method is only meaningful when handing data over to Helidon APIs (e.g. for server response and client requests).
        Specified by:
        flush in interface DataChunk
        Returns:
        true if it is requested to flush all caches after this chunk is written, defaults to false.
      • isReadOnly

        public boolean isReadOnly()
        Description copied from interface: DataChunk
        Returns true if the underlying byte buffer of this chunk is read only or false otherwise.
        Specified by:
        isReadOnly in interface DataChunk
        Returns:
        Immutability outcome.
      • release

        public void release()
        Description copied from interface: DataChunk
        Releases this chunk. The underlying data as well as the data structure instances returned by methods DataChunk.bytes() and DataChunk.iterator() may become stale and should not be used anymore. The implementations may choose to not implement this optimization and to never mutate the underlying memory; in such case this method does no-op.

        Note that the methods of this instance are expected to be called by a single thread; if not, external synchronization must be used.

        Specified by:
        release in interface DataChunk
      • writeFuture

        public void writeFuture​(CompletableFuture<DataChunk> writeFuture)
        Description copied from interface: DataChunk
        Set a write future that will complete when data chunk has been written to a connection.
        Specified by:
        writeFuture in interface DataChunk
        Parameters:
        writeFuture - Write future.
      • remaining

        public int remaining()
        Description copied from interface: DataChunk
        Returns the sum of elements between the current position and the limit of each of the underlying ByteBuffer.
        Specified by:
        remaining in interface DataChunk
        Returns:
        The number of elements remaining in all underlying buffers
      • data

        public ByteBuffer[] data()
        This method is needed for testing some of our examples. It bypasses the optimization for which this class was created.
        Specified by:
        data in interface DataChunk
        Returns:
        array of ByteBuffer
      • duplicate

        public DataChunk duplicate()
        Description copied from interface: DataChunk
        Makes a copy of this data chunk including its underlying ByteBuffer. This may be necessary for caching in case ByteBuffer.rewind() is called to reuse a byte buffer. Note that only the actual bytes used in the data chunk are copied, the resulting data chunk's capacity may be less than the original.
        Specified by:
        duplicate in interface DataChunk
        Returns:
        A copy of this data chunk.
      • bytes

        public byte[] bytes()
        Description copied from interface: DataChunk
        Gets the content of the underlying byte buffers as an array of bytes. The returned array contains only the part of data that wasn't read yet. Calling this method doesn't cause the underlying byte buffers to be read.

        It is expected the returned byte array holds a reference to data that will become stale upon calling method DataChunk.release(). (For instance, the memory segment is pooled by the underlying TCP server and is reused for a subsequent request chunk.) The idea behind this class is to be able to minimize data copying; ideally, in order to achieve the best performance, to not copy them at all. However, the implementations may choose otherwise.

        Note that the methods of this instance are expected to be called by a single thread; if not, external synchronization must be used.

        Specified by:
        bytes in interface DataChunk
        Returns:
        an array of bytes that is guarantied to stay immutable as long as method DataChunk.release() is not called