BytePacketBuilder

class BytePacketBuilder(headerSizeHint: Int, pool: ObjectPool<ChunkBuffer>) : 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

Link copied to clipboard
fun BytePacketBuilder(headerSizeHint: Int = 0, pool: ObjectPool<ChunkBuffer>)

Functions

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

Append single UTF-8 character

open override fun append(csq: CharSequence?): BytePacketBuilder
open override fun append(csq: CharArray, start: Int, end: Int): Appendable
open override fun append(csq: CharSequence?, start: Int, end: Int): BytePacketBuilder
Link copied to clipboard
fun appendNewBuffer(): IoBuffer
Link copied to clipboard
@JvmName(name = "append")
fun appendOld(c: Char): BytePacketBuilderBase
@JvmName(name = "append")
fun appendOld(csq: CharSequence?): BytePacketBuilderBase
@JvmName(name = "append")
fun appendOld(csq: CharSequence?, start: Int, end: Int): BytePacketBuilderBase
Link copied to clipboard
fun build(): ByteReadPacket

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
expect open fun fill(n: Long, v: Byte)
Link copied to clipboard
override fun flush()
Link copied to clipboard
fun prepareWriteHead(n: Int): ChunkBuffer
Link copied to clipboard
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.

Link copied to clipboard
fun release()

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

Link copied to clipboard
override fun reset()

Discard all written bytes and prepare to build another packet.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
override fun writeByte(v: Byte)
Link copied to clipboard
expect open fun writeDouble(v: Double)
Link copied to clipboard
expect open fun writeFloat(v: Float)
Link copied to clipboard
expect open fun writeFully(src: IoBuffer, length: Int)
expect open fun writeFully(src: ByteArray, offset: Int, length: Int)
expect open fun writeFully(src: DoubleArray, offset: Int, length: Int)
expect open fun writeFully(src: FloatArray, offset: Int, length: Int)
expect open fun writeFully(src: IntArray, offset: Int, length: Int)
expect open fun writeFully(src: LongArray, offset: Int, length: Int)
expect open fun writeFully(src: ShortArray, offset: Int, length: Int)
Link copied to clipboard
expect open fun writeInt(v: Int)
Link copied to clipboard
expect open fun writeLong(v: Long)
Link copied to clipboard
fun writePacket(p: ByteReadPacket)

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

fun writePacket(p: ByteReadPacket, n: Int)
fun writePacket(p: ByteReadPacket, n: Long)

Write exact n bytes from packet to the builder

Link copied to clipboard
expect open fun writeShort(v: Short)
Link copied to clipboard
fun writeStringUtf8(cs: CharSequence)
fun writeStringUtf8(s: String)

Properties

Link copied to clipboard
override var byteOrder: ByteOrder

Byte order (Endianness) to be used by future write functions calls on this builder instance. Doesn't affect any previously written values.

Link copied to clipboard
val isEmpty: Boolean

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

Link copied to clipboard
val isNotEmpty: Boolean

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.

Extensions

Link copied to clipboard
fun BytePacketBuilder.outputStream(): OutputStream

Creates OutputStream adapter to the builder

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 BytePacketBuilder.reset()

Discard all written bytes and prepare to build another packet.

Link copied to clipboard
inline fun BytePacketBuilder.writeByteBufferDirect(size: Int, block: (ByteBuffer) -> Unit): Int

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

Link copied to clipboard
inline fun BytePacketBuilder.writeDirect(size: Int, block: (ByteBuffer) -> Unit)

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

Link copied to clipboard
fun BytePacketBuilder.writeFully(src: ByteBuffer)

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

fun BytePacketBuilder.writeFully(src: <ERROR CLASS><<ERROR CLASS>>, size: Int)

Write all remaining src buffer bytes and change its position accordingly

Link copied to clipboard
fun BytePacketBuilder.writerUTF8(): Writer

Creates Writer that encodes all characters in UTF-8 encoding