AbstractInput

abstract class AbstractInput : Input

The default abstract base class implementing Input interface.

See Also

AbstractInput.fill

Constructors

<init>

AbstractInput(
    head: IoBuffer = IoBuffer.Empty,
    remaining: Long = head.remainingAll(),
    pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool)
AbstractInput(
    head: ChunkBuffer = ChunkBuffer.Empty,
    remaining: Long = head.remainingAll(),
    pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool)

The default abstract base class implementing Input interface.

Properties

byteOrder

var byteOrder: ByteOrder

endOfInput

val endOfInput: Boolean

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.

isEmpty

val isEmpty: Boolean

true if no bytes available for read

isNotEmpty

val isNotEmpty: Boolean

pool

remaining

val remaining: Long

Number of bytes available for read

Functions

canRead

fun canRead(): Boolean

close

fun close(): Unit

closeSource

abstract fun closeSource(): Unit

Should close the underlying bytes source. Could do nothing or throw exceptions.

discard

fun discard(n: Int): Int

Discards at most n bytes

fun discard(n: Long): Long

discardExact

fun discardExact(n: Int): Unit

Discards exactly n bytes or fails with EOFException

ensureNextHead

fun ensureNextHead(current: ChunkBuffer): ChunkBuffer?

fill

abstract fun fill(
    destination: Memory,
    offset: Int,
    length: Int
): Int

Read the next bytes into the destination starting at offset at most length bytes. May block until at least one byte is available. Usually bypass all exceptions from the underlying source.

open fun fill(): ChunkBuffer?

Reads the next chunk suitable for reading or null if no more chunks available. It is also allowed to return a chain of chunks linked through ChunkBuffer.next. The last chunk should have null next reference. Could rethrow exceptions from the underlying source.

fixGapAfterRead

fun fixGapAfterRead(current: ChunkBuffer): Unit

hasBytes

fun hasBytes(n: Int): Boolean

markNoMoreChunksAvailable

fun markNoMoreChunksAvailable(): Unit

peekTo

fun peekTo(
    destination: Memory,
    destinationOffset: Long,
    offset: Long,
    min: Long,
    max: Long
): Long

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 fails with an exception. 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 but only if min = 0, fails otherwise. This function usually copy more bytes than min (unless max = min).

fun peekTo(buffer: IoBuffer): Int

Copy available bytes to the specified buffer but keep them available. If the underlying implementation could trigger bytes population from the underlying source and block until any bytes available

prepareReadHead

fun prepareReadHead(minSize: Int): ChunkBuffer?

readByte

fun readByte(): Byte

readDouble

fun readDouble(): Double

readFloat

fun readFloat(): Float

readFully

fun readFully(dst: ByteArray, offset: Int, length: Int): Unit

Read exactly length bytes to dst array at specified offset

readInt

fun readInt(): Int

readLong

fun readLong(): Long

readShort

fun readShort(): Short

readText

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

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

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

readTextExact

fun readTextExact(
    out: Appendable,
    exactCharacters: Int
): Unit

Read exactly exactCharacters characters and append them to out

fun readTextExact(exactCharacters: Int): String

Read a string exactly exactCharacters length

release

fun release(): Unit

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.

tryPeek

fun tryPeek(): Int

updateHeadRemaining

fun updateHeadRemaining(remaining: Int): Unit

Inherited Functions

readAvailable

open fun readAvailable(
    dst: ByteArray,
    offset: Int,
    length: Int
): Int
open fun readAvailable(
    dst: ShortArray,
    offset: Int,
    length: Int
): Int
open fun readAvailable(
    dst: IntArray,
    offset: Int,
    length: Int
): Int
open fun readAvailable(
    dst: LongArray,
    offset: Int,
    length: Int
): Int
open fun readAvailable(
    dst: FloatArray,
    offset: Int,
    length: Int
): Int
open fun readAvailable(
    dst: DoubleArray,
    offset: Int,
    length: Int
): Int
open fun readAvailable(dst: IoBuffer, length: Int): Int
open fun readAvailable(
    dst: ByteBuffer,
    length: Int = dst.remaining()
): Int

readFully

open fun readFully(
    dst: ShortArray,
    offset: Int,
    length: Int
): Unit
open fun readFully(
    dst: IntArray,
    offset: Int,
    length: Int
): Unit
open fun readFully(
    dst: LongArray,
    offset: Int,
    length: Int
): Unit
open fun readFully(
    dst: FloatArray,
    offset: Int,
    length: Int
): Unit
open fun readFully(
    dst: DoubleArray,
    offset: Int,
    length: Int
): Unit
open fun readFully(dst: IoBuffer, length: Int): Unit
open fun readFully(
    dst: ByteBuffer,
    length: Int = dst.remaining()
): Unit

Extension Properties

isEmpty

val Input.isEmpty: Boolean

For streaming input it should be Input.endOfInput instead.

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.

Extension Functions

asStream

fun Input.asStream(): InputStream

Convert io.ktor.utils.io Input to java InputStream

completeReadHead

fun Input.completeReadHead(current: IoBuffer): Unit
fun Input.completeReadHead(current: ChunkBuffer): Unit

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

forEach

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

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

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.

prepareReadFirstHead

fun Input.prepareReadFirstHead(minSize: Int): ChunkBuffer?

prepareReadFirstHeadOld

fun Input.prepareReadFirstHeadOld(minSize: Int): IoBuffer?

prepareReadNextHead

fun Input.prepareReadNextHead(
    current: ChunkBuffer
): ChunkBuffer?

prepareReadNextHeadOld

fun Input.prepareReadNextHeadOld(
    current: IoBuffer
): IoBuffer?

readAvailable

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

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

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

readBytes

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 AbstractInput.readDirect(
    size: Int,
    block: (ByteBuffer) -> Unit
): Unit

readDouble

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

readDoubleFallback

fun Input.readDoubleFallback(): Double

readDoubleLittleEndian

fun Input.readDoubleLittleEndian(): Double

readFloat

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

readFloatFallback

fun Input.readFloatFallback(): Float

readFloatLittleEndian

fun Input.readFloatLittleEndian(): Float

readFully

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

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

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 Input.readInt(byteOrder: ByteOrder): Int
fun Input.readInt(): Int

readIntLittleEndian

fun Input.readIntLittleEndian(): Int

readLong

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

readLongLittleEndian

fun Input.readLongLittleEndian(): Long

readShort

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

readShortLittleEndian

fun Input.readShortLittleEndian(): Short

readText

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

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 Input.readUByte(): <ERROR CLASS>

readUInt

fun Input.readUInt(): <ERROR CLASS>

readULong

fun Input.readULong(): <ERROR CLASS>

readUShort

fun Input.readUShort(): <ERROR CLASS>

readUTF8Line

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.

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

Inheritors

ByteReadPacketBase

abstract class ByteReadPacketBase : AbstractInput