ChunkBuffer

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

Deprecated

We're migrating to the new kotlinx-io library. This declaration is deprecated and will be removed in Ktor 4.0.0 If you have any problems with migration, please contact us in https://youtrack.jetbrains.com/issue/KTOR-6030/Migrate-to-new-kotlinx.io-library

Constructors

Link copied to clipboard
constructor(memory: Memory, origin: ChunkBuffer?, parentPool: ObjectPool<ChunkBuffer>?)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

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
Link copied to clipboard

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

Link copied to clipboard

Reference to an origin buffer view this was copied from

Link copied to clipboard

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

Number of bytes available for reading.

Link copied to clipboard
Link copied to clipboard

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

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

Size of the free space available for writing in bytes.

Functions

Link copied to clipboard
inline fun Buffer.canRead(): Boolean
Link copied to clipboard
inline fun Buffer.canWrite(): Boolean
Link copied to clipboard
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 Buffer.fill(times: Int, value: Byte)

Write byte value repeated the specified times.

fun Buffer.fill(times: Int, value: UByte)

Write unsigned byte value repeated the specified times.

fun Buffer.fill(n: Long, v: Byte)

Write byte v value repeated n times.

Link copied to clipboard
inline fun Buffer.forEach(block: (Byte) -> Unit)

For every byte from this buffer invokes block function giving it as parameter.

Link copied to clipboard
inline fun Buffer.read(block: (memory: Memory, start: Int, endExclusive: Int) -> Int): Int

Apply block of code with buffer's memory providing read range indices. The returned value of block lambda should return number of bytes to be marked as consumed. No read/write functions on this buffer should be called inside of block otherwise an undefined behaviour may occur including data damage.

Link copied to clipboard
fun Buffer.readAvailable(dst: Buffer, length: Int = dst.writeRemaining): Int

Read at most length available bytes to the dst buffer or -1 if no bytes available for read.

fun Buffer.readAvailable(destination: ByteArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: UByteArray, offset: Int = 0, length: Int = destination.size - offset): Int

Read available for read bytes to the destination array range starting at array offset and length bytes. If less than length bytes available then less bytes will be copied and the corresponding number will be returned as result.

fun Buffer.readAvailable(destination: DoubleArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: FloatArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: IntArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: LongArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: ShortArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: UIntArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: ULongArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(destination: UShortArray, offset: Int = 0, length: Int = destination.size - offset): Int

Read available for read bytes to the destination array range starting at array offset and length elements. If less than length elements available then less elements will be copied and the corresponding number will be returned as result (possibly zero).

inline fun ChunkBuffer.readAvailable(destination: ByteArray, offset: Int = 0, length: Int = destination.size - offset): Int
fun Buffer.readAvailable(dst: ArrayBuffer, offset: Int = 0, length: Int = dst.byteLength - offset): Int
fun Buffer.readAvailable(dst: ArrayBufferView, offset: Int = 0, length: Int = dst.byteLength - offset): Int
fun Buffer.readAvailable(dst: ByteBuffer, length: Int = dst.remaining()): Int
fun Buffer.readAvailable(pointer: CPointer<ByteVar>, offset: Int, length: Int): Int
fun Buffer.readAvailable(pointer: CPointer<ByteVar>, offset: Long, length: Int): Int
Link copied to clipboard
fun Buffer.readAvailableLittleEndian(dst: DoubleArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: FloatArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: IntArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: LongArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: ShortArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: UIntArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: ULongArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Buffer.readAvailableLittleEndian(dst: UShortArray, offset: Int = 0, length: Int = dst.size - offset): Int
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
fun Buffer.readBytes(count: Int = readRemaining): ByteArray

Read the specified number of bytes specified (optional, read all remaining by default)

Link copied to clipboard
inline fun Buffer.readDirect(block: (DataView) -> Int): Int
inline fun Buffer.readDirect(block: (ByteBuffer) -> Unit): Int

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.

inline fun Buffer.readDirect(block: (CPointer<ByteVar>) -> Int): Int
Link copied to clipboard
inline fun Buffer.readDirectInt8Array(block: (Int8Array) -> Int): Int
Link copied to clipboard

Read a floating point number or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

Link copied to clipboard

Read a floating point number or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

Link copied to clipboard
Link copied to clipboard
fun Buffer.readFully(dst: Buffer, length: Int = dst.writeRemaining): Int

Read at most length bytes from this buffer to the dst buffer.

fun Buffer.readFully(dst: Array<Byte>, offset: Int = 0, length: Int = dst.size - offset)
inline fun ChunkBuffer.readFully(destination: ByteArray, offset: Int = 0, length: Int = destination.size - offset)

fun Buffer.readFully(destination: ByteArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: UByteArray, offset: Int = 0, length: Int = destination.size - offset)

Read from this buffer to the destination array to offset and length bytes.

fun Buffer.readFully(destination: DoubleArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: FloatArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: IntArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: LongArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: ShortArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: UIntArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: ULongArray, offset: Int = 0, length: Int = destination.size - offset)
fun Buffer.readFully(destination: UShortArray, offset: Int = 0, length: Int = destination.size - offset)

Read from this buffer to the destination array to offset and length bytes. Numeric values are interpreted in the network byte order (Big Endian).

fun Buffer.readFully(dst: ArrayBuffer, offset: Int = 0, length: Int = dst.byteLength - offset)
fun Buffer.readFully(dst: ArrayBufferView, offset: Int = 0, length: Int = dst.byteLength - offset)
fun Buffer.readFully(destination: ByteBuffer)

Read buffer's content to the destination buffer moving its position.

fun Buffer.readFully(dst: ByteBuffer, length: Int)
fun Buffer.readFully(pointer: CPointer<ByteVar>, offset: Int, length: Int)
fun Buffer.readFully(pointer: CPointer<ByteVar>, offset: Long, length: Int)
Link copied to clipboard
fun Buffer.readFullyLittleEndian(dst: DoubleArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: FloatArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: IntArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: LongArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: ShortArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: UIntArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: ULongArray, offset: Int = 0, length: Int = dst.size - offset)
fun Buffer.readFullyLittleEndian(dst: UShortArray, offset: Int = 0, length: Int = dst.size - offset)
Link copied to clipboard

Read an integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

inline fun ChunkBuffer.readInt(): Int
Link copied to clipboard
Link copied to clipboard

Read a long integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

inline fun ChunkBuffer.readLong(): Long
Link copied to clipboard
Link copied to clipboard

Read a short integer or fail if no bytes available for reading. The numeric value is decoded in the network order (Big Endian).

Link copied to clipboard
Link copied to clipboard
fun Buffer.readText(charset: Charset = Charsets.UTF_8, max: Int = Int.MAX_VALUE): String

Reads at most max characters decoding bytes with specified charset. Extra character bytes will remain unconsumed

Link copied to clipboard

Read an unsigned byte or fail if no bytes available for reading.

Link copied to clipboard

Read an unsigned integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

inline fun ChunkBuffer.readUInt(): UInt
Link copied to clipboard

Read an unsigned long integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

Link copied to clipboard

Read an unsigned short integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

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

Marks the whole buffer available for read and no for write

Link copied to clipboard

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

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

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
inline fun Buffer.write(block: (memory: Memory, start: Int, endExclusive: Int) -> Int): Int

Apply block of code with buffer's memory providing write range indices. The returned value of block lambda should return number of bytes were written. o read/write functions on this buffer should be called inside of block otherwise an undefined behaviour may occur including data damage.

Link copied to clipboard
fun writeByte(value: Byte)

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

Link copied to clipboard
inline fun Buffer.writeDirect(block: (DataView) -> Int): Int
inline fun Buffer.writeDirect(size: Int = 1, block: (ByteBuffer) -> Unit): Int

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.

inline fun Buffer.writeDirect(block: (CPointer<ByteVar>) -> Int): Int
Link copied to clipboard
inline fun Buffer.writeDirectInt8Array(block: (Int8Array) -> Int): Int
Link copied to clipboard

Write a floating point number or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeDouble(value: Double)
Link copied to clipboard
Link copied to clipboard
fun Buffer.writeFloat(value: Float)

Write a floating point number or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeFloat(value: Float)
Link copied to clipboard
Link copied to clipboard

Write all readable bytes from src to this buffer. Fails if not enough space available to write all bytes.

fun Buffer.writeFully(src: Buffer, length: Int)

Write at most length readable bytes from src to this buffer. Fails if not enough space available to write all bytes.

fun Buffer.writeFully(source: ByteArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: UByteArray, offset: Int = 0, length: Int = source.size - offset)

Write the whole source array range staring at offset and having the specified bytes length.

fun Buffer.writeFully(source: DoubleArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: FloatArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: IntArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: LongArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: ShortArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: UIntArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: ULongArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(source: UShortArray, offset: Int = 0, length: Int = source.size - offset)

Write the whole source array range staring at offset and having the specified items length. Numeric values are interpreted in the network byte order (Big Endian).

inline fun ChunkBuffer.writeFully(source: ByteArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFully(src: ArrayBuffer, offset: Int = 0, length: Int = src.byteLength)
fun Buffer.writeFully(src: ArrayBufferView, offset: Int = 0, length: Int = src.byteLength - offset)

Write source buffer content moving its position.

fun Buffer.writeFully(pointer: CPointer<ByteVar>, offset: Int, length: Int)
fun Buffer.writeFully(pointer: CPointer<ByteVar>, offset: Long, length: Int)
Link copied to clipboard
fun Buffer.writeFullyLittleEndian(source: DoubleArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: FloatArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: IntArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: LongArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: ShortArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: UIntArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: ULongArray, offset: Int = 0, length: Int = source.size - offset)
fun Buffer.writeFullyLittleEndian(source: UShortArray, offset: Int = 0, length: Int = source.size - offset)
Link copied to clipboard
fun Buffer.writeInt(value: Int)

Write an integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeInt(value: Int)
Link copied to clipboard
Link copied to clipboard
fun Buffer.writeLong(value: Long)

Write a long integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeLong(value: Long)
Link copied to clipboard
Link copied to clipboard
fun Buffer.writeShort(value: Short)

Write a short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeShort(value: Short)
Link copied to clipboard
Link copied to clipboard
fun Buffer.writeUByte(value: UByte)

Write an unsigned byte or fail if not enough space available for writing.

Link copied to clipboard
fun Buffer.writeUInt(value: UInt)

Write an unsigned integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeUInt(value: UInt)
Link copied to clipboard
fun Buffer.writeULong(value: ULong)

Write an unsigned long integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeULong(value: ULong)
Link copied to clipboard

Write an unsigned short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

inline fun ChunkBuffer.writeUShort(value: UShort)