Input

abstract class Input(head: ChunkBuffer = ChunkBuffer.Empty, remaining: Long = head.remainingAll(), val pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool) : Closeable

Deprecated

We're migrating to the new kotlinx-io library. This declaration is deprecated and will be removed in Ktor 4.0.0 If you have any problems with migration, please contact us in https://youtrack.jetbrains.com/issue/KTOR-6030/Migrate-to-new-kotlinx.io-library

Usually shouldn't be implemented directly. Inherit Input instead.

Inheritors

Constructors

Link copied to clipboard
constructor(head: ChunkBuffer = ChunkBuffer.Empty, remaining: Long = head.remainingAll(), pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

It is true when it is known that no more bytes will be available. When it is false then this means that it is not known yet or there are available bytes. Please note that false value doesn't guarantee that there are available bytes so readByte() may fail.

Link copied to clipboard

For streaming input it should be Input.endOfInput instead.

Link copied to clipboard

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.

Link copied to clipboard
Link copied to clipboard

Number of bytes available for read

Functions

Link copied to clipboard
Link copied to clipboard
open override fun close()

Close input including the underlying source. All pending bytes will be discarded. It is not recommended to invoke it with read operations in-progress concurrently.

Link copied to clipboard
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, Input and Output are always optimized so no bytes will be copied.

Link copied to clipboard
fun discard(n: Int): Int

Discards at most n bytes

fun discard(n: Long): Long

Discard at most n bytes

Link copied to clipboard

Discard all remaining bytes.

Link copied to clipboard

Discards exactly n bytes or fails with EOFException

Link copied to clipboard

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

Link copied to clipboard

Discards bytes until delimiter occurred

Link copied to clipboard
fun Input.discardUntilDelimiters(delimiter1: Byte, delimiter2: Byte): Long

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

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun peekTo(buffer: ChunkBuffer): Int

fun peekTo(destination: Memory, destinationOffset: Long, offset: Long = 0, min: Long = 1, max: Long = Long.MAX_VALUE): Long

Try to 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 after skipping offset bytes then it will trigger the underlying source reading first and after that will simply copy available bytes even if EOF encountered so min is not a requirement but a desired number of bytes. It is safe to specify max greater than the destination free space. 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 allowed to specify too big offset so in this case this function will always return 0 after prefetching all underlying bytes but note that it may lead to significant memory consumption. 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 endOfInput.

Link copied to clipboard
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: ByteArray, 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: FloatArray, 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: ShortArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Input.readAvailable(dst: ArrayBuffer, offset: Int = 0, length: Int = dst.byteLength - offset): Int
fun Input.readAvailable(dst: ArrayBufferView, byteOffset: Int = 0, byteLength: Int = dst.byteLength - byteOffset): Int
fun Input.readAvailable(dst: Int8Array, offset: Int = 0, length: Int = dst.length - offset): Int
fun Input.readAvailable(dst: ByteBuffer, length: Int = dst.remaining()): Int
fun Input.readAvailable(dst: CPointer<ByteVar>, offset: Int, length: Int): Int
fun Input.readAvailable(dst: CPointer<ByteVar>, offset: Long, length: Long): Long
Link copied to clipboard
fun Input.readAvailableLittleEndian(dst: DoubleArray, 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: 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: ShortArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Input.readAvailableLittleEndian(dst: UIntArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Input.readAvailableLittleEndian(dst: ULongArray, offset: Int = 0, length: Int = dst.size - offset): Int
fun Input.readAvailableLittleEndian(dst: UShortArray, offset: Int = 0, length: Int = dst.size - offset): Int
Link copied to clipboard
fun readByte(): Byte

Read the next upcoming byte

Link copied to clipboard

Reads all remaining bytes from the input

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

Link copied to clipboard
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

Link copied to clipboard
inline fun Input.readDirect(size: Int, block: (ByteBuffer) -> Unit)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun Input.readFully(dst: Buffer, length: Int = dst.writeRemaining)
fun Input.readFully(destination: Memory, destinationOffset: Int, length: Int)
fun Input.readFully(destination: Memory, destinationOffset: Long, length: Long)
fun Input.readFully(dst: ByteArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFully(dst: DoubleArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFully(dst: FloatArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFully(dst: IntArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFully(dst: LongArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFully(dst: ShortArray, offset: Int = 0, length: Int = dst.size - offset)
inline fun Input.readFully(dst: UByteArray, offset: Int = 0, length: Int = dst.size - offset)
inline fun Input.readFully(dst: UIntArray, offset: Int = 0, length: Int = dst.size - offset)
inline fun Input.readFully(dst: ULongArray, offset: Int = 0, length: Int = dst.size - offset)
inline fun Input.readFully(dst: UShortArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFully(dst: ArrayBuffer, offset: Int = 0, length: Int = dst.byteLength - offset)
fun Input.readFully(dst: ArrayBufferView, byteOffset: Int = 0, byteLength: Int = dst.byteLength - byteOffset)
fun Input.readFully(dst: Int8Array, offset: Int = 0, length: Int = dst.length - offset)
fun Input.readFully(dst: ByteBuffer, length: Int = dst.remaining())
fun Input.readFully(dst: CPointer<ByteVar>, offset: Int, length: Int)
fun Input.readFully(dst: CPointer<ByteVar>, offset: Long, length: Long)
Link copied to clipboard
fun Input.readFullyLittleEndian(dst: DoubleArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: FloatArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: IntArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: LongArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: ShortArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: UIntArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: ULongArray, offset: Int = 0, length: Int = dst.size - offset)
fun Input.readFullyLittleEndian(dst: UShortArray, offset: Int = 0, length: Int = dst.size - offset)
Link copied to clipboard
fun Input.readInt(byteOrder: ByteOrder): Int
Link copied to clipboard
Link copied to clipboard
fun Input.readLong(byteOrder: ByteOrder): Long
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun readText(min: Int = 0, max: Int = Int.MAX_VALUE): String

Read a string at last min and at most max characters length

fun readText(out: Appendable, min: Int = 0, max: Int = Int.MAX_VALUE): Int

Read at least min and at most max characters and append them to out

Link copied to clipboard
fun Input.readText(charset: Charset = Charsets.UTF_8, max: Int = Int.MAX_VALUE): String
fun Input.readText(out: Appendable, charset: Charset = Charsets.UTF_8, max: Int = Int.MAX_VALUE): Int

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

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

Link copied to clipboard
fun readTextExact(exactCharacters: Int): String

Read a string exactly exactCharacters length

fun readTextExact(out: Appendable, exactCharacters: Int)

Read exactly exactCharacters characters and append them to out

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

Read exactly n characters interpreting bytes in the specified charset.

Link copied to clipboard
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).

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

Read exactly charactersCount characters interpreting bytes in the specified charset.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun Input.readUntilDelimiter(delimiter: Byte, dst: Output): Long

Copies to dst output until the specified delimiter occurred.

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.

Link copied to clipboard
fun Input.readUntilDelimiters(delimiter1: Byte, delimiter2: Byte, dst: Output): Long

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

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.

Link copied to clipboard
Link copied to clipboard
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

Link copied to clipboard

Reads UTF-8 line and append all line characters to out except line endings. Supports CR, LF and CR+LF

Link copied to clipboard
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

Link copied to clipboard
fun Input.readUTF8UntilDelimiterTo(out: Output, delimiters: String, limit: Int = Int.MAX_VALUE): Int
fun Input.readUTF8UntilDelimiterTo(out: Appendable, 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

Link copied to clipboard
fun release()

Release packet. After this function invocation the packet becomes empty. If it has been copied via ByteReadPacket.copy then the copy should be released as well.

Link copied to clipboard
inline fun Input.takeWhile(block: (Buffer) -> Boolean)

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

Link copied to clipboard
fun tryPeek(): Int

Returns next byte (unsigned) or -1 if no more bytes available