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 theMemorySegment
. To access written data please useBufferConsumer
which allows to buildBuffer
instances from the written data.
-
-
Constructor Summary
Constructors Constructor Description BufferBuilder(MemorySegment memorySegment, BufferRecycler recycler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
append(ByteBuffer source)
Append as many data as possible fromsource
.int
appendAndCommit(ByteBuffer source)
Same asappend(ByteBuffer)
but additionallycommit()
the appending.void
close()
void
commit()
Make the change visible to the readers.BufferConsumer
createBufferConsumer()
This method always creates aBufferConsumer
starting from the current writer offset.BufferConsumer
createBufferConsumerFromBeginning()
This method always creates aBufferConsumer
starting from position 0 ofMemorySegment
.int
finish()
Mark thisBufferBuilder
and associatedBufferConsumer
as finished - no new data writes will be allowed.int
getCommittedBytes()
Buffer.DataType
getDataType()
Gets the data type of the internal buffer.int
getMaxCapacity()
int
getWritableBytes()
boolean
isFinished()
boolean
isFull()
void
setDataType(Buffer.DataType dataType)
Sets the data type of the internal buffer.void
trim(int newSize)
The result capacity can not be greater than allocated memorySegment.
-
-
-
Constructor Detail
-
BufferBuilder
public BufferBuilder(MemorySegment memorySegment, BufferRecycler recycler)
-
-
Method Detail
-
createBufferConsumer
public BufferConsumer createBufferConsumer()
This method always creates aBufferConsumer
starting from the current writer offset. Data written toBufferBuilder
before creation ofBufferConsumer
won't be visible for thatBufferConsumer
.- Returns:
- created matching instance of
BufferConsumer
to thisBufferBuilder
.
-
createBufferConsumerFromBeginning
public BufferConsumer createBufferConsumerFromBeginning()
This method always creates aBufferConsumer
starting from position 0 ofMemorySegment
.- Returns:
- created matching instance of
BufferConsumer
to thisBufferBuilder
.
-
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 asappend(ByteBuffer)
but additionallycommit()
the appending.
-
append
public int append(ByteBuffer source)
Append as many data as possible fromsource
. Not everything might be copied if there is not enough space in the underlyingMemorySegment
- 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 thisBufferBuilder
and associatedBufferConsumer
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 interfaceAutoCloseable
-
-