Package io.ktor.utils.io.core

Types

AbstractInput

abstract class AbstractInput : Input

The default abstract base class implementing Input interface.

AbstractOutput

abstract class AbstractOutput : Appendable, Output

The default Output implementation.

Buffer

open class Buffer

Represents a buffer with read and write positions.

ByteOrder

enum expect class ByteOrder

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.

BytePacketBuilderBase

abstract class BytePacketBuilderBase : AbstractOutput

BytePacketBuilderPlatformBase

abstract class BytePacketBuilderPlatformBase : 
    BytePacketBuilderBase

ByteReadPacket

class ByteReadPacket : ByteReadPacketPlatformBase, Input

Read-only immutable byte packet. Could be consumed only once however it does support copy that doesn’t copy every byte but creates a new view instead. Once packet created it should be either completely read (consumed) or released via release.

ByteReadPacketBase

abstract class ByteReadPacketBase : AbstractInput

ByteReadPacketPlatformBase

abstract class ByteReadPacketPlatformBase : 
    ByteReadPacketBase

Closeable

expect interface Closeable

Input

expect interface Input : Closeable

Usually shouldn’t be implemented directly. Inherit AbstractInput instead.

IoBuffer

expect class IoBuffer : Input, Output, ChunkBuffer

A read-write facade to actual buffer of fixed size. Multiple views could share the same actual buffer. Concurrent unsafe. The only concurrent-safe operation is release. In most cases ByteReadPacket and BytePacketBuilder should be used instead.

Output

expect interface Output : Appendable, Closeable

This shouldn’t be implemented directly. Inherit AbstractOutput instead.

Annotations

ExperimentalIoApi

annotation class ExperimentalIoApi

API marked with this annotation is experimental and could be changed

Exceptions

BufferLimitExceededException

class BufferLimitExceededException : Exception

EOFException

expect class EOFException : IOException

InsufficientSpaceException

class InsufficientSpaceException : Exception

Extensions for External Classes

kotlin.String

Properties

PACKET_MAX_COPY_SIZE

expect val PACKET_MAX_COPY_SIZE: Int

byteOrder

var Buffer.byteOrder: ByteOrder

isEmpty

val Input.isEmpty: Boolean

For streaming input it should be Input.endOfInput instead.

val ByteReadPacket.isEmpty: Boolean

isNotEmpty

val Input.isNotEmpty: Boolean

For streaming input there is no reliable way to detect it without triggering bytes population from the underlying source. Consider using Input.endOfInput or use ByteReadPacket instead.

val ByteReadPacket.isNotEmpty: Boolean

Functions

BytePacketBuilder

expect fun BytePacketBuilder(
    headerSizeHint: Int = 0
): BytePacketBuilder

ByteReadPacket

expect fun ByteReadPacket(
    array: ByteArray,
    offset: Int = 0,
    length: Int = array.size,
    block: (ByteArray) -> Unit
): ByteReadPacket
fun ByteReadPacket(
    array: ByteArray,
    offset: Int = 0,
    length: Int = array.size
): ByteReadPacket
fun ByteReadPacket(
    bb: ByteBuffer,
    release: (ByteBuffer) -> Unit = {}
): ByteReadPacket

String

expect fun String(
    bytes: ByteArray,
    offset: Int = 0,
    length: Int = bytes.size,
    charset: Charset = Charsets.UTF_8
): String

Create an instance of String from the specified bytes range starting at offset and bytes length interpreting characters in the specified charset.

append

fun Buffer.append(c: Char): Buffer
fun Buffer.append(csq: CharSequence?): Buffer
fun Buffer.append(
    csq: CharSequence?,
    start: Int,
    end: Int
): Buffer
fun Buffer.append(
    csq: CharArray,
    start: Int,
    end: Int
): Buffer
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

buildPacket

fun buildPacket(
    headerSizeHint: Int = 0,
    block: BytePacketBuilder.() -> Unit
): ByteReadPacket

Build a byte packet in block lambda. Creates a temporary builder and releases it in case of failure

canRead

fun Buffer.canRead(): Boolean

canWrite

fun Buffer.canWrite(): Boolean

copyTo

fun Input.copyTo(output: Output): Long

Copy all bytes to the output. Depending on actual input and output implementation it could be zero-copy or copy byte per byte. All regular types such as ByteReadPacket, BytePacketBuilder, AbstractInput and AbstractOutput are always optimized so no bytes will be copied.

discard

fun Input.discard(): Long

Discard all remaining bytes.

discardExact

fun Input.discardExact(n: Long): Unit
fun Input.discardExact(n: Int): Unit

Discard exactly n bytes or fail if not enough bytes in the input to be discarded.

discardUntilDelimiter

fun Input.discardUntilDelimiter(delimiter: Byte): Long

Discards bytes until delimiter occurred

discardUntilDelimiters

fun Input.discardUntilDelimiters(
    delimiter1: Byte,
    delimiter2: Byte
): Long

Discards bytes until of of the specified delimiters delimiter1 or delimiter2 occurred

fill

fun Buffer.fill(times: Int, value: Byte): Unit

Write byte value repeated the specified times.

fun Buffer.fill(times: Int, value: <ERROR CLASS>): Unit

Write unsigned byte value repeated the specified times.

fun Buffer.fill(n: Long, v: Byte): Unit

Write byte v value repeated n times.

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

flush

fun Buffer.flush(): Unit

forEach

fun Buffer.forEach(block: (Byte) -> Unit): Unit

For every byte from this buffer invokes block function giving it as parameter.

fun Input.forEach(block: (Byte) -> Unit): Unit

For every byte from this input invokes block function giving it as parameter.

makeView

fun Buffer.makeView(): Buffer
fun ChunkBuffer.makeView(): ChunkBuffer

peekCharUtf8

fun Input.peekCharUtf8(): Char

peekTo

fun Input.peekTo(
    destination: IoBuffer,
    offset: Int = 0,
    min: Int = 1,
    max: Int = Int.MAX_VALUE
): Int
fun Input.peekTo(
    destination: Buffer,
    offset: Int = 0,
    min: Int = 1,
    max: Int = Int.MAX_VALUE
): Int

Copy at least min but up to max bytes to the specified destination buffer from this input skipping offset bytes. If there are not enough bytes available to provide min bytes then it simply return number of available bytes with no exception so the returned value need to be checked. It is safe to specify max > destination.writeRemaining but min shouldn’t be bigger than the destination free space. This function could trigger the underlying source reading that may lead to blocking I/O. It is safe to specify too big offset so in this case this function will always return 0. This function usually copy more bytes than min (unless max = min) but it is not guaranteed. When 0 is returned with offset = 0 then it makes sense to check Input.endOfInput.

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.

pushBack

fun Buffer.pushBack(n: Int): Unit

Push back n bytes: only possible if there were at least n bytes read before this operation.

read

fun Buffer.read(
    block: (memory: Memory, start: Int, endExclusive: Int) -> Int
): Int

Apply block of code with buffer’s memory providing read range indices. The returned value of block lambda should return number of bytes to be marked as consumed. No read/write functions on this buffer should be called inside of block otherwise an undefined behaviour may occur including data damage.

readAvailable

fun Buffer.readAvailable(
    destination: ByteArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int

Read available for read bytes to the destination array range starting at array offset and length bytes. If less than length bytes available then less bytes will be copied and the corresponding number will be returned as result.

fun IoBuffer.readAvailable(
    destination: ByteArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Input.readAvailable(
    dst: IoBuffer,
    size: Int = dst.writeRemaining
): Int
fun Input.readAvailable(
    dst: ByteArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailable(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailable(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailable(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailable(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailable(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailable(
    dst: Buffer,
    length: Int = dst.writeRemaining
): Int
fun Input.readAvailable(
    destination: Memory,
    destinationOffset: Int,
    length: Int
): Int
fun Input.readAvailable(
    destination: Memory,
    destinationOffset: Long,
    length: Long
): Long
fun Input.readAvailable(
    dst: ByteBuffer,
    length: Int = dst.remaining()
): Int
fun Buffer.readAvailable(
    dst: ByteBuffer,
    length: Int = dst.remaining()
): Int
fun Buffer.readAvailable(
    destination: ShortArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: IntArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: LongArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: FloatArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int
fun Buffer.readAvailable(
    destination: DoubleArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Int

Read available for read bytes to the destination array range starting at array offset and length elements. If less than length elements available then less elements will be copied and the corresponding number will be returned as result (possibly zero).

fun Buffer.readAvailable(
    dst: Buffer,
    length: Int = dst.writeRemaining
): Int

Read at most length available bytes to the dst buffer or -1 if no bytes available for read.

fun ByteReadPacket.readAvailable(dst: ByteBuffer): Int

Read at most dst.remaining() bytes to the specified dst byte buffer and change it’s position accordingly

readAvailableLittleEndian

fun Input.readAvailableLittleEndian(
    dst: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableLittleEndian(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableLittleEndian(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableLittleEndian(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableLittleEndian(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableLittleEndian(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Buffer.readAvailableLittleEndian(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int

readAvailableOld

fun Input.readAvailableOld(
    dst: ByteArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableOld(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableOld(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableOld(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableOld(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int
fun Input.readAvailableOld(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Int

readByteBuffer

fun ByteReadPacket.readByteBuffer(
    n: Int = remaining.coerceAtMostMaxIntOrFail("Unable to make a ByteBuffer: packet is too big"),
    direct: Boolean = false
): ByteBuffer

Read exactly n (optional, read all remaining by default) bytes to a newly allocated byte buffer

readBytes

fun Buffer.readBytes(count: Int = readRemaining): ByteArray

Read the specified number of bytes specified (optional, read all remaining by default)

fun ByteReadPacket.readBytes(
    n: Int = remaining.coerceAtMostMaxIntOrFail("Unable to convert to a ByteArray: packet is too big")
): ByteArray

Read exactly n bytes (consumes all remaining if n is not specified but up to Int.MAX_VALUE bytes). Does fail if not enough bytes remaining.

fun Input.readBytes(n: Int): ByteArray

Reads exactly n bytes from the input or fails if not enough bytes available.

fun Input.readBytes(): ByteArray

Reads all remaining bytes from the input

readBytesOf

fun Input.readBytesOf(
    min: Int = 0,
    max: Int = Int.MAX_VALUE
): ByteArray

Reads at least min but no more than max bytes from the input to a new byte array

readDirect

fun ByteReadPacket.readDirect(
    size: Int,
    block: (ByteBuffer) -> Unit
): Unit
fun AbstractInput.readDirect(
    size: Int,
    block: (ByteBuffer) -> Unit
): Unit
fun ByteReadPacketBase.readDirect(
    size: Int,
    block: (ByteBuffer) -> Unit
): Unit
fun Buffer.readDirect(block: (ByteBuffer) -> Unit): Int

readDouble

fun Buffer.readDouble(): Double

Read a floating point number or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readDouble(): Double
fun Input.readDouble(byteOrder: ByteOrder): Double
fun Input.readDouble(): Double

readDoubleFallback

fun Input.readDoubleFallback(): Double

readDoubleLittleEndian

fun Input.readDoubleLittleEndian(): Double
fun Buffer.readDoubleLittleEndian(): Double

readFloat

fun Buffer.readFloat(): Float

Read a floating point number or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readFloat(): Float
fun Input.readFloat(byteOrder: ByteOrder): Float
fun Input.readFloat(): Float

readFloatFallback

fun Input.readFloatFallback(): Float

readFloatLittleEndian

fun Input.readFloatLittleEndian(): Float
fun Buffer.readFloatLittleEndian(): Float

readFully

fun Buffer.readFully(
    dst: Array<Byte>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun IoBuffer.readFully(
    destination: ByteArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Input.readFully(
    dst: IoBuffer,
    size: Int = dst.writeRemaining
): Unit
fun Input.readFully(
    dst: ByteArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: Buffer,
    length: Int = dst.writeRemaining
): Unit
fun Input.readFully(
    destination: Memory,
    destinationOffset: Int,
    length: Int
): Unit
fun Input.readFully(
    destination: Memory,
    destinationOffset: Long,
    length: Long
): Unit
fun Input.readFully(
    dst: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFully(
    dst: ByteBuffer,
    length: Int = dst.remaining()
): Unit
fun Buffer.readFully(dst: ByteBuffer, length: Int): Unit
fun Buffer.readFully(
    destination: ByteArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Buffer.readFully(
    destination: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit

Read from this buffer to the destination array to offset and length bytes.

fun Buffer.readFully(
    destination: ShortArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Buffer.readFully(
    destination: IntArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Buffer.readFully(
    destination: LongArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Buffer.readFully(
    destination: FloatArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit
fun Buffer.readFully(
    destination: DoubleArray,
    offset: Int = 0,
    length: Int = destination.size - offset
): Unit

Read from this buffer to the destination array to offset and length bytes. Numeric values are interpreted in the network byte order (Big Endian).

fun Buffer.readFully(
    dst: Buffer,
    length: Int = dst.writeRemaining
): Int

Read at most length bytes from this buffer to the dst buffer.

fun Buffer.readFully(destination: ByteBuffer): Unit

Read buffer’s content to the destination buffer moving it’s position.

fun ByteReadPacket.readFully(dst: ByteBuffer): Int

Read exactly dst.remaining() bytes to the specified dst byte buffer and change it’s position accordingly

readFullyLittleEndian

fun Input.readFullyLittleEndian(
    dst: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyLittleEndian(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyLittleEndian(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyLittleEndian(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyLittleEndian(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyLittleEndian(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Buffer.readFullyLittleEndian(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit

readFullyOld

fun Input.readFullyOld(
    dst: ByteArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyOld(
    dst: ShortArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyOld(
    dst: IntArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyOld(
    dst: LongArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyOld(
    dst: FloatArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit
fun Input.readFullyOld(
    dst: DoubleArray,
    offset: Int = 0,
    length: Int = dst.size - offset
): Unit

readInt

fun Buffer.readInt(): Int

Read an integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readInt(): Int
fun Input.readInt(byteOrder: ByteOrder): Int
fun Input.readInt(): Int

readIntLittleEndian

fun Input.readIntLittleEndian(): Int
fun Buffer.readIntLittleEndian(): Int

readLong

fun Buffer.readLong(): Long

Read a long integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readLong(): Long
fun Input.readLong(byteOrder: ByteOrder): Long
fun Input.readLong(): Long

readLongLittleEndian

fun Input.readLongLittleEndian(): Long
fun Buffer.readLongLittleEndian(): Long

readShort

fun Buffer.readShort(): Short

Read a short integer or fail if no bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readShort(): Short
fun Input.readShort(byteOrder: ByteOrder): Short
fun Input.readShort(): Short

readShortLittleEndian

fun Input.readShortLittleEndian(): Short
fun Buffer.readShortLittleEndian(): Short

readText

fun Buffer.readText(
    decoder: CharsetDecoder,
    out: Appendable,
    lastBuffer: Boolean,
    max: Int = Int.MAX_VALUE
): Int
fun ByteReadPacket.readText(
    decoder: CharsetDecoder,
    out: Appendable,
    max: Int = Int.MAX_VALUE
): Int
fun Input.readText(
    out: Appendable,
    decoder: CharsetDecoder,
    max: Int = Int.MAX_VALUE
): Int
fun Input.readText(
    decoder: CharsetDecoder,
    max: Int = Int.MAX_VALUE
): String

Reads at most max characters decoding bytes with specified decoder. Extra character bytes will remain unconsumed

fun Input.readText(
    out: Appendable,
    charset: Charset = Charsets.UTF_8,
    max: Int = Int.MAX_VALUE
): Int
fun Input.readText(
    charset: Charset = Charsets.UTF_8,
    max: Int = Int.MAX_VALUE
): String
fun Buffer.readText(
    charset: Charset = Charsets.UTF_8,
    max: Int = Int.MAX_VALUE
): String

Reads at most max characters decoding bytes with specified charset. Extra character bytes will remain unconsumed

readTextExact

fun Input.readTextExact(
    charset: Charset = Charsets.UTF_8,
    n: Int
): String

Read exactly n characters interpreting bytes in the specified charset.

readTextExactBytes

fun Input.readTextExactBytes(
    charset: Charset = Charsets.UTF_8,
    bytes: Int
): String

Read exactly the specified number of bytes interpreting bytes in the specified charset (optional, UTF-8 by default).

fun Input.readTextExactBytes(
    bytesCount: Int,
    charset: Charset = Charsets.UTF_8
): String

Read exactly bytesCount interpreting bytes in the specified charset (optional, UTF-8 by default).

readTextExactCharacters

fun Input.readTextExactCharacters(
    charactersCount: Int,
    charset: Charset = Charsets.UTF_8
): String

Read exactly charactersCount characters interpreting bytes in the specified charset.

readUByte

fun Buffer.readUByte(): <ERROR CLASS>

Read an unsigned byte or fail if no bytes available for reading.

fun IoBuffer.readUByte(): <ERROR CLASS>
fun Input.readUByte(): <ERROR CLASS>

readUInt

fun Buffer.readUInt(): <ERROR CLASS>

Read an unsigned integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readUInt(): <ERROR CLASS>
fun Input.readUInt(): <ERROR CLASS>

readULong

fun Buffer.readULong(): <ERROR CLASS>

Read an unsigned long integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readULong(): <ERROR CLASS>
fun Input.readULong(): <ERROR CLASS>

readUShort

fun Buffer.readUShort(): <ERROR CLASS>

Read an unsigned short integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian).

fun IoBuffer.readUShort(): <ERROR CLASS>
fun Input.readUShort(): <ERROR CLASS>

readUTF8Line

fun ByteReadPacket.readUTF8Line(
    estimate: Int = 16,
    limit: Int = Int.MAX_VALUE
): String?
fun Input.readUTF8Line(
    estimate: Int = 16,
    limit: Int = Int.MAX_VALUE
): String?

Read a string line considering optionally specified estimate but up to optional limit characters length (does fail once limit exceeded) or return null if the packet is empty

readUTF8LineTo

fun Input.readUTF8LineTo(
    out: Appendable,
    limit: Int
): Boolean

Read UTF-8 line and append all line characters to out except line endings. Does support CR, LF and CR+LF

readUTF8UntilDelimiter

fun Input.readUTF8UntilDelimiter(
    delimiters: String,
    limit: Int = Int.MAX_VALUE
): String

Reads UTF-8 characters until one of the specified delimiters found, limit exceeded or end of stream encountered

readUTF8UntilDelimiterTo

fun Input.readUTF8UntilDelimiterTo(
    out: Appendable,
    delimiters: String,
    limit: Int = Int.MAX_VALUE
): Int
fun Input.readUTF8UntilDelimiterTo(
    out: Output,
    delimiters: String,
    limit: Int = Int.MAX_VALUE
): Int

Reads UTF-8 characters to out buffer until one of the specified delimiters found, limit exceeded or end of stream encountered

fun Input.readUTF8UntilDelimiterTo(
    out: BytePacketBuilderBase,
    delimiters: String,
    limit: Int = Int.MAX_VALUE
): Int

readUntilDelimiter

fun Input.readUntilDelimiter(
    delimiter: Byte,
    dst: ByteArray,
    offset: Int = 0,
    length: Int = dst.size
): Int

Copies to dst array at offset at most length bytes or until the specified delimiter occurred.

fun Input.readUntilDelimiter(
    delimiter: Byte,
    dst: Output
): Long

Copies to dst output until the specified delimiter occurred.

readUntilDelimiters

fun Input.readUntilDelimiters(
    delimiter1: Byte,
    delimiter2: Byte,
    dst: ByteArray,
    offset: Int = 0,
    length: Int = dst.size
): Int

Copies to dst array at offset at most length bytes or until one of the specified delimiters delimiter1 or delimiter2 occurred.

fun Input.readUntilDelimiters(
    delimiter1: Byte,
    delimiter2: Byte,
    dst: Output
): Long

Copies to dst output until one of the specified delimiters delimiter1 or delimiter2 occurred.

release

fun IoBuffer.release(pool: ObjectPool<IoBuffer>): Unit

releases buffer view and returns it to the pool if there are no more usages. Based on simple ref-counting so it is very fragile.

remainingAll

fun ChunkBuffer.remainingAll(): Long

Summarize remainings of all elements of the chain

fun remainingAll(buffer: IoBuffer): Long

reset

fun BytePacketBuilder.reset(): Unit

Discard all written bytes and prepare to build another packet.

takeWhile

fun Input.takeWhile(block: (Buffer) -> Boolean): Unit

Invoke block function for every chunk until end of input or block function return false block function returns true to request more chunks or false to stop loop

takeWhileSize

fun Input.takeWhileSize(
    initialSize: Int = 1,
    block: (Buffer) -> Int
): Unit

Invoke block function for every chunk until end of input or block function return zero block function returns number of bytes required to read next primitive and shouldn’t require too many bytes at once otherwise it could fail with an exception. It is not guaranteed that every chunk will have fixed size but it will be always at least requested bytes length. block function should never release provided buffer and should not write to it otherwise an undefined behaviour could be observed

tryPeek

fun Buffer.tryPeek(): Int

Peek the next unsigned byte or return -1 if no more bytes available for reading. No bytes will be marked as consumed in any case.

use

fun <C, R> C.use(block: (C) -> R): R
fun <I : Input, R> I.use(block: (I) -> R): R
fun <O : Output, R> O.use(block: (O) -> R): R

withBuffer

fun <R> withBuffer(size: Int, block: Buffer.() -> R): R

Invoke block function with a temporary Buffer instance of the specified size in bytes. The provided instance shouldn’t be captured and used outside of the block otherwise an undefined behaviour may occur including crash and/or data corruption.

fun <R> withBuffer(
    pool: ObjectPool<Buffer>,
    block: Buffer.() -> R
): R

Invoke block function with a temporary Buffer instance taken from the specified pool. Depending on the pool it may be safe or unsafe to capture and use the provided buffer outside of the block. Usually it is always recommended to NOT capture an instance outside.

write

fun Buffer.write(
    block: (memory: Memory, start: Int, endExclusive: Int) -> Int
): Int

Apply block of code with buffer’s memory providing write range indices. The returned value of block lambda should return number of bytes were written. o read/write functions on this buffer should be called inside of block otherwise an undefined behaviour may occur including data damage.

writeByteBufferDirect

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.

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.

fun Buffer.writeDirect(
    size: Int = 1,
    block: (ByteBuffer) -> Unit
): Int

writeDouble

fun Buffer.writeDouble(value: Double): Unit

Write a floating point number or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeDoubleLittleEndian

fun Output.writeDoubleLittleEndian(value: Double): Unit
fun Buffer.writeDoubleLittleEndian(value: Double): Unit

writeFloat

fun Buffer.writeFloat(value: Float): Unit

Write a floating point number or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeFloatLittleEndian

fun Output.writeFloatLittleEndian(value: Float): Unit
fun Buffer.writeFloatLittleEndian(value: Float): Unit

writeFully

fun Buffer.writeFully(
    source: ByteArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFully(
    source: <ERROR CLASS>,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit

Write the whole source array range staring at offset and having the specified bytes length.

fun IoBuffer.writeFully(
    source: ByteArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
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
fun Output.writeFully(bb: ByteBuffer): Unit
fun Buffer.writeFully(
    source: ShortArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFully(
    source: IntArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFully(
    source: LongArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFully(
    source: FloatArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit
fun Buffer.writeFully(
    source: DoubleArray,
    offset: Int = 0,
    length: Int = source.size - offset
): Unit

Write the whole source array range staring at offset and having the specified items length. Numeric values are interpreted in the network byte order (Big Endian).

fun Buffer.writeFully(src: Buffer): Unit

Write all readable bytes from src to this buffer. Fails if not enough space available to write all bytes.

fun Buffer.writeFully(src: Buffer, length: Int): Unit

Write at most length readable bytes from src to this buffer. Fails if not enough space available to write all bytes.

fun Buffer.writeFully(source: ByteBuffer): Unit

Write source buffer content moving it’s position.

fun BytePacketBuilder.writeFully(src: ByteBuffer): Unit

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

writeFullyLittleEndian

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

writeInt

fun Buffer.writeInt(value: Int): Unit

Write an integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeIntLittleEndian

fun Output.writeIntLittleEndian(value: Int): Unit
fun Buffer.writeIntLittleEndian(value: Int): Unit

writeLong

fun Buffer.writeLong(value: Long): Unit

Write a long integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeLongLittleEndian

fun Output.writeLongLittleEndian(value: Long): Unit
fun Buffer.writeLongLittleEndian(value: Long): Unit

writePacket

fun Output.writePacket(packet: ByteReadPacket): Unit

writeShort

fun Buffer.writeShort(value: Short): Unit

Write a short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeShortLittleEndian

fun Output.writeShortLittleEndian(value: Short): Unit
fun Buffer.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 Buffer.writeUByte(value: <ERROR CLASS>): Unit

Write an unsigned byte or fail if not enough space available for writing.

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

writeUInt

fun Buffer.writeUInt(value: <ERROR CLASS>): Unit

Write an unsigned integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeULong

fun Buffer.writeULong(value: <ERROR CLASS>): Unit

Write an unsigned long integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

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

writeUShort

fun Buffer.writeUShort(value: <ERROR CLASS>): Unit

Write an unsigned short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian).

fun IoBuffer.writeUShort(value: <ERROR CLASS>): Unit
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).