asNSInputStream

fun Source.asNSInputStream(): NSInputStream(source)

Returns an input stream that reads from this source. Closing the stream will also close this source.

The stream supports both polling and run-loop scheduling, please check Apple's documentation for information about stream events handling.

The stream does not implement initializers (NSInputStream.initWithURL, NSInputStream.initWithData, NSInputStream.initWithFileAtPath), their use will result in a runtime error.

Samples

import kotlinx.cinterop.*
import kotlinx.io.*
import platform.Foundation.*
import kotlin.test.Test
import kotlin.test.assertContentEquals

fun main() { 
   //sampleStart 
   val buffer = Buffer()
val data = ByteArray(100) { it.toByte() }.toNSData()

val outputStream = buffer.asNSOutputStream()
outputStream.open()
outputStream.write(data.bytes?.reinterpret(), data.length)

val inputStream = buffer.asNSInputStream()
inputStream.open()
val readData = NSMutableData.create(length = 100.convert())!!
inputStream.read(readData.bytes?.reinterpret(), 100.convert())

assertContentEquals(data.toByteArray(), readData.toByteArray()) 
   //sampleEnd
}