Hi,
I’m looking for suspending way of reading/writing on file. I found this example from Kotlin/coroutines-examples which is wraping AsynchronousFileChannel
with suspendCoroutine { ... }
.
suspend fun AsynchronousFileChannel.aRead(buf: ByteBuffer): Int =
suspendCoroutine { cont ->
read(buf, 0L, Unit, object : CompletionHandler<Int, Unit> {
override fun completed(bytesRead: Int, attachment: Unit) {
cont.resume(bytesRead)
}
override fun failed(exception: Throwable, attachment: Unit) {
cont.resumeWithException(exception)
}
})
}
And I’m wondering if there is any benefit from wraping synchronous call with withContext{}
like so
val text = withContext(IO) { File(...).bufferedReader().readText() }