Skip to main content

Can I demultiplex streams?



I want to join stderr ( getErrorStream ) and stdout ( getInputStream ) of a Process into a single Stream to be consumed elsewhere. Is there anything in Java's library that will do that for me?





Note: no external libraries . I'm not interested in the existence of a solution provided by, say, Apache Commons IO. I only want to know if there's something that comes with JDK.


Comments

  1. You can use a SequenceInputStream to merge the 2 InputStream.

    ReplyDelete
  2. ProcessBuilder.redirectErrorStream(boolean) does what you want.


    public ProcessBuilder redirectErrorStream(boolean redirectErrorStream)

    Sets this process builder's redirectErrorStream property.

    If this property is true, then any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.


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

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?