BytePacketBuilder

class BytePacketBuilder(pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool) : Output

A builder that provides ability to build byte packets with no knowledge of it's size. Unlike Java's ByteArrayOutputStream it doesn't copy the whole content every time it's internal buffer overflows but chunks buffers instead. Packet building via build function is O(1) operation and only does instantiate a new ByteReadPacket. Once a byte packet has been built via build function call, the builder could be reused again. You also can discard all written bytes via reset or release. Please note that an instance of builder need to be terminated either via build function invocation or via release call otherwise it will cause byte buffer leak so that may have performance impact.

Byte packet builder is also an Appendable so it does append UTF-8 characters to a packet

buildPacket {
listOf(1,2,3).joinTo(this, separator = ",")
}

Constructors

Link copied to clipboard
constructor(pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool)

Properties

Link copied to clipboard

If no bytes were written or the builder has been reset.

Link copied to clipboard

If at least one byte was written after the creation or the last reset.

Link copied to clipboard
val size: Int

Number of bytes written to the builder after the creation or the last reset.

Functions

Link copied to clipboard
open override fun append(value: Char): BytePacketBuilder

Append single UTF-8 character

open override fun append(value: CharSequence?): BytePacketBuilder
open override fun append(value: CharSequence?, startIndex: Int, endIndex: Int): BytePacketBuilder
fun append(csq: CharArray, start: Int, end: Int): Appendable
Link copied to clipboard
fun Output.append(csq: CharArray, start: Int = 0, end: Int = csq.size): Appendable
fun Output.append(csq: CharSequence, start: Int = 0, end: Int = csq.length): Appendable
Link copied to clipboard

Builds byte packet instance and resets builder's state to be able to build another one packet if needed

Link copied to clipboard
override fun close()

Should flush and close the destination

Link copied to clipboard
fun Output.fill(times: Long, value: Byte = 0)
Link copied to clipboard
fun flush()
Link copied to clipboard
inline fun <R> BytePacketBuilder.preview(block: (tmp: ByteReadPacket) -> R): R

Creates a temporary packet view of the packet being build without discarding any bytes from the builder. This is similar to build().copy() except that the builder keeps already written bytes untouched. A temporary view packet is passed as argument to block function and it shouldn't leak outside of this block otherwise an unexpected behaviour may occur.

Link copied to clipboard
fun release()

Release any resources that the builder holds. Builder shouldn't be used after release

Link copied to clipboard

Discard all written bytes and prepare to build another packet.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun writeByte(v: Byte)
Link copied to clipboard
fun Output.writeDouble(value: Double, byteOrder: ByteOrder)
Link copied to clipboard
Link copied to clipboard
fun Output.writeFloat(value: Float)
fun Output.writeFloat(value: Float, byteOrder: ByteOrder)
Link copied to clipboard
Link copied to clipboard
fun Output.writeFully(src: Buffer, length: Int = src.readRemaining)
fun Output.writeFully(src: Memory, offset: Int, length: Int)
fun Output.writeFully(src: Memory, offset: Long, length: Long)
fun Output.writeFully(src: ByteArray, offset: Int = 0, length: Int = src.size - offset)
fun Output.writeFully(src: DoubleArray, offset: Int = 0, length: Int = src.size - offset)
fun Output.writeFully(src: FloatArray, offset: Int = 0, length: Int = src.size - offset)
fun Output.writeFully(src: IntArray, offset: Int = 0, length: Int = src.size - offset)
fun Output.writeFully(src: LongArray, offset: Int = 0, length: Int = src.size - offset)
fun Output.writeFully(src: ShortArray, offset: Int = 0, length: Int = src.size - offset)
inline fun Output.writeFully(array: UByteArray, offset: Int = 0, length: Int = array.size - offset)
inline fun Output.writeFully(array: UIntArray, offset: Int = 0, length: Int = array.size - offset)
inline fun Output.writeFully(array: ULongArray, offset: Int = 0, length: Int = array.size - offset)
inline fun Output.writeFully(array: UShortArray, offset: Int = 0, length: Int = array.size - offset)
fun BytePacketBuilder.writeFully(src: ArrayBuffer, offset: Int = 0, length: Int = src.byteLength - offset)

Write exactly length bytes from the specified src array buffer

fun BytePacketBuilder.writeFully(src: Int8Array, offset: Int = 0, length: Int = src.length - offset)

Write exactly length bytes from the specified src typed array

Write all remaining src buffer bytes and change its position accordingly

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

Writes another packet to the end. Please note that the instance packet gets consumed so you don't need to release it

Write exact n bytes from packet to the builder

Link copied to clipboard
fun Output.writeShort(value: Short)
fun Output.writeShort(value: Short, byteOrder: ByteOrder)
Link copied to clipboard
Link copied to clipboard
fun Output.writeText(text: CharArray, fromIndex: Int = 0, toIndex: Int = text.size, charset: Charset = Charsets.UTF_8)
fun Output.writeText(text: CharSequence, fromIndex: Int = 0, toIndex: Int = text.length, charset: Charset = Charsets.UTF_8)

Writes text characters in range \[fromIndex .. toIndex) with the specified charset

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard