https://kotlinlang.org logo
#ktor
Title
# ktor
m

Manuel Pérez Alcolea

03/26/2023, 7:15 PM
I'm using a library designed around coroutines that also uses ktor so I also have ktor goodies to use. I noticed ktor provides "channels" (
ByteReadChannel
,
ByteWriteChannel
, what's the benefit of them? And what does the following mean?
Operations on this channel cannot be invoked concurrently.
when is a
ByteWriteChannel
more convenient than this?
Copy code
val url = URL(...)
val targetDir = Path.of(...)

withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
  url.openStream().use { stream ->
    Files.copy(stream, targetDir)
  }
}
are they just meant to be used to read and write in chunks without blocking everything else in a thread, and to yield it to other coroutines?
a

Aleksei Tirman [JB]

03/27/2023, 9:39 AM
They are primitives to work with a stream of bytes in a non-blocking way.
2 Views