BytePacketBuilder

class BytePacketBuilder(pool: ObjectPool<ChunkBuffer>) : 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
fun BytePacketBuilder(pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool)

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 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
fun flush()
Link copied to clipboard
fun release()

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

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun writeByte(v: Byte)
Link copied to clipboard
fun writePacket(packet: ByteReadPacket)

Writes another packet to the end. Please note that the instance packet 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

Properties

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

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