Package io.ktor.utils.io

Types

Link copied to clipboard
interface ByteChannel : ByteReadChannel, ByteWriteChannel

Channel for asynchronous reading and writing of sequences of bytes. This is a buffered single-reader single-writer channel.

Link copied to clipboard

Sequential (non-concurrent) byte channel implementation

Link copied to clipboard
class ByteChannelSequentialJVM(initial: ChunkBuffer, autoFlush: Boolean) : ByteChannelSequentialBase
Link copied to clipboard
expect interface ByteReadChannel

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

actual interface ByteReadChannel

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

actual interface ByteReadChannel

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

actual interface ByteReadChannel

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

Link copied to clipboard
expect interface ByteWriteChannel

Channel for asynchronous writing of sequences of bytes. This is a single-writer channel.

actual interface ByteWriteChannel

Channel for asynchronous writing of sequences of bytes. This is a single-writer channel.

actual interface ByteWriteChannel

Channel for asynchronous writing of sequences of bytes. This is a single-writer channel.

actual interface ByteWriteChannel

Channel for asynchronous writing of sequences of bytes. This is a single-writer channel.

Link copied to clipboard
typealias CancellationException = kotlinx.coroutines.CancellationException
Link copied to clipboard
class ClosedWriteChannelException(message: String?) : CancellationException

Indicates attempt to write on isClosedForWrite channel that was closed without a cause. A failed channel rethrows the original close cause exception on send attempts.

Link copied to clipboard
typealias ConsumeEachBufferVisitor = (buffer: ByteBuffer, last: Boolean) -> Boolean

Visitor function that is invoked for every available buffer (or chunk) of a channel. The last parameter shows that the buffer is known to be the last.

Link copied to clipboard
interface LookAheadSession
Link copied to clipboard
interface LookAheadSuspendSession : LookAheadSession
Link copied to clipboard
interface ReaderJob : Job

A coroutine job that is reading from a byte channel

Link copied to clipboard
interface ReaderScope : CoroutineScope
Link copied to clipboard
interface ReadSession
Link copied to clipboard
interface SuspendableReadSession : ReadSession
Link copied to clipboard
interface WriterJob : Job

A coroutine job that is writing to a byte channel

Link copied to clipboard
interface WriterScope : CoroutineScope
Link copied to clipboard
interface WriterSession
Link copied to clipboard
interface WriterSuspendSession : WriterSession

Functions

Link copied to clipboard
expect fun ByteChannel(autoFlush: Boolean = false): ByteChannel

Creates buffered channel for asynchronous reading and writing of sequences of bytes.

actual fun ByteChannel(autoFlush: Boolean = false): ByteChannel

Creates buffered channel for asynchronous reading and writing of sequences of bytes.

actual fun ByteChannel(autoFlush: Boolean = false): ByteChannel

Creates buffered channel for asynchronous reading and writing of sequences of bytes.

fun ByteChannel(autoFlush: Boolean = false, exceptionMapper: (Throwable?) -> Throwable?): ByteChannel

Creates buffered channel for asynchronous reading and writing of sequences of bytes using close function to close channel.

actual fun ByteChannel(autoFlush: Boolean = false): ByteChannel

Creates buffered channel for asynchronous reading and writing of sequences of bytes.

Link copied to clipboard
fun ByteReadChannel(content: ByteArray): ByteReadChannel
fun ByteReadChannel(content: ByteArray, offset: Int): ByteReadChannel

Creates channel for reading from the specified byte array. Please note that it could use content directly or copy its bytes depending on the platform.

fun ByteReadChannel(text: String, charset: Charset = Charsets.UTF_8): ByteReadChannel

expect fun ByteReadChannel(content: ByteArray, offset: Int, length: Int): ByteReadChannel

Creates channel for reading from the specified byte array. Please note that it could use content directly or copy its bytes depending on the platform

fun ByteReadChannel(content: ArrayBufferView): ByteReadChannel

Creates channel for reading from the specified ArrayBufferView

actual fun ByteReadChannel(content: ByteArray, offset: Int, length: Int): ByteReadChannel

Creates channel for reading from the specified byte array.

fun ByteReadChannel(content: ByteBuffer): ByteReadChannel

Creates channel for reading from the specified byte buffer.

actual fun ByteReadChannel(content: ByteArray, offset: Int, length: Int): ByteReadChannel

Creates channel for reading from the specified byte array.

actual fun ByteReadChannel(content: ByteArray, offset: Int, length: Int): ByteReadChannel

Creates channel for reading from the specified byte array.

Link copied to clipboard
fun ByteReadChannel.cancel(): Boolean
Link copied to clipboard
fun ByteWriteChannel.close(): Boolean

Closes this channel with no failure (successfully)

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
inline fun LookAheadSession.consumeEachRemaining(visitor: (ByteBuffer) -> Boolean)
inline suspend fun LookAheadSuspendSession.consumeEachRemaining(visitor: suspend (ByteBuffer) -> Boolean)
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.

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.

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)
actual suspend fun ByteReadChannel.joinTo(dst: ByteWriteChannel, closeOnEnd: Boolean)
Link copied to clipboard
expect fun Throwable.printStack()

Print exception stacktrace.

actual fun Throwable.printStack()

Print exception stacktrace.

actual fun Throwable.printStack()

Print exception stacktrace.

actual fun Throwable.printStack()

Print exception stacktrace.

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
fun CoroutineScope.reader(coroutineContext: CoroutineContext = EmptyCoroutineContext, channel: ByteChannel, block: suspend ReaderScope.() -> Unit): ReaderJob
fun CoroutineScope.reader(coroutineContext: CoroutineContext = EmptyCoroutineContext, autoFlush: Boolean = false, block: suspend ReaderScope.() -> Unit): ReaderJob
fun reader(coroutineContext: CoroutineContext, channel: ByteChannel, parent: Job? = null, block: suspend ReaderScope.() -> Unit): ReaderJob
fun reader(coroutineContext: CoroutineContext, autoFlush: Boolean = false, parent: Job? = null, block: suspend ReaderScope.() -> Unit): ReaderJob
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: ChunkBuffer)
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.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 <E : Throwable> tryCopyException(exception: E, cause: Throwable): E?

Try copy exception using cause as cause.

Link copied to clipboard
inline suspend fun ByteWriteChannel.write(desiredSpace: Int = 1, block: (freeSpace: Memory, startOffset: Long, endExclusive: Long) -> Int): Int

Await for desiredSpace will be available for write and invoke block function providing Memory instance and the corresponding range suitable for wiring in the memory. The block function should return number of bytes were written, possibly 0.

Link copied to clipboard
suspend fun ByteWriteChannel.writeAvailable(src: ByteArray): Int
Link copied to clipboard
suspend fun ByteWriteChannel.writeBoolean(b: Boolean)
Link copied to clipboard
suspend fun ByteWriteChannel.writeByte(b: Int)
Link copied to clipboard
suspend fun ByteWriteChannel.writeChar(ch: Char)

Writes UTF16 character

Link copied to clipboard
suspend fun ByteWriteChannel.writeDouble(value: Double, byteOrder: ByteOrder)
Link copied to clipboard
suspend fun ByteWriteChannel.writeDoubleLittleEndian(value: Double)
Link copied to clipboard
suspend fun ByteWriteChannel.writeFloat(value: Float, byteOrder: ByteOrder)
Link copied to clipboard
suspend fun ByteWriteChannel.writeFloatLittleEndian(value: Float)
Link copied to clipboard
suspend fun ByteWriteChannel.writeFully(src: ByteArray)
Link copied to clipboard
suspend fun ByteWriteChannel.writeInt(i: Long)
suspend fun ByteWriteChannel.writeInt(value: Int, byteOrder: ByteOrder)
suspend fun ByteWriteChannel.writeInt(i: Long, byteOrder: ByteOrder)
Link copied to clipboard
suspend fun ByteWriteChannel.writeIntLittleEndian(value: Int)
Link copied to clipboard
suspend fun ByteWriteChannel.writeLong(value: Long, byteOrder: ByteOrder)
Link copied to clipboard
suspend fun ByteWriteChannel.writeLongLittleEndian(value: Long)
Link copied to clipboard
inline suspend fun ByteWriteChannel.writePacket(builder: BytePacketBuilder.() -> Unit)
Link copied to clipboard
suspend fun ByteWriteChannel.writePacketSuspend(builder: suspend BytePacketBuilder.() -> Unit)
Link copied to clipboard
fun CoroutineScope.writer(coroutineContext: CoroutineContext = EmptyCoroutineContext, channel: ByteChannel, block: suspend WriterScope.() -> Unit): WriterJob
fun CoroutineScope.writer(coroutineContext: CoroutineContext = EmptyCoroutineContext, autoFlush: Boolean = false, block: suspend WriterScope.() -> Unit): WriterJob
fun writer(coroutineContext: CoroutineContext, channel: ByteChannel, parent: Job? = null, block: suspend WriterScope.() -> Unit): WriterJob
fun writer(coroutineContext: CoroutineContext, autoFlush: Boolean = false, parent: Job? = null, block: suspend WriterScope.() -> Unit): WriterJob
Link copied to clipboard
suspend fun ByteWriteChannel.writeShort(s: Int)
suspend fun ByteWriteChannel.writeShort(s: Int, byteOrder: ByteOrder)
suspend fun ByteWriteChannel.writeShort(value: Short, byteOrder: ByteOrder)
Link copied to clipboard
suspend fun ByteWriteChannel.writeShortLittleEndian(value: Short)
Link copied to clipboard
suspend fun ByteWriteChannel.writeStringUtf8(s: CharSequence)
suspend fun ByteWriteChannel.writeStringUtf8(s: String)