Using Okio with InputStream and ProcessBuilder

77 Views Asked by At

I'm working on a project where we wrap a python cli API with Javas ProcessBuilder API. The implementation is using manual InputStream-bufferhandling, which is errorprone and I'd like to replace it with Okio.

The current implementation looks like this:

...
override fun execute(vararg command: String): CommandOutput {
    val builder = ProcessBuilder()
    builder.redirectErrorStream(true)
    val process = builder.command(*command).start()
    val out: InputStream = process.inputStream

    val buffer = ByteArray(4096)
    while (process.isAlive) {
        val no: Int = out.available()
        if (no > 0) {
            val n: Int = out.read(buffer, 0, min(no, buffer.size))
            logger.debug { String(buffer, 0, n) }
        }
    }
    ...
    return CommandOutput(exitCode, logs)
}

This outputs correctly: Provide token for coordinate [2, D]:

I've tried numerorus implementations using Okio, but without any luck. My naive implementation looks like this:

...
override fun execute(vararg command: String): CommandOutput {
    val builder = ProcessBuilder()
    builder.redirectErrorStream(true)
    val process = builder.command(*command).start()
    while (process.isAlive) {
        val data = process.inputStream.source().buffer()
        logger.debug { data.readUtf8Line() }
    }
    ...

The above code will hang forever without producing any log-output.

I've studied both unittests in the Okio-project and googled the World Wide Web without any luck. Any help is appreciated. Thanks.

0

There are 0 best solutions below