ChunkBuffer

open class ChunkBuffer(memory: Memory, origin: ChunkBuffer?, parentPool: ObjectPool<ChunkBuffer>?) : Buffer

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun cleanNext(): ChunkBuffer?
Link copied to clipboard
fun commitWritten(count: Int)
Link copied to clipboard
fun discardExact(count: Int = readRemaining)

Discard count readable bytes.

Link copied to clipboard
open override fun duplicate(): ChunkBuffer

Create a new Buffer instance pointing to the same memory and having the same positions.

Link copied to clipboard
fun readByte(): Byte

Read the next byte or fail with EOFException if it's not available. The returned byte is marked as consumed.

Link copied to clipboard
open fun release(pool: ObjectPool<ChunkBuffer>)
Link copied to clipboard
fun reserveEndGap(endGap: Int)

Reserve endGap bytes in the end. Could move readPosition and writePosition to reserve space but only when no bytes were written or all written bytes are marked as consumed (were read or discarded).

Link copied to clipboard
fun reserveStartGap(startGap: Int)

Reserve startGap bytes in the beginning. May move readPosition and writePosition if no bytes available for reading.

Link copied to clipboard
override fun reset()

Clear buffer's state: read/write positions, gaps and so on. Byte content is not cleaned-up.

Link copied to clipboard
fun resetForRead()

Marks the whole buffer available for read and no for write

Link copied to clipboard
fun resetForWrite()

Marks all capacity writable except the start gap reserved before. The end gap reservation is discarded.

fun resetForWrite(limit: Int)

Marks up to limit bytes of the buffer available for write and no bytes for read. It does respect startGap already reserved. All extra bytes after the specified limit are considered as endGap.

Link copied to clipboard
fun rewind(count: Int = readPosition - startGap)

Rewind readPosition backward to make count bytes available for reading again.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun tryPeekByte(): Int

Peek the next unsigned byte or return -1 if no more bytes available for reading. No bytes will be marked as consumed in any case.

Link copied to clipboard
fun tryReadByte(): Int

Read the next unsigned byte or return -1 if no more bytes available for reading. The returned byte is marked as consumed.

Link copied to clipboard
fun writeByte(value: Byte)

Write a byte value at writePosition (incremented when written successfully).

Properties

Link copied to clipboard
val capacity: Int

Buffer's capacity (including reserved startGap and endGap). Value for released buffer is unspecified.

Link copied to clipboard
val endGap: Int

Number of bytes reserved in the end.

Link copied to clipboard
var limit: Int

Write position limit. No bytes could be written ahead of this limit. When the limit is less than the capacity then this means that there are reserved bytes in the end (endGap). Such a reserved space in the end could be used to write size, hash and so on. Also it is useful when several buffers are connected into a chain and some primitive value (e.g. kotlin.Int) is separated into two chunks so bytes from the second chain could be copied to the reserved space of the first chunk and then the whole value could be read at once.

Link copied to clipboard
val memory: Memory
Link copied to clipboard
var next: ChunkBuffer?

Reference to next buffer view. Useful to chain multiple views.

Link copied to clipboard
var origin: ChunkBuffer?

Reference to an origin buffer view this was copied from

Link copied to clipboard
var readPosition: Int = 0

Current read position. It is always non-negative and will never run ahead of the writePosition. It is usually greater or equal to startGap reservation. This position is affected by discard, rewind, resetForRead, resetForWrite, reserveStartGap and reserveEndGap.

Link copied to clipboard
val readRemaining: Int

Number of bytes available for reading.

Link copied to clipboard
val referenceCount: Int
Link copied to clipboard
var startGap: Int = 0

Start gap is a reserved space in the beginning. The reserved space is usually used to write a packet length in the case when it's not known before the packet constructed.

Link copied to clipboard
var writePosition: Int = 0

Current write position. It is always non-negative and will never run ahead of the limit. It is always greater or equal to the readPosition.

Link copied to clipboard
val writeRemaining: Int

Size of the free space available for writing in bytes.

Extensions

Link copied to clipboard
fun ChunkBuffer.makeView(): ChunkBuffer
Link copied to clipboard
inline fun ChunkBuffer.readAvailable(destination: ByteArray, offset: Int = 0, length: Int = destination.size - offset): Int
Link copied to clipboard
inline fun ChunkBuffer.readDirect(block: (ByteBuffer) -> Unit): Int

Apply block function on a ByteBuffer of readable bytes. The block function should return number of consumed bytes.

Link copied to clipboard
inline fun ChunkBuffer.readDouble(): Double
Link copied to clipboard
inline fun ChunkBuffer.readFloat(): Float
Link copied to clipboard
inline fun ChunkBuffer.readFully(destination: ByteArray, offset: Int = 0, length: Int = destination.size - offset)
Link copied to clipboard
inline fun ChunkBuffer.readInt(): Int
Link copied to clipboard
inline fun ChunkBuffer.readLong(): Long
Link copied to clipboard
inline fun ChunkBuffer.readShort(): Short
Link copied to clipboard
inline fun ChunkBuffer.readUByte(): UByte
Link copied to clipboard
inline fun ChunkBuffer.readUInt(): UInt
Link copied to clipboard
inline fun ChunkBuffer.readULong(): ULong
Link copied to clipboard
inline fun ChunkBuffer.readUShort(): UShort
Link copied to clipboard
inline fun ChunkBuffer.writeDirect(size: Int, block: (ByteBuffer) -> Unit): Int

Apply block function on a ByteBuffer of the free space. The block function should return number of written bytes.

Link copied to clipboard
inline fun ChunkBuffer.writeDouble(value: Double)
Link copied to clipboard
inline fun ChunkBuffer.writeFloat(value: Float)
Link copied to clipboard
inline fun ChunkBuffer.writeFully(source: ByteArray, offset: Int = 0, length: Int = source.size - offset)
Link copied to clipboard
inline fun ChunkBuffer.writeInt(value: Int)
Link copied to clipboard
inline fun ChunkBuffer.writeLong(value: Long)
Link copied to clipboard
inline fun ChunkBuffer.writeShort(value: Short)
Link copied to clipboard
fun ChunkBuffer.writeUByte(value: UByte)
Link copied to clipboard
inline fun ChunkBuffer.writeUInt(value: UInt)
Link copied to clipboard
inline fun ChunkBuffer.writeULong(value: ULong)
Link copied to clipboard
inline fun ChunkBuffer.writeUShort(value: UShort)