Yan Pujante
05/14/2021, 5:28 PMjava.lang.Process
and although this does NOT trigger an error, the call to waitFor
is flagged as inaprorriate blocking call. Is there a better way to do it? Code in thread to not pollute here...Yan Pujante
05/14/2021, 5:28 PMsealed class ProcessResult {
data class Out(val line: String) : ProcessResult()
data class Err(val line: String) : ProcessResult()
data class ExitValue(val exitValue: Int) : ProcessResult()
}
@OptIn(ExperimentalCoroutinesApi::class)
fun Process.asFlow(charset: Charset = Charsets.UTF_8) : Flow<ProcessResult> {
val process = this
return channelFlow {
coroutineScope {
if(process.inputStream != null) {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
process.inputStream.use {
for(line in it.bufferedReader(charset).lines()) {
send(ProcessResult.Out(line))
}
}
}
}
if(process.errorStream != null) {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
process.errorStream.use {
for(line in it.bufferedReader(charset).lines()) {
send(ProcessResult.Err(line))
}
}
}
}
}
send(ProcessResult.ExitValue(process.waitFor()))
}
}
elizarov
05/14/2021, 7:00 PMonExit().await()
Yan Pujante
05/14/2021, 7:02 PMUnresolved reference: await
Yan Pujante
05/14/2021, 7:08 PMimplementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.5.0")
to my build file.
It now works. Thanks!Yan Pujante
05/15/2021, 2:34 PMProcessBuilder.start()
gets flagged as inappropriate blocking call.elizarov
05/15/2021, 5:38 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }just in case
Yan Pujante
05/15/2021, 5:39 PM