package se.artcomputer; import java.io.*; /** * Keeps track of a process and echoes its standard output and error * to a given output stream. * @author Per Lundholm ArtComputer * @version 1.0 $Date: 2002/03/15 23:18:16 $ */ public class ProcessTracker { /** Tracks a process until end of file is received. The output from the process, stdout and stderr, is interleaved to the given ouput. Returns when the process ends or if it receives an InterruptedException. */ public static void track(Process inProc, OutputStream inOut) throws IOException { try { new Thread(new StreamTracker(inProc.getInputStream(), inOut)).start(); new Thread(new StreamTracker(inProc.getErrorStream(), inOut)).start(); inProc.waitFor(); } catch (InterruptedException e) { ; } } /** Echoes the output from one stream into another. */ static class StreamTracker implements Runnable { private InputStream iIn; private OutputStream iOut; /** Setup the streams. */ StreamTracker(InputStream inIn, OutputStream inOut) { iIn = inIn; iOut = inOut; } /** Start echoing, proceed until eof. */ public void run() { try { int lInCh = 0; while (lInCh != -1) { lInCh = iIn.read(); iOut.write(lInCh); } } catch (IOException e) { ; // thats it } } } }