Class BufferBuilder

java.lang.Object
org.apache.flink.runtime.io.network.buffer.BufferBuilder
All Implemented Interfaces:
AutoCloseable

@NotThreadSafe public class BufferBuilder extends Object implements AutoCloseable
Not thread safe class for filling in the content of the MemorySegment. To access written data please use BufferConsumer which allows to build Buffer instances from the written data.
  • Constructor Details

    • BufferBuilder

      public BufferBuilder(org.apache.flink.core.memory.MemorySegment memorySegment, BufferRecycler recycler)
  • Method Details

    • createBufferConsumer

      public BufferConsumer createBufferConsumer()
      This method always creates a BufferConsumer starting from the current writer offset. Data written to BufferBuilder before creation of BufferConsumer won't be visible for that BufferConsumer.
      Returns:
      created matching instance of BufferConsumer to this BufferBuilder.
    • createBufferConsumerFromBeginning

      public BufferConsumer createBufferConsumerFromBeginning()
      This method always creates a BufferConsumer starting from position 0 of MemorySegment.
      Returns:
      created matching instance of BufferConsumer to this BufferBuilder.
    • getDataType

      public Buffer.DataType getDataType()
      Gets the data type of the internal buffer.
    • setDataType

      public void setDataType(Buffer.DataType dataType)
      Sets the data type of the internal buffer.
    • appendAndCommit

      public int appendAndCommit(ByteBuffer source)
      Same as append(ByteBuffer) but additionally commit() the appending.
    • append

      public int append(ByteBuffer source)
      Append as many data as possible from source. Not everything might be copied if there is not enough space in the underlying MemorySegment
      Returns:
      number of copied bytes
    • commit

      public void commit()
      Make the change visible to the readers. This is costly operation (volatile access) thus in case of bulk writes it's better to commit them all together instead one by one.
    • finish

      public int finish()
      Mark this BufferBuilder and associated BufferConsumer as finished - no new data writes will be allowed.

      This method should be idempotent to handle failures and task interruptions. Check FLINK-8948 for more details.

      Returns:
      number of written bytes.
    • isFinished

      public boolean isFinished()
    • isFull

      public boolean isFull()
    • getWritableBytes

      public int getWritableBytes()
    • getCommittedBytes

      public int getCommittedBytes()
    • getMaxCapacity

      public int getMaxCapacity()
    • trim

      public void trim(int newSize)
      The result capacity can not be greater than allocated memorySegment. It also can not be less than already written data.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable