ChunkBuffer

open class ChunkBuffer : Buffer

Properties

next

var next: ChunkBuffer?

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

origin

var origin: ChunkBuffer?

Reference to an origin buffer view this was copied from

referenceCount

val referenceCount: Int

Inherited Properties

attachment

var attachment: Any?

User data: could be a session, connection or anything useful

capacity

val capacity: Int

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

endGap

val endGap: Int

Number of bytes reserved in the end.

limit

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.

memory

val memory: Memory

readPosition

var readPosition: Int

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.

readRemaining

val readRemaining: Int

Number of bytes available for reading.

startGap

var startGap: Int

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.

writePosition

var writePosition: Int

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.

writeRemaining

val writeRemaining: Int

Size of the free space available for writing in bytes.

Functions

cleanNext

fun cleanNext(): ChunkBuffer?

duplicate

open fun duplicate(): ChunkBuffer

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

release

open fun release(pool: ObjectPool<ChunkBuffer>): Unit

reset

fun reset(): Unit

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

Inherited Functions

commitWritten

fun commitWritten(count: Int): Unit

discard

fun discard(count: Int): Int
fun discard(count: Long): Long

discardExact

fun discardExact(count: Int = readRemaining): Unit

Discard count readable bytes.

duplicateTo

open fun duplicateTo(copy: Buffer): Unit

readByte

fun readByte(): Byte

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

reserveEndGap

fun reserveEndGap(endGap: Int): Unit

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).

reserveStartGap

fun reserveStartGap(startGap: Int): Unit

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

resetForRead

fun resetForRead(): Unit

Marks the whole buffer available for read and no for write

resetForWrite

fun resetForWrite(): Unit

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

fun resetForWrite(limit: Int): Unit

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.

rewind

fun rewind(count: Int = readPosition - startGap): Unit

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

toString

open fun toString(): String

tryPeekByte

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.

tryReadByte

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.

writeByte

fun writeByte(value: Byte): Unit

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

Companion Object Properties

Empty

val Empty: ChunkBuffer

EmptyPool

val EmptyPool: ObjectPool<ChunkBuffer>

A pool that always returns ChunkBuffer.Empty

Pool

Extension Properties

byteOrder

var Buffer.byteOrder: ByteOrder

Extension Functions

append

fun Buffer.append(c: Char): Buffer
fun Buffer.append(csq: CharSequence?): Buffer
fun Buffer.append(
    csq: CharSequence?,
    start: Int,
    end: Int
): Buffer
fun Buffer.append(
    csq: CharArray,
    start: Int,
    end: Int
): Buffer

canRead

fun Buffer.canRead(): Boolean

canWrite

fun Buffer.canWrite(): Boolean

decodeUTF8

fun Buffer.decodeUTF8(consumer: (Char) -> Boolean): Int

Decodes all the bytes to utf8 applying every character on consumer until or consumer return false. If a consumer returned false then a character will be pushed back (including all surrogates will be pushed back as well) and decodeUTF8 returns -1

fill

fun Buffer.fill(times: Int, value: Byte): Unit

Write byte value repeated the specified times.

fun Buffer.fill(times: Int, value: <ERROR CLASS>): Unit

Write unsigned byte value repeated the specified times.

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

Write byte v value repeated n times.

flush

fun Buffer.flush(): Unit

forEach

fun Buffer.forEach(block: (Byte) -> Unit): Unit

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

makeView

fun ChunkBuffer.makeView(): ChunkBuffer

pushBack

fun Buffer.pushBack(n: Int): Unit

Push back n bytes: only possible if there were at least n bytes read before this operation.

read

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.

readAvailable

fun Buffer.readAvailable(
    destination: ByteArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: <ERROR CLASS>,
    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: ShortArray,
    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: FloatArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: DoubleArray,
    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).

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(
    dst: ByteBuffer,
    length: Int = dst.remaining()
): Int

readAvailableLittleEndian

fun Buffer.readAvailableLittleEndian(
    dst: <ERROR CLASS>,
    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: 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: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int

readBytes

fun Buffer.readBytes(count: Int = readRemaining): ByteArray

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

readDirect

fun Buffer.readDirect(block: (ByteBuffer) -> Unit): Int

readDouble

fun Buffer.readDouble(): Double

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).

readDoubleLittleEndian

fun Buffer.readDoubleLittleEndian(): Double

readFloat

fun Buffer.readFloat(): Float

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).

readFloatLittleEndian

fun Buffer.readFloatLittleEndian(): Float

readFully

fun Buffer.readFully(
    dst: Array<Byte>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFully(dst: ByteBuffer, length: Int): Unit
fun Buffer.readFully(
    destination: ByteArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Buffer.readFully(
    destination: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit

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

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

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: Buffer,
    length: Int = dst.writeRemaining
): Int

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

fun Buffer.readFully(destination: ByteBuffer): Unit

Read buffer’s content to the destination buffer moving it’s position.

readFullyLittleEndian

fun Buffer.readFullyLittleEndian(
    dst: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit

readInt

fun Buffer.readInt(): Int

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

readIntLittleEndian

fun Buffer.readIntLittleEndian(): Int

readLong

fun Buffer.readLong(): Long

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

readLongLittleEndian

fun Buffer.readLongLittleEndian(): Long

readShort

fun Buffer.readShort(): Short

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

readShortLittleEndian

fun Buffer.readShortLittleEndian(): Short

readText

fun Buffer.readText(
    decoder: CharsetDecoder,
    out: Appendable,
    lastBuffer: Boolean,
    max: Int = Int.MAX_VALUE
): Int
fun Buffer.readText(
    charset: Charset = Charsets.UTF_8,
    max: Int = Int.MAX_VALUE
): String

readUByte

fun Buffer.readUByte(): <ERROR CLASS>

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

readUInt

fun Buffer.readUInt(): <ERROR CLASS>

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

readULong

fun Buffer.readULong(): <ERROR CLASS>

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).

readUShort

fun Buffer.readUShort(): <ERROR CLASS>

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).

remainingAll

fun ChunkBuffer.remainingAll(): Long

Summarize remainings of all elements of the chain

tryPeek

fun Buffer.tryPeek(): 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.

write

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.

writeDirect

fun Buffer.writeDirect(
    size: Int = 1,
    block: (ByteBuffer) -> Unit
): Int

writeDouble

fun Buffer.writeDouble(value: Double): Unit

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).

writeDoubleLittleEndian

fun Buffer.writeDoubleLittleEndian(value: Double): Unit

writeFloat

fun Buffer.writeFloat(value: Float): Unit

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).

writeFloatLittleEndian

fun Buffer.writeFloatLittleEndian(value: Float): Unit

writeFully

fun Buffer.writeFully(
    source: ByteArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFully(
    source: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit

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

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

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).

fun Buffer.writeFully(src: Buffer): Unit

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): Unit

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: ByteBuffer): Unit

Write source buffer content moving it’s position.

writeFullyLittleEndian

fun Buffer.writeFullyLittleEndian(
    source: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFullyLittleEndian(
    source: ShortArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFullyLittleEndian(
    source: IntArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFullyLittleEndian(
    source: LongArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFullyLittleEndian(
    source: FloatArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFullyLittleEndian(
    source: DoubleArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit

writeInt

fun Buffer.writeInt(value: Int): Unit

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

writeIntLittleEndian

fun Buffer.writeIntLittleEndian(value: Int): Unit

writeLong

fun Buffer.writeLong(value: Long): Unit

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

writeLongLittleEndian

fun Buffer.writeLongLittleEndian(value: Long): Unit

writeShort

fun Buffer.writeShort(value: Short): Unit

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

writeShortLittleEndian

fun Buffer.writeShortLittleEndian(value: Short): Unit

writeUByte

fun Buffer.writeUByte(value: <ERROR CLASS>): Unit

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

writeUInt

fun Buffer.writeUInt(value: <ERROR CLASS>): Unit

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

writeULong

fun Buffer.writeULong(value: <ERROR CLASS>): Unit

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).

writeUShort

fun Buffer.writeUShort(value: <ERROR CLASS>): Unit

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).

Inheritors

IoBuffer

expect class IoBuffer : Input, Output, ChunkBuffer

A read-write facade to actual buffer of fixed size. Multiple views could share the same actual buffer. Concurrent unsafe. The only concurrent-safe operation is release. In most cases ByteReadPacket and BytePacketBuilder should be used instead.