asNSOutputStream

fun Sink.asNSOutputStream(): NSOutputStream(source)

Returns an output stream that writes to this sink. Closing the stream will also close this sink.

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 (NSOutputStream.initToBuffer, NSOutputStream.initToMemory, NSOutputStream.initWithURL, NSOutputStream.initToFileAtPath), 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
}