RawSource

Supplies a stream of bytes. RawSource is a base interface for kotlinx-io data suppliers.

The interface should be implemented to read data from wherever it's located: from the network, storage, or a buffer in memory. Sources may be layered to transform supplied data, such as to decompress, decrypt, or remove protocol framing.

Most applications shouldn't operate on a raw source directly, but rather on a buffered Source which is both more efficient and more convenient. Use buffered to wrap any raw source with a buffer.

Implementors should abstain from throwing exceptions other than those that are documented for RawSource methods.

Samples

kotlinx.io.samples.RC4DecryptingSource
import kotlinx.io.*
import kotlin.test.Test
import kotlin.test.assertEquals

fun main() { 
   //sampleStart 
   val key = "key"
val source = Buffer().also { it.write(byteArrayOf(0x58, 0x09, 0x57, 0x9fU.toByte(), 0x41, 0xfbU.toByte())) }
val rc4Source = RC4DecryptingSource(source, key).buffered()

assertEquals("Secret", rc4Source.readString()) 
   //sampleEnd
}

Inheritors

Functions

Link copied to clipboard

Returns a new source that buffers reads from the source. The returned source will perform bulk reads into its in-memory buffer. Use this wherever you read a source to get ergonomic and efficient access to data.

Link copied to clipboard
abstract override fun close()

Closes this source and releases the resources held by this source. It is an error to read a closed source. It is safe to close a source more than once.

Link copied to clipboard
abstract fun readAtMostTo(sink: Buffer, byteCount: Long): Long

Removes at least 1, and up to byteCount bytes from this source and appends them to sink. Returns the number of bytes read, or -1 if this source is exhausted.