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
BytePacketBuilder( 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
val isEmpty: Boolean If no bytes were written or the builder has been reset. |
|
val isNotEmpty: Boolean If at least one byte was written after the creation or the last reset. |
|
val size: Int Number of bytes written to the builder after the creation or the last reset. |
Functions
fun append(c: Char): BytePacketBuilder Append single UTF-8 character fun append(csq: CharSequence?): BytePacketBuilder fun append( |
|
fun fun fun |
|
fun build(): ByteReadPacket Builds byte packet instance and resets builder’s state to be able to build another one packet if needed |
|
fun closeDestination(): Unit Does nothing for memory-backed output |
|
Does nothing for memory-backed output |
|
fun <R> Creates a temporary packet view of the packet being build without discarding any bytes from the builder.
This is similar to |
|
fun Discard all written bytes and prepare to build another packet. |
|
fun toString(): String |
Extension Functions
fun Output.afterHeadWrite(current: ChunkBuffer): Unit |
|
fun Output.append( fun Output.append( |
|
fun BytePacketBuilder.outputStream(): OutputStream Creates OutputStream adapter to the builder |
|
fun Output.prepareWriteHead( |
|
fun <R> BytePacketBuilder.preview( Creates a temporary packet view of the packet being build without discarding any bytes from the builder.
This is similar to |
|
fun BytePacketBuilder.reset(): Unit Discard all written bytes and prepare to build another packet. |
|
fun BytePacketBuilder. Write bytes directly to packet builder’s segment. Generally shouldn’t be used in user’s code and useful for efficient integration. |
|
fun BytePacketBuilder. Write all src buffer remaining bytes and change it’s position accordingly fun Output.writeFully( fun Output.writeFully( fun Output.writeFully( |
|
fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( |
|
fun Output.writePacket(packet: ByteReadPacket): Unit |
|
fun Output. Writes text characters in range [fromIndex .. toIndex) with the specified encoder fun Output.writeText( fun Output.writeText( Writes text characters in range [fromIndex .. toIndex) with the specified charset |
|
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. |
|
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). |
|
fun BytePacketBuilder.writerUTF8(): Writer Creates Writer that encodes all characters in UTF-8 encoding |