Yan Pujante
05/14/2021, 5:28 PMjava.lang.ProcesswaitForYan 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: awaitYan Pujante
05/14/2021, 7:08 PMimplementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.5.0")Yan Pujante
05/15/2021, 2:34 PMProcessBuilder.start()elizarov
05/15/2021, 5:38 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }just in caseYan Pujante
05/15/2021, 5:39 PM