Class BufferConsumer

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    @NotThreadSafe
    public class BufferConsumer
    extends Object
    implements Closeable
    Not thread safe class for producing Buffer.

    It reads data written by BufferBuilder. Although it is not thread safe and can be used only by one single thread, this thread can be different than the thread using/writing to BufferBuilder. Pattern here is simple: one thread writes data to BufferBuilder and there can be a different thread reading from it using BufferConsumer.

    • Constructor Detail

      • BufferConsumer

        public BufferConsumer​(Buffer buffer,
                              int size)
        Constructs BufferConsumer instance with static content of a certain size.
      • BufferConsumer

        public BufferConsumer​(Buffer buffer,
                              org.apache.flink.runtime.io.network.buffer.BufferBuilder.PositionMarker currentWriterPosition,
                              int currentReaderPosition)
    • Method Detail

      • isFinished

        public boolean isFinished()
        Checks whether the BufferBuilder has already been finished.

        BEWARE: this method accesses the cached value of the position marker which is only updated after calls to build() and skip(int)!

        Returns:
        true if the buffer was finished, false otherwise
      • build

        public Buffer build()
        Returns:
        sliced Buffer containing the not yet consumed data. Returned Buffer shares the reference counter with the parent BufferConsumer - in order to recycle memory both of them must be recycled/closed.
      • copy

        public BufferConsumer copy()
        Returns a retained copy with separate indexes. This allows to read from the same MemorySegment twice.

        WARNING: the newly returned BufferConsumer will have its reader index copied from the original buffer. In other words, data already consumed before copying will not be visible to the returned copies.

        Returns:
        a retained copy of self with separate indexes
      • copyWithReaderPosition

        public BufferConsumer copyWithReaderPosition​(int readerPosition)
        Returns a retained copy with separate indexes and sets the reader position to the given value. This allows to read from the same MemorySegment twice starting from the supplied position.
        Parameters:
        readerPosition - the new reader position. Can be less than the currentReaderPosition, but may not exceed the current writer's position.
        Returns:
        a retained copy of self with separate indexes
      • isBuffer

        public boolean isBuffer()
      • isRecycled

        public boolean isRecycled()
      • getWrittenBytes

        public int getWrittenBytes()
      • isDataAvailable

        public boolean isDataAvailable()
        Returns true if there is new data available for reading.
      • toDebugString

        public String toDebugString​(boolean includeHash)