FileDocCategorySizeDatePackage
PumpStreamHandler.javaAPI DocApache Ant 1.706739Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs

PumpStreamHandler

public class PumpStreamHandler extends Object implements ExecuteStreamHandler
Copies standard output and error of subprocesses to standard output and error of the parent process.
since
Ant 1.2

Fields Summary
private Thread
outputThread
private Thread
errorThread
private StreamPumper
inputPump
private OutputStream
out
private OutputStream
err
private InputStream
input
Constructors Summary
public PumpStreamHandler(OutputStream out, OutputStream err, InputStream input)
Construct a new PumpStreamHandler.

param
out the output OutputStream.
param
err the error OutputStream.
param
input the input InputStream.

        this.out = out;
        this.err = err;
        this.input = input;
    
public PumpStreamHandler(OutputStream out, OutputStream err)
Construct a new PumpStreamHandler.

param
out the output OutputStream.
param
err the error OutputStream.

        this(out, err, null);
    
public PumpStreamHandler(OutputStream outAndErr)
Construct a new PumpStreamHandler.

param
outAndErr the output/error OutputStream.

        this(outAndErr, outAndErr);
    
public PumpStreamHandler()
Construct a new PumpStreamHandler.

        this(System.out, System.err);
    
Methods Summary
StreamPumpercreateInputPump(java.io.InputStream is, java.io.OutputStream os, boolean closeWhenExhausted)
Creates a stream pumper to copy the given input stream to the given output stream. Used for standard input.

since
Ant 1.6.3

        StreamPumper pumper = new StreamPumper(is, os, closeWhenExhausted);
        pumper.setAutoflush(true);
        return pumper;
    
protected voidcreateProcessErrorPump(java.io.InputStream is, java.io.OutputStream os)
Create the pump to handle error output.

param
is the input stream to copy from.
param
os the output stream to copy to.

        errorThread = createPump(is, os);
    
protected voidcreateProcessOutputPump(java.io.InputStream is, java.io.OutputStream os)
Create the pump to handle process output.

param
is the InputStream.
param
os the OutputStream.

        outputThread = createPump(is, os);
    
protected java.lang.ThreadcreatePump(java.io.InputStream is, java.io.OutputStream os)
Creates a stream pumper to copy the given input stream to the given output stream.

param
is the input stream to copy from.
param
os the output stream to copy to.
return
a thread object that does the pumping.

        return createPump(is, os, false);
    
protected java.lang.ThreadcreatePump(java.io.InputStream is, java.io.OutputStream os, boolean closeWhenExhausted)
Creates a stream pumper to copy the given input stream to the given output stream.

param
is the input stream to copy from.
param
os the output stream to copy to.
param
closeWhenExhausted if true close the inputstream.
return
a thread object that does the pumping.

        final Thread result
            = new Thread(new StreamPumper(is, os, closeWhenExhausted));
        result.setDaemon(true);
        return result;
    
protected java.io.OutputStreamgetErr()
Get the error stream.

return
OutputStream.

        return err;
    
protected java.io.OutputStreamgetOut()
Get the output stream.

return
OutputStream.

        return out;
    
public voidsetProcessErrorStream(java.io.InputStream is)
Set the InputStream from which to read the standard error of the process.

param
is the InputStream.

        if (err != null) {
            createProcessErrorPump(is, err);
        }
    
public voidsetProcessInputStream(java.io.OutputStream os)
Set the OutputStream by means of which input can be sent to the process.

param
os the OutputStream.

        if (input != null) {
            inputPump = createInputPump(input, os, true);
        } else {
            try {
                os.close();
            } catch (IOException e) {
                //ignore
            }
        }
    
public voidsetProcessOutputStream(java.io.InputStream is)
Set the InputStream from which to read the standard output of the process.

param
is the InputStream.

        createProcessOutputPump(is, out);
    
public voidstart()
Start the Threads.

        outputThread.start();
        errorThread.start();
        if (inputPump != null) {
            Thread inputThread = new Thread(inputPump);
            inputThread.setDaemon(true);
            inputThread.start();
        }
    
public voidstop()
Stop pumping the streams.

        try {
            outputThread.join();
        } catch (InterruptedException e) {
            // ignore
        }
        try {
            errorThread.join();
        } catch (InterruptedException e) {
            // ignore
        }

        if (inputPump != null) {
            inputPump.stop();
        }

        try {
            err.flush();
        } catch (IOException e) {
            // ignore
        }
        try {
            out.flush();
        } catch (IOException e) {
            // ignore
        }