BytePacketBuilder

class BytePacketBuilder : BytePacketBuilderPlatformBase

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

<init>

BytePacketBuilder(
    headerSizeHint: Int = 0,
    pool: ObjectPool<ChunkBuffer>)

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.

Properties

isEmpty

val isEmpty: Boolean

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

isNotEmpty

val isNotEmpty: Boolean

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

size

val size: Int

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

Functions

append

fun append(c: Char): BytePacketBuilder

Append single UTF-8 character

fun append(csq: CharSequence?): BytePacketBuilder
fun append(
    csq: CharSequence?,
    start: Int,
    end: Int
): BytePacketBuilder

appendOld

fun appendOld(c: Char): BytePacketBuilderBase
fun appendOld(csq: CharSequence?): BytePacketBuilderBase
fun appendOld(
    csq: CharSequence?,
    start: Int,
    end: Int
): BytePacketBuilderBase

build

fun build(): ByteReadPacket

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

closeDestination

fun closeDestination(): Unit

Does nothing for memory-backed output

flush

fun flush(source: Memory, offset: Int, length: Int): Unit

Does nothing for memory-backed output

preview

fun <R> 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.

reset

fun reset(): Unit

Discard all written bytes and prepare to build another packet.

toString

fun toString(): String

Extension Functions

afterHeadWrite

fun Output.afterHeadWrite(current: IoBuffer): Unit
fun Output.afterHeadWrite(current: ChunkBuffer): Unit

append

fun Output.append(
    csq: CharSequence,
    start: Int = 0,
    end: Int = csq.length
): Appendable
fun Output.append(
    csq: CharArray,
    start: Int = 0,
    end: Int = csq.size
): Appendable

fill

fun Output.fill(times: Long, value: Byte = 0): Unit

outputStream

fun BytePacketBuilder.outputStream(): OutputStream

Creates OutputStream adapter to the builder

prepareWriteHead

fun Output.prepareWriteHead(
    capacity: Int,
    current: IoBuffer?
): IoBuffer
fun Output.prepareWriteHead(
    capacity: Int,
    current: ChunkBuffer?
): ChunkBuffer

preview

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.

reset

fun BytePacketBuilder.reset(): Unit

Discard all written bytes and prepare to build another packet.

writeDirect

fun BytePacketBuilder.writeDirect(
    size: Int,
    block: (ByteBuffer) -> Unit
): Unit

Write bytes directly to packet builder’s segment. Generally shouldn’t be used in user’s code and useful for efficient integration.

writeDouble

fun Output.writeDouble(
    value: Double,
    byteOrder: ByteOrder
): Unit
fun Output.writeDouble(value: Double): Unit

writeDoubleLittleEndian

fun Output.writeDoubleLittleEndian(value: Double): Unit

writeFloat

fun Output.writeFloat(
    value: Float,
    byteOrder: ByteOrder
): Unit
fun Output.writeFloat(value: Float): Unit

writeFloatLittleEndian

fun Output.writeFloatLittleEndian(value: Float): Unit

writeFully

fun BytePacketBuilder.writeFully(src: ByteBuffer): Unit

Write all src buffer remaining bytes and change it’s position accordingly

fun Output.writeFully(
    src: ByteArray,
    offset: Int = 0,
    length: Int = src.size - offset
): Unit
fun Output.writeFully(
    src: ShortArray,
    offset: Int = 0,
    length: Int = src.size - offset
): Unit
fun Output.writeFully(
    src: IntArray,
    offset: Int = 0,
    length: Int = src.size - offset
): Unit
fun Output.writeFully(
    src: LongArray,
    offset: Int = 0,
    length: Int = src.size - offset
): Unit
fun Output.writeFully(
    src: FloatArray,
    offset: Int = 0,
    length: Int = src.size - offset
): Unit
fun Output.writeFully(
    src: DoubleArray,
    offset: Int = 0,
    length: Int = src.size - offset
): Unit
fun Output.writeFully(
    src: IoBuffer,
    length: Int = src.readRemaining
): Unit
fun Output.writeFully(
    src: Buffer,
    length: Int = src.readRemaining
): Unit
fun Output.writeFully(
    src: Memory,
    offset: Int,
    length: Int
): Unit
fun Output.writeFully(
    src: Memory,
    offset: Long,
    length: Long
): Unit
fun Output.writeFully(
    array: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = array.size - offset
): Unit

writeFullyLittleEndian

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

writeInt

fun Output.writeInt(value: Int, byteOrder: ByteOrder): Unit
fun Output.writeInt(value: Int): Unit

writeIntLittleEndian

fun Output.writeIntLittleEndian(value: Int): Unit

writeLong

fun Output.writeLong(value: Long, byteOrder: ByteOrder): Unit
fun Output.writeLong(value: Long): Unit

writeLongLittleEndian

fun Output.writeLongLittleEndian(value: Long): Unit

writePacket

fun Output.writePacket(packet: ByteReadPacket): Unit

writeShort

fun Output.writeShort(
    value: Short,
    byteOrder: ByteOrder
): Unit
fun Output.writeShort(value: Short): Unit

writeShortLittleEndian

fun Output.writeShortLittleEndian(value: Short): Unit

writeText

fun Output.writeText(
    text: CharSequence,
    fromIndex: Int = 0,
    toIndex: Int = text.length,
    encoder: CharsetEncoder
): Unit

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

fun Output.writeText(
    text: CharSequence,
    fromIndex: Int = 0,
    toIndex: Int = text.length,
    charset: Charset = Charsets.UTF_8
): Unit
fun Output.writeText(
    text: CharArray,
    fromIndex: Int = 0,
    toIndex: Int = text.size,
    charset: Charset = Charsets.UTF_8
): Unit

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

writeUByte

fun Output.writeUByte(v: <ERROR CLASS>): Unit

writeUInt

fun Output.writeUInt(v: <ERROR CLASS>): Unit

writeULong

fun Output.writeULong(v: <ERROR CLASS>): Unit

writeUShort

fun Output.writeUShort(v: <ERROR CLASS>): Unit

writeWhile

fun Output.writeWhile(block: (Buffer) -> Boolean): Unit

Append number of chunks invoking block function while the returned value is true. Depending on the output underlying implementation it could invoke block function with the same buffer several times however it is guaranteed that it is always non-empty.

writeWhileSize

fun Output.writeWhileSize(
    initialSize: Int = 1,
    block: (Buffer) -> Int
): Unit

Append number of chunks invoking block function while the returned value is positive. If returned value is positive then it will be invoked again with a buffer having at least requested number of bytes space (could be the same buffer as before if it complies to the restriction).

writerUTF8

fun BytePacketBuilder.writerUTF8(): Writer

Creates Writer that encodes all characters in UTF-8 encoding