Is there a simple way to create an `okio.Source` f...
# ktor
s
Is there a simple way to create an
okio.Source
from a
ByteReadChannel
?
a
You can use the following conversion method, but I'm not sure how effective it is:
Copy code
fun ByteReadChannel.toOkioSource(context: CoroutineContext = EmptyCoroutineContext): okio.Source {
    val channel = this
    return object : okio.Source {
        override fun close() {
            channel.cancel()
        }

        override fun read(sink: Buffer, byteCount: Long): Long {
            val buffer = ByteBuffer.allocateDirect(4096)
            val read = runBlocking(context) { channel.readAvailable(buffer) }
            buffer.flip()
            sink.write(buffer)
            return read.toLong()
        }

        override fun timeout(): Timeout {
            return Timeout()
        }
    }
}
s
using
runBlocking
on every buffer read does not feel great sweating