saket
07/18/2021, 4:30 AMByteReadChannel
looks correct?
private fun ByteReadChannel.source(): okio.Source {
val channel = this
return object : okio.Source {
override fun read(sink: okio.Buffer, byteCount: Long): Long {
if (byteCount == 0L) return 0L
return runBlocking {
if (!channel.isClosedForRead) {
val bytes = channel.toByteArray(byteCount.toInt())
sink.write(bytes)
bytes.size.toLong()
} else {
-1. // Source is exhausted.
}
}
}
override fun close() = Unit // Couldn't find any way to close ByteReadChannel.
override fun timeout() = Timeout.NONE
}
}
rnett
07/18/2021, 6:55 AMThomas
07/18/2021, 12:19 PMchannel.toInputStream().source()
import io.ktor.utils.io.jvm.javaio.toInputStream
import okio.source
saket
07/20/2021, 4:13 PM