ByteChannel
Channel for asynchronous reading and writing of sequences of bytes. 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 any other operations and between themselves at any time.
Functions
Suspend until the channel has bytes to read or gets closed. Throws exception if the channel was closed with an error.
Invokes block when at least 1 byte is available for write.
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.
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.
Reads a boolean value (suspending if no bytes available yet) or fails if channel has been closed and not enough bytes.
Reads double number (suspending if not enough bytes available) or fails if channel has been closed and not enough bytes.
Reads the specified amount of bytes and makes a byte packet from them. Fails if channel has been closed and not enough bytes available.
Reads up to limit bytes and makes a byte packet or until end of stream encountered.
Starts non-suspendable read session. After channel preparation consumer lambda will be invoked immediately event if there are no bytes available for read yet.
Writes as much as possible and only suspends if buffer is full
Writes double number and suspends until written. Crashes if channel get closed while writing.
Writes float number and suspends until written. Crashes if channel get closed while writing.
Writes a packet fully or fails if channel get closed before the whole packet has been written
Writes short number and suspends until written. Crashes if channel get closed while writing.
Properties
Returns true
if the channel is closed and no remaining bytes are available for read. It implies that availableForRead is zero.