Joffrey
07/29/2019, 5:48 PMFlow
of lines from a File
(on JVM)?
I thought about several options, like:
- FileReader(file).buffered().lineSequence().asFlow()
(requires to close reader externally)
- flow { FileReader(file).useLines { lines -> lines.forEach { emit(it) } } }
(broken reader closure it early termination)
But I believe that would be using the blocking file API behind the scenes. Is this OK, though?
Should I implement my flow on top of AsynchronousFileChannel
instead?tseisel
07/29/2019, 6:17 PMflowOn
.Joffrey
07/29/2019, 6:19 PMflowOn(<http://Dispatchers.IO|Dispatchers.IO>)
with it, but I'm not comfortable enough with the implementation details of flows and coroutines to be sure that this is OK compared to using actual asynchronous APIs in the first placeJoffrey
07/29/2019, 7:00 PMfun File.lineFlow(): Flow<String> {
val reader = bufferedReader()
return reader.buffered()
.lineSequence()
.asFlow()
.onCompletion { reader.close() }
.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
}