ByteChannelSequentialJVM

class ByteChannelSequentialJVM : ByteChannelSequentialBase

Constructors

<init>

ByteChannelSequentialJVM(
    initial: IoBuffer,
    autoFlush: Boolean)

Inherited Properties

autoFlush

open val autoFlush: Boolean

Returns true if channel flushes automatically all pending bytes after every write function call. If false then flush only happens at manual flush invocation or when the buffer is full.

availableForRead

open 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.

availableForWrite

open val availableForWrite: Int

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

closed

var closed: Boolean

closedCause

var closedCause: Throwable?

An closure cause exception or null if closed successfully or not yet closed

isClosedForRead

open val isClosedForRead: Boolean

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

isClosedForWrite

open val isClosedForWrite: Boolean

Returns true is channel has been closed and attempting to write to the channel will cause an exception.

readByteOrder

open var readByteOrder: ByteOrder

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

readable

val readable: ByteReadPacket

totalBytesRead

open 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.

totalBytesWritten

open val totalBytesWritten: Long

Number of bytes written to the channel. It is not guaranteed to be atomic so could be updated in the middle of write operation.

writable

val writable: BytePacketBuilder

writeByteOrder

open var writeByteOrder: ByteOrder

Byte order that is used for multi-byte write operations (such as writeShort, writeInt, writeLong, writeFloat, and writeDouble).

Functions

attachJob

fun attachJob(job: Job): Unit

consumeEachBufferRange

suspend fun consumeEachBufferRange(
    visitor: ConsumeEachBufferVisitor
): Unit

lookAhead

fun <R> lookAhead(visitor: LookAheadSession.() -> R): R

lookAheadSuspend

suspend fun <R> lookAheadSuspend(
    visitor: suspend LookAheadSuspendSession.() -> R
): R

read

suspend fun read(
    min: Int,
    consumer: (ByteBuffer) -> Unit
): Unit

readAvailable

suspend fun readAvailable(dst: ByteBuffer): Int

readFully

suspend fun readFully(dst: ByteBuffer): Int

write

suspend fun write(
    min: Int,
    block: (ByteBuffer) -> Unit
): Unit

writeAvailable

suspend fun writeAvailable(src: ByteBuffer): Int

writeFully

suspend fun writeFully(src: ByteBuffer): Unit

writeWhile

suspend fun writeWhile(block: (ByteBuffer) -> Boolean): Unit

Inherited Functions

afterRead

fun afterRead(): Unit

afterWrite

fun afterWrite(): Unit

await

open suspend fun await(atLeast: Int): Boolean

Suspend until atLeast bytes become available or end of stream encountered (possibly due to exceptional close)

awaitFreeSpace

suspend fun awaitFreeSpace(): Unit

awaitSuspend

suspend fun awaitSuspend(atLeast: Int): Boolean

beginWriteSession

open fun beginWriteSession(): WriterSuspendSession

cancel

open 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.

close

fun close(cause: Throwable?): Boolean

Closes this channel with an optional exceptional cause. It flushes all pending write bytes (via flush). This is an idempotent operation – repeated invocations of this function have no effect and return false.

discard

open fun discard(n: Int): Int

Discard at most n available bytes or 0 if no bytes available yet

open suspend fun discard(max: Long): Long

Discard up to max bytes

endReadSession

open fun endReadSession(): Unit

endWriteSession

open fun endWriteSession(written: Int): Unit

flush

open fun flush(): Unit

Flushes all pending write bytes making them available for read.

peekTo

suspend fun peekTo(
    destination: Memory,
    destinationOffset: Long,
    offset: Long,
    min: Long,
    max: Long
): 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.

readAvailable

open suspend fun readAvailable(dst: IoBuffer): Int
open 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

readAvailableClosed

fun readAvailableClosed(): Int

readBoolean

open 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.

readByte

open suspend fun readByte(): Byte

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

readDouble

open suspend fun readDouble(): Double

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

readFloat

open suspend fun readFloat(): Float

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

readFully

open suspend fun readFully(dst: IoBuffer, n: Int): Unit
open suspend fun readFully(
    dst: ByteArray,
    offset: Int,
    length: Int
): Unit

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

readInt

open 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.

readLong

open 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.

readPacket

open 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.

readRemaining

open 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.

readSession

open fun readSession(consumer: ReadSession.() -> Unit): 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.

readShort

open 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.

readSuspendableSession

open suspend fun readSuspendableSession(
    consumer: suspend SuspendableReadSession.() -> Unit
): 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.

readUTF8Line

open 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.

readUTF8LineTo

open suspend fun <A> 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. Throws an exception if the specified limit has been exceeded.

request

open fun request(atLeast: Int): IoBuffer?

Request buffer range atLeast bytes length

startReadSession

open fun startReadSession(): SuspendableReadSession

writeAvailable

open suspend fun writeAvailable(src: IoBuffer): Int
open suspend fun writeAvailable(
    src: ByteArray,
    offset: Int,
    length: Int
): Int

Writes as much as possible and only suspends if buffer is full

writeByte

open suspend fun writeByte(b: Byte): Unit

Writes byte and suspends until written. Crashes if channel get closed while writing.

writeDouble

open suspend fun writeDouble(d: Double): Unit

Writes double number and suspends until written. Crashes if channel get closed while writing.

writeFloat

open suspend fun writeFloat(f: Float): Unit

Writes float number and suspends until written. Crashes if channel get closed while writing.

writeFully

open suspend fun writeFully(src: IoBuffer): Unit
open suspend fun writeFully(
    src: ByteArray,
    offset: Int,
    length: Int
): Unit

Writes all src bytes and suspends until all bytes written. Causes flush if buffer filled up or when autoFlush Crashes if channel get closed while writing.

writeInt

open suspend fun writeInt(i: Int): Unit

Writes int number and suspends until written. Crashes if channel get closed while writing.

writeLong

open suspend fun writeLong(l: Long): Unit

Writes long number and suspends until written. Crashes if channel get closed while writing.

writePacket

open suspend fun writePacket(packet: ByteReadPacket): Unit

Writes a packet fully or fails if channel get closed before the whole packet has been written

writeShort

open suspend fun writeShort(s: Short): Unit

Writes short number and suspends until written. Crashes if channel get closed while writing.

writeSuspendSession

open suspend fun writeSuspendSession(
    visitor: suspend WriterSuspendSession.() -> Unit
): Unit

Extension Functions

bufferedWriter

fun ByteWriteChannel.bufferedWriter(
    charset: Charset = Charsets.UTF_8
): BufferedWriter

Open a buffered writer to the channel

cancel

fun ByteReadChannel.cancel(): Boolean

close

Closes this channel with no failure (successfully)

consumeEachBufferRange

suspend fun ByteReadChannel.consumeEachBufferRange(
    visitor: ConsumeEachBufferVisitor
): Unit

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

copyAndClose

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.

copyTo

suspend fun ByteChannelSequentialBase.copyTo(
    dst: ByteChannelSequentialBase,
    limit: Long = Long.MAX_VALUE
): Long

copyTo

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

copyTo

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

deflated

fun ByteReadChannel.deflated(
    gzip: Boolean = true,
    pool: ObjectPool<ByteBuffer> = KtorDefaultPool,
    coroutineContext: <ERROR CLASS> = Dispatchers.Unconfined
): ByteReadChannel
fun ByteWriteChannel.deflated(
    gzip: Boolean = true,
    pool: ObjectPool<ByteBuffer> = KtorDefaultPool,
    coroutineContext: <ERROR CLASS> = Dispatchers.Unconfined
): ByteWriteChannel

Launch a coroutine on coroutineContext that does deflate compression optionally doing CRC and writing GZIP header and trailer if gzip = true

discard

suspend fun ByteReadChannel.discard(): Long

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

discardExact

suspend fun ByteReadChannel.discardExact(n: Long): Unit

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

joinTo

suspend fun ByteChannelSequentialBase.joinTo(
    dst: ByteChannelSequentialBase,
    closeOnEnd: Boolean
): Unit

pass

suspend fun ByteReadChannel.pass(
    buffer: ByteBuffer,
    block: (ByteBuffer) -> Unit
): Unit

Read data chunks from ByteReadChannel using buffer

read

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.

readAvailable

suspend fun ByteReadChannel.readAvailable(
    dst: ByteArray
): Int

readDouble

suspend fun ByteReadChannel.readDouble(
    byteOrder: ByteOrder
): Double

readDoubleLittleEndian

suspend fun ByteReadChannel.readDoubleLittleEndian(): Double

readFloat

suspend fun ByteReadChannel.readFloat(
    byteOrder: ByteOrder
): Float

readFloatLittleEndian

suspend fun ByteReadChannel.readFloatLittleEndian(): Float

readFully

suspend fun ByteReadChannel.readFully(dst: IoBuffer): Unit
suspend fun ByteReadChannel.readFully(dst: ByteArray): Unit

readInt

suspend fun ByteReadChannel.readInt(
    byteOrder: ByteOrder
): Int

readIntLittleEndian

suspend fun ByteReadChannel.readIntLittleEndian(): Int

readLong

suspend fun ByteReadChannel.readLong(
    byteOrder: ByteOrder
): Long

readLongLittleEndian

suspend fun ByteReadChannel.readLongLittleEndian(): Long

readPacket

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.

readRemaining

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

readShort

suspend fun ByteReadChannel.readShort(
    byteOrder: ByteOrder
): Short

readShortLittleEndian

suspend fun ByteReadChannel.readShortLittleEndian(): Short

readUTF8Line

suspend fun ByteReadChannel.readUTF8Line(): String?

readUTF8LineTo

suspend fun ByteReadChannel.readUTF8LineTo(
    out: Appendable
): Boolean

readUntilDelimiter

suspend fun ByteReadChannel.readUntilDelimiter(
    delimiter: ByteBuffer,
    dst: ByteBuffer
): Int

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

skipDelimiter

suspend fun ByteReadChannel.skipDelimiter(
    delimiter: ByteBuffer
): Unit

split

fun ByteReadChannel.split(
    coroutineScope: CoroutineScope
): Pair<ByteReadChannel, ByteReadChannel>

Split source ByteReadChannel into 2 new one. Cancel of one channel in split(input or both outputs) cancels other channels.

toByteArray

suspend fun ByteReadChannel.toByteArray(): ByteArray

Read channel to byte array.

toByteArray

suspend fun ByteReadChannel.toByteArray(
    limit: Int = Int.MAX_VALUE
): ByteArray

Convert ByteReadChannel to ByteArray

toInputStream

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

toOutputStream

fun ByteWriteChannel.toOutputStream(
    parent: Job? = null
): OutputStream

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

use

fun ByteWriteChannel.use(
    block: ByteWriteChannel.() -> Unit
): Unit

Executes block on ByteWriteChannel and close it down correctly whether an exception

write

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.

write

suspend fun ByteWriteChannel.write(
    string: String,
    charset: Charset = Charsets.UTF_8
): Unit

Write a string in the specified charset

writeAvailable

suspend fun ByteWriteChannel.writeAvailable(
    src: ByteArray
): Int

writeBoolean

suspend fun ByteWriteChannel.writeBoolean(b: Boolean): Unit

writeByte

suspend fun ByteWriteChannel.writeByte(b: Int): Unit

writeChar

suspend fun ByteWriteChannel.writeChar(ch: Char): Unit

Writes UTF16 character

writeDouble

suspend fun ByteWriteChannel.writeDouble(
    value: Double,
    byteOrder: ByteOrder
): Unit

writeDoubleLittleEndian

suspend fun ByteWriteChannel.writeDoubleLittleEndian(
    value: Double
): Unit

writeFloat

suspend fun ByteWriteChannel.writeFloat(
    value: Float,
    byteOrder: ByteOrder
): Unit

writeFloatLittleEndian

suspend fun ByteWriteChannel.writeFloatLittleEndian(
    value: Float
): Unit

writeFully

suspend fun ByteWriteChannel.writeFully(src: ByteArray): Unit

writeInt

suspend fun ByteWriteChannel.writeInt(i: Long): Unit
suspend fun ByteWriteChannel.writeInt(
    i: Long,
    byteOrder: ByteOrder
): Unit
suspend fun ByteWriteChannel.writeInt(
    value: Int,
    byteOrder: ByteOrder
): Unit

writeIntLittleEndian

suspend fun ByteWriteChannel.writeIntLittleEndian(
    value: Int
): Unit

writeLong

suspend fun ByteWriteChannel.writeLong(
    value: Long,
    byteOrder: ByteOrder
): Unit

writeLongLittleEndian

suspend fun ByteWriteChannel.writeLongLittleEndian(
    value: Long
): Unit

writePacket

suspend fun ByteWriteChannel.writePacket(
    headerSizeHint: Int = 0,
    builder: BytePacketBuilder.() -> Unit
): Unit

writePacketSuspend

suspend fun ByteWriteChannel.writePacketSuspend(
    builder: suspend BytePacketBuilder.() -> Unit
): Unit

writeShort

suspend fun ByteWriteChannel.writeShort(s: Int): Unit
suspend fun ByteWriteChannel.writeShort(
    s: Int,
    byteOrder: ByteOrder
): Unit
suspend fun ByteWriteChannel.writeShort(
    value: Short,
    byteOrder: ByteOrder
): Unit

writeShortLittleEndian

suspend fun ByteWriteChannel.writeShortLittleEndian(
    value: Short
): Unit

writeStringUtf8

suspend fun ByteWriteChannel.writeStringUtf8(
    s: CharSequence
): Unit
suspend fun ByteWriteChannel.writeStringUtf8(s: String): Unit

writer

fun ByteWriteChannel.writer(
    charset: Charset = Charsets.UTF_8
): Writer

Open a writer to the channel