SdkByteChannel

Channel for asynchronous reading and writing sequences of bytes. Conceptually a pipe with a reader on one end, decoupled from a writer on the other.

This is a buffered single-reader single writer channel.

Read operations can be invoked concurrently with write operations, but multiple reads or multiple writes cannot be invoked concurrently with themselves. Exceptions are close and flush which can be invoked concurrently with other operations including between themselves at any time.

Inherited properties

Link copied to clipboard
abstract val autoFlush: Boolean

Returns true if the channel flushes automatically all pending bytes after every write operation. If false then flush only happens when flush is explicitly called or when the buffer is full.

Link copied to clipboard
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
abstract val availableForWrite: Int

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

Link copied to clipboard
abstract val closedCause: Throwable?

Returns the underlying cause the channel was closed with or null if closed successfully or not yet closed. A failed channel will have a closed cause.

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

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

Link copied to clipboard
abstract val totalBytesWritten: Long

Total number of bytes written to the channel.

Functions

Link copied to clipboard
open override fun close()

Release any resources held by this object

Inherited functions

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

Close channel with optional cause cancellation. This is an idempotent operation — subsequent invocations of this function have no effect and return false

Link copied to clipboard
abstract fun close(cause: Throwable?): Boolean

Closes this channel with an optional exceptional cause. All pending bytes are flushed. This is an idempotent operation — subsequent invocations of this function have no effect and return false

Link copied to clipboard
abstract fun flush()

Flushes all pending write bytes making them available for read. Thread safe and can be invoked at any time. It does nothing when invoked on a closed channel.

Link copied to clipboard
abstract suspend fun read(sink: SdkBuffer, limit: Long): Long

Remove at least 1 byte, and up-to limit bytes from this and appends them to sink. Suspends if no bytes are available. Returns the number of bytes read, or -1 if this channel is exhausted. It is not safe to modify sink until this function returns

Link copied to clipboard

Read all bytes from this channel into sink. Returns the total number of bytes written.

Link copied to clipboard
suspend fun SdkByteReadChannel.readFully(sink: SdkBuffer, byteCount: Long)

Read exactly byteCount bytes from this into sink or throws EOFException if the channel is exhausted before all bytes could be read.

Link copied to clipboard

Caution Read the entire contents of the channel into sink. This function will suspend until the channel is exhausted and no bytes remain OR the channel cancelled

Link copied to clipboard

Caution Read the entire contents of the channel into a new buffer and return it. This function will suspend until the channel is exhausted and no bytes remain OR the channel cancelled

Link copied to clipboard
abstract suspend fun write(source: SdkBuffer, byteCount: Long = source.size)

Removes exactly byteCount bytes from source and appends them to this. Suspends until all bytes have been written. It is not safe to modify source until this function returns Throws ClosedWriteChannelException if this channel was already closed.

Link copied to clipboard
suspend fun SdkByteWriteChannel.write(source: ByteArray, offset: Int = 0, limit: Int = source.size - offset)

Write limit bytes from source starting at offset. Suspends until all bytes can be written.

Link copied to clipboard

Removes all bytes from source and writes them to this channel. Returns the total number of bytes read.

Link copied to clipboard

Convenience function to write as many bytes from source as possible without suspending. Returns the number of bytes that could be written.