ByteReadChannel

expect interface ByteReadChannel

Channel for asynchronous reading of sequences of bytes. This is a single-reader channel.

Operations on this channel cannot be invoked concurrently.

actual interface ByteReadChannel

Channel for asynchronous reading of sequences of bytes. This is a single-reader channel.

Operations on this channel cannot be invoked concurrently.

actual interface ByteReadChannel

Channel for asynchronous reading of sequences of bytes. This is a single-reader channel.

Operations on this channel cannot be invoked concurrently.

Types

Link copied to clipboard
expect object Companion
actual object Companion
actual object Companion

Functions

Link copied to clipboard
abstract suspend fun awaitContent()
Link copied to clipboard
expect abstract fun cancel(cause: Throwable?): Boolean

Close channel with optional cause cancellation. Unlike ByteWriteChannel.close that could close channel normally, cancel does always close with error so any operations on this channel will always fail and all suspensions will be resumed with exception.

actual abstract fun cancel(cause: Throwable?): Boolean

Close channel with optional cause cancellation. Unlike ByteWriteChannel.close that could close channel normally, cancel does always close with error so any operations on this channel will always fail and all suspensions will be resumed with exception.

actual abstract fun cancel(cause: Throwable?): Boolean

Close channel with optional cause cancellation. Unlike ByteWriteChannel.close that could close channel normally, cancel does always close with error so any operations on this channel will always fail and all suspensions will be resumed with exception.

Link copied to clipboard
open suspend fun consumeEachBufferRange(visitor: ConsumeEachBufferVisitor)

For every available bytes range invokes visitor function until it return false or end of stream encountered

Link copied to clipboard
expect abstract suspend fun discard(max: Long): Long

Discard up to max bytes

actual abstract suspend fun discard(max: Long): Long

Discard up to max bytes

actual abstract suspend fun discard(max: Long): Long

Discard up to max bytes

Link copied to clipboard
abstract fun <R> lookAhead(visitor: LookAheadSession.() -> R): R
Link copied to clipboard
abstract suspend fun <R> lookAheadSuspend(visitor: suspend LookAheadSuspendSession.() -> R): R
Link copied to clipboard
expect abstract suspend 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 suspending reading. 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 isClosedForRead.

actual abstract suspend 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 suspending reading. 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.

actual abstract suspend 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 suspending reading. 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
abstract suspend fun read(min: Int = 1, consumer: (ByteBuffer) -> Unit)

Invokes consumer when it will be possible to read at least min bytes providing byte buffer to it so lambda can read from the buffer up to ByteBuffer.remaining bytes. If there are no min bytes available then the invocation could suspend until the requirement will be met.

Link copied to clipboard
expect abstract suspend fun readAvailable(dst: IoBuffer): Int
expect abstract suspend fun readAvailable(dst: ByteArray, offset: Int, length: Int): Int

Reads all available bytes to dst buffer and returns immediately or suspends if no bytes available

actual abstract suspend fun readAvailable(dst: IoBuffer): Int
abstract suspend fun readAvailable(dst: ByteBuffer): Int
abstract fun readAvailable(min: Int = 1, block: (ByteBuffer) -> Unit): Int

Invokes block if it is possible to read at least min byte providing byte buffer to it so lambda can read from the buffer up to ByteBuffer.available bytes. If there are no min bytes available then the invocation returns 0.

actual abstract suspend fun readAvailable(dst: ByteArray, offset: Int, length: Int): Int

Reads all available bytes to dst buffer and returns immediately or suspends if no bytes available

actual abstract suspend fun readAvailable(dst: IoBuffer): Int
abstract suspend fun readAvailable(dst: <ERROR CLASS><<ERROR CLASS>>, offset: Int, length: Int): Int
abstract suspend fun readAvailable(dst: <ERROR CLASS><<ERROR CLASS>>, offset: Long, length: Long): Int
actual abstract suspend fun readAvailable(dst: ByteArray, offset: Int, length: Int): Int

Reads all available bytes to dst buffer and returns immediately or suspends if no bytes available

Link copied to clipboard
expect abstract suspend fun readBoolean(): Boolean

Reads a boolean value (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readBoolean(): Boolean

Reads a boolean value (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readBoolean(): Boolean

Reads a boolean value (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readByte(): Byte

Reads a byte (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readByte(): Byte

Reads a byte (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readByte(): Byte

Reads a byte (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readDouble(): Double

Reads double number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readDouble(): Double

Reads double number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readDouble(): Double

Reads double number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readFloat(): Float

Reads float number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readFloat(): Float

Reads float number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readFloat(): Float

Reads float number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readFully(dst: IoBuffer, n: Int)
expect abstract suspend fun readFully(dst: ByteArray, offset: Int, length: Int)

Reads all length bytes to dst buffer or fails if channel has been closed. Suspends if not enough bytes available.

abstract suspend fun readFully(dst: ByteBuffer): Int
actual abstract suspend fun readFully(dst: IoBuffer, n: Int)

actual abstract suspend fun readFully(dst: ByteArray, offset: Int, length: Int)

Reads all length bytes to dst buffer or fails if channel has been closed. Suspends if not enough bytes available.

actual abstract suspend fun readFully(dst: IoBuffer, n: Int)

Reads all length bytes to dst buffer or fails if channel has been closed. Suspends if not enough bytes available.

abstract suspend fun readFully(dst: <ERROR CLASS><<ERROR CLASS>>, offset: Int, length: Int)
abstract suspend fun readFully(dst: <ERROR CLASS><<ERROR CLASS>>, offset: Long, length: Long)
actual abstract suspend fun readFully(dst: ByteArray, offset: Int, length: Int)

Reads all length bytes to dst buffer or fails if channel has been closed. Suspends if not enough bytes available.

Link copied to clipboard
expect abstract suspend fun readInt(): Int

Reads an int number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readInt(): Int

Reads an int number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readInt(): Int

Reads an int number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readLong(): Long

Reads a long number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readLong(): Long

Reads a long number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readLong(): Long

Reads a long number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readPacket(size: Int, headerSizeHint: Int): ByteReadPacket

Reads the specified amount of bytes and makes a byte packet from them. Fails if channel has been closed and not enough bytes available. Accepts headerSizeHint to be provided, see BytePacketBuilder.

actual abstract suspend fun readPacket(size: Int, headerSizeHint: Int): ByteReadPacket

Reads the specified amount of bytes and makes a byte packet from them. Fails if channel has been closed and not enough bytes available. Accepts headerSizeHint to be provided, see WritePacket.

actual abstract suspend fun readPacket(size: Int, headerSizeHint: Int): ByteReadPacket

Reads the specified amount of bytes and makes a byte packet from them. Fails if channel has been closed and not enough bytes available. Accepts headerSizeHint to be provided, see BytePacketBuilder.

Link copied to clipboard
expect abstract suspend fun readRemaining(limit: Long, headerSizeHint: Int): ByteReadPacket

Reads up to limit bytes and makes a byte packet or until end of stream encountered. Accepts headerSizeHint to be provided, see BytePacketBuilder.

actual abstract suspend fun readRemaining(limit: Long, headerSizeHint: Int): ByteReadPacket

Reads up to limit bytes and makes a byte packet or until end of stream encountered. Accepts headerSizeHint to be provided, see BytePacketBuilder.

actual abstract suspend fun readRemaining(limit: Long, headerSizeHint: Int): ByteReadPacket

Reads up to limit bytes and makes a byte packet or until end of stream encountered. Accepts headerSizeHint to be provided, see BytePacketBuilder.

Link copied to clipboard
expect abstract fun readSession(consumer: ReadSession.() -> Unit)

Starts non-suspendable read session. After channel preparation consumer lambda will be invoked immediately event if there are no bytes available for read yet.

actual abstract fun readSession(consumer: ReadSession.() -> Unit)

Starts non-suspendable read session. After channel preparation consumer lambda will be invoked immediately even if there are no bytes available for read yet.

actual abstract fun readSession(consumer: ReadSession.() -> Unit)

Starts non-suspendable read session. After channel preparation consumer lambda will be invoked immediately event if there are no bytes available for read yet.

Link copied to clipboard
expect abstract suspend fun readShort(): Short

Reads a short number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readShort(): Short

Reads a short number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

actual abstract suspend fun readShort(): Short

Reads a short number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.

Link copied to clipboard
expect abstract suspend fun readSuspendableSession(consumer: suspend SuspendableReadSession.() -> Unit)

Starts a suspendable read session. After channel preparation consumer lambda will be invoked immediately even if there are no bytes available for read yet. consumer lambda could suspend as much as needed.

actual abstract suspend fun readSuspendableSession(consumer: suspend SuspendableReadSession.() -> Unit)

Starts a suspendable read session. After channel preparation consumer lambda will be invoked immediately even if there are no bytes available for read yet. consumer lambda could suspend as much as needed.

actual abstract suspend fun readSuspendableSession(consumer: suspend SuspendableReadSession.() -> Unit)

Starts a suspendable read session. After channel preparation consumer lambda will be invoked immediately even if there are no bytes available for read yet. consumer lambda could suspend as much as needed.

Link copied to clipboard
expect abstract suspend fun readUTF8Line(limit: Int): String?

Reads a line of UTF-8 characters up to limit characters. Supports both CR-LF and LF line endings. Throws an exception if the specified limit has been exceeded.

actual abstract suspend fun readUTF8Line(limit: Int): String?

Reads a line of UTF-8 characters up to limit characters. Supports both CR-LF and LF line endings. Throws an exception if the specified limit has been exceeded.

actual abstract suspend fun readUTF8Line(limit: Int): String?

Reads a line of UTF-8 characters up to limit characters. Supports both CR-LF and LF line endings. Throws an exception if the specified limit has been exceeded.

Link copied to clipboard
expect abstract suspend fun <A : Appendable> readUTF8LineTo(out: A, limit: Int): Boolean

Reads a line of UTF-8 characters to the specified out buffer up to limit characters. Supports both CR-LF and LF line endings. No line ending characters will be appended to out buffer. Throws an exception if the specified limit has been exceeded.

Properties

Link copied to clipboard
expect abstract val availableForRead: Int

Returns number of bytes that can be read without suspension. Read operations do no suspend and return immediately when this number is at least the number of bytes requested for read.

actual abstract val availableForRead: Int

Returns number of bytes that can be read without suspension. Read operations do no suspend and return immediately when this number is at least the number of bytes requested for read.

actual abstract val availableForRead: Int

Returns number of bytes that can be read without suspension. Read operations do no suspend and return immediately when this number is at least the number of bytes requested for read.

Link copied to clipboard
expect abstract val closedCause: Throwable?

A closure causes exception or null if closed successfully or not yet closed

actual abstract val closedCause: Throwable?

A closure causes exception or null if closed successfully or not yet closed

actual abstract val closedCause: Throwable?

A closure causes exception or null if closed successfully or not yet closed

Link copied to clipboard
expect abstract val isClosedForRead: Boolean

Returns true if the channel is closed and no remaining bytes are available for read. It implies that availableForRead is zero.

actual abstract val isClosedForRead: Boolean

Returns true if the channel is closed and no remaining bytes are available for read. It implies that availableForRead is zero.

actual abstract val isClosedForRead: Boolean

Returns true if the channel is closed and no remaining bytes are available for read. It implies that availableForRead is zero.

Link copied to clipboard
expect abstract val isClosedForWrite: Boolean
actual abstract val isClosedForWrite: Boolean
actual abstract val isClosedForWrite: Boolean

Returns true if the channel is closed from the writer side. The availableForRead can be 0.

Link copied to clipboard
expect abstract var readByteOrder: ByteOrder

Byte order that is used for multi-byte read operations (such as readShort, readInt, readLong, readFloat, and readDouble).

actual abstract var readByteOrder: ByteOrder

Byte order that is used for multi-byte read operations (such as readShort, readInt, readLong, readFloat, and readDouble).

actual abstract var readByteOrder: ByteOrder

Byte order that is used for multi-byte read operations (such as readShort, readInt, readLong, readFloat, and readDouble).

Link copied to clipboard
expect abstract val totalBytesRead: Long

Number of bytes read from the channel. It is not guaranteed to be atomic so could be updated in the middle of long-running read operation.

actual abstract val totalBytesRead: Long

Number of bytes read from the channel. It is not guaranteed to be atomic so could be updated in the middle of long-running read operation.

actual abstract val totalBytesRead: Long

Number of bytes read from the channel. It is not guaranteed to be atomic so could be updated in the middle of long-running read operation.

Inheritors

Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
fun ByteReadChannel.cancel(): Boolean
Link copied to clipboard
inline suspend fun ByteReadChannel.consumeEachBufferRange(visitor: ConsumeEachBufferVisitor)

For every available bytes range invokes visitor function until it return false or end of stream encountered. The provided buffer should be never captured outside of the visitor block otherwise resource leaks, crashes and data corruptions may occur. The visitor block may be invoked multiple times, once or never.

Link copied to clipboard
suspend fun ByteReadChannel.copyAndClose(dst: ByteWriteChannel, limit: Long = Long.MAX_VALUE): Long

Reads all the bytes from receiver channel and writes them to dst channel and then closes it. Closes dst channel if fails to read or write with cause exception.

Link copied to clipboard
suspend fun ByteReadChannel.copyTo(dst: ByteWriteChannel): Long

Reads bytes from receiver channel and writes them to dst channel. Closes dst channel if fails to read or write with cause exception.

expect suspend fun ByteReadChannel.copyTo(dst: ByteWriteChannel, limit: Long): Long

Reads up to limit bytes from receiver channel and writes them to dst channel. Closes dst channel if fails to read or write with cause exception.

suspend fun ByteReadChannel.copyTo(channel: WritableByteChannel, limit: Long = Long.MAX_VALUE): Long

Copy up to limit bytes to blocking NIO channel. Copying to non-blocking channel requires selection and not supported. It does suspend if no data available in byte channel but may block if destination NIO channel blocks.

suspend fun ByteReadChannel.copyTo(pipe: Pipe, limit: Long = Long.MAX_VALUE): Long

Copy up to limit bytes to blocking pipe. A shortcut to copyTo function with NIO channel destination

suspend fun ByteReadChannel.copyTo(out: OutputStream, limit: Long = Long.MAX_VALUE): Long

Copies up to limit bytes from this byte channel to out stream suspending on read channel and blocking on output

actual suspend fun ByteReadChannel.copyTo(dst: ByteWriteChannel, limit: Long): Long

Reads up to limit bytes from receiver channel and writes them to dst channel. Closes dst channel if fails to read or write with cause exception.

actual suspend fun ByteReadChannel.copyTo(dst: ByteWriteChannel, limit: Long): Long

Reads up to limit bytes from receiver channel and writes them to dst channel. Closes dst channel if fails to read or write with cause exception.

Link copied to clipboard
suspend fun ByteReadChannel.discard(): Long

Discards all bytes in the channel and suspends until end of stream.

Link copied to clipboard
inline suspend fun ByteReadChannel.discardExact(n: Long)

Discards exactly n bytes or fails if not enough bytes in the channel

Link copied to clipboard
expect suspend fun ByteReadChannel.joinTo(dst: ByteWriteChannel, closeOnEnd: Boolean)
actual suspend fun ByteReadChannel.joinTo(dst: ByteWriteChannel, closeOnEnd: Boolean)
actual suspend fun ByteReadChannel.joinTo(dst: ByteWriteChannel, closeOnEnd: Boolean)
Link copied to clipboard
inline suspend fun ByteReadChannel.read(desiredSize: Int = 1, block: (source: Memory, start: Long, endExclusive: Long) -> Int): Int

Await until at least desiredSize is available for read or EOF and invoke block function. The block function should never capture a provided Memory instance outside otherwise an undefined behaviour may occur including accidental crash or data corruption. Block function should return number of bytes consumed or 0.

Link copied to clipboard
suspend fun ByteReadChannel.readAvailable(dst: ByteArray): Int
Link copied to clipboard
inline suspend fun ByteReadChannel.readDouble(byteOrder: ByteOrder): Double
Link copied to clipboard
inline suspend fun ByteReadChannel.readDoubleLittleEndian(): Double
Link copied to clipboard
inline suspend fun ByteReadChannel.readFloat(byteOrder: ByteOrder): Float
Link copied to clipboard
inline suspend fun ByteReadChannel.readFloatLittleEndian(): Float
Link copied to clipboard
suspend fun ByteReadChannel.readFully(dst: IoBuffer)
suspend fun ByteReadChannel.readFully(dst: ByteArray)
Link copied to clipboard
inline suspend fun ByteReadChannel.readInt(byteOrder: ByteOrder): Int
Link copied to clipboard
inline suspend fun ByteReadChannel.readIntLittleEndian(): Int
Link copied to clipboard
inline suspend fun ByteReadChannel.readLong(byteOrder: ByteOrder): Long
Link copied to clipboard
inline suspend fun ByteReadChannel.readLongLittleEndian(): Long
Link copied to clipboard
suspend fun ByteReadChannel.readPacket(size: Int): ByteReadPacket

Reads the specified amount of bytes and makes a byte packet from them. Fails if channel has been closed and not enough bytes available.

Link copied to clipboard
suspend fun ByteReadChannel.readRemaining(limit: Long): ByteReadPacket

Reads up to limit bytes and makes a byte packet or until end of stream encountered.

suspend fun ByteReadChannel.readRemaining(): ByteReadPacket

Reads all remaining bytes and makes a byte packet

Link copied to clipboard
inline suspend fun ByteReadChannel.readShort(byteOrder: ByteOrder): Short
Link copied to clipboard
inline suspend fun ByteReadChannel.readShortLittleEndian(): Short
Link copied to clipboard
suspend fun ByteReadChannel.readUntilDelimiter(delimiter: ByteBuffer, dst: ByteBuffer): Int

Reads from the channel to the specified dst byte buffer until one of the following:

Link copied to clipboard
suspend fun ByteReadChannel.readUTF8Line(): String?
Link copied to clipboard
suspend fun ByteReadChannel.readUTF8LineTo(out: Appendable): Boolean
Link copied to clipboard
suspend fun ByteReadChannel.skipDelimiter(delimiter: ByteBuffer)
Link copied to clipboard
fun ByteReadChannel.toInputStream(parent: Job? = null): InputStream

Create blocking java.io.InputStream for this channel that does block every time the channel suspends at read Similar to do reading in runBlocking however you can pass it to regular blocking API