Can I demultiplex streams?

I want to join stderr ( getErrorStream ) and stdout ( getInputStream ) Process in one Stream , which will be used in another place. Is there anything in the Java library that will do this for me?

Note: no external libraries. I am not interested in having a solution provided, for example, by Apache Commons IO. I just want to know if there is something that comes with the JDK.

+6
source share
3 answers

ProcessBuilder.redirectErrorStream(boolean) does what you want.

public ProcessBuilder redirectErrorStream(boolean redirectErrorStream)

Sets the linker property of the redirectErrorStream process.

If this property is true , then any error output generated by subprocesses subsequently triggered by this start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This simplifies the correlation of error messages with the corresponding output. The initial value is false .

EDIT: @Since Java 5 or later should be widely available.

+1
source

You can use SequenceInputStream to combine 2 InputStream.

0
source

No, nothing happens in the JDK (before 6, at 7 I have not looked at all classes yet).

0
source

Source: https://habr.com/ru/post/908160/


All Articles