https://kotlinlang.org logo
l

Leon K

02/27/2020, 1:33 PM
hey! I have a
Process
that gives me both
inputStream
and
errorStream
(
stdout
and
stderr
respectively), and I need to log the output's of both as soon as they come in. what's the best way to handle that? I guess just doing
inputStream.bufferedReader().forEachLine {...}
would block and thus not allow stderr to be handled, thus blocking the process Any ideas?
s

spand

02/27/2020, 1:53 PM
Use two threads
l

Leon K

02/27/2020, 1:56 PM
is that the only option? no input-stream combination stuff or something?
s

spand

02/27/2020, 2:00 PM
There is no async io on it so yes.
A crazy workaround would be to use ProcessBuilder redirectTo to two files and read from those async.
Not sure what the problem with two threads are though.
d

Dennis

02/27/2020, 3:48 PM
NuProcess (github) seems to be an option
Also for fun I wrote a coroutine that polls InputStream::available() and returns a flow. Though this is polling and you release the thread via delay. Not optimal either
5 Views