FileDocCategorySizeDatePackage
StreamPumper.javaAPI DocApache Ant 1.705183Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

StreamPumper

public class StreamPumper extends Object implements Runnable
Copies all data from an input stream to an output stream.
since
Ant 1.2

Fields Summary
private InputStream
is
private OutputStream
os
private volatile boolean
finish
private volatile boolean
finished
private boolean
closeWhenExhausted
private boolean
autoflush
private Exception
exception
private int
bufferSize
private boolean
started
Constructors Summary
public StreamPumper(InputStream is, OutputStream os, boolean closeWhenExhausted)
Create a new stream pumper.

param
is input stream to read data from
param
os output stream to write data to.
param
closeWhenExhausted if true, the output stream will be closed when the input is exhausted.


                                                    
        
                          
        this.is = is;
        this.os = os;
        this.closeWhenExhausted = closeWhenExhausted;
    
public StreamPumper(InputStream is, OutputStream os)
Create a new stream pumper.

param
is input stream to read data from
param
os output stream to write data to.

        this(is, os, false);
    
Methods Summary
public synchronized intgetBufferSize()
Get the size in bytes of the read buffer.

return
the int size of the read buffer.

        return bufferSize;
    
public synchronized java.lang.ExceptiongetException()
Get the exception encountered, if any.

return
the Exception encountered.

        return exception;
    
public booleanisFinished()
Tells whether the end of the stream has been reached.

return
true is the stream has been exhausted.

        return finished;
    
public voidrun()
Copies data from the input stream to the output stream. Terminates as soon as the input stream is closed or an error occurs.

        synchronized (this) {
            started = true;
        }
        finished = false;
        finish = false;

        final byte[] buf = new byte[bufferSize];

        int length;
        try {
            while ((length = is.read(buf)) > 0 && !finish) {
                os.write(buf, 0, length);
                if (autoflush) {
                    os.flush();
                }
            }
            os.flush();
        } catch (Exception e) {
            synchronized (this) {
                exception = e;
            }
        } finally {
            if (closeWhenExhausted) {
                try {
                    os.close();
                } catch (IOException e) {
                    // ignore
                }
            }
            finished = true;
            synchronized (this) {
                notifyAll();
            }
        }
    
voidsetAutoflush(boolean autoflush)
Set whether data should be flushed through to the output stream.

param
autoflush if true, push through data; if false, let it be buffered
since
Ant 1.6.3

        this.autoflush = autoflush;
    
public synchronized voidsetBufferSize(int bufferSize)
Set the size in bytes of the read buffer.

param
bufferSize the buffer size to use.
throws
IllegalStateException if the StreamPumper is already running.

        if (started) {
            throw new IllegalStateException(
                "Cannot set buffer size on a running StreamPumper");
        }
        this.bufferSize = bufferSize;
    
synchronized voidstop()
Stop the pumper as soon as possible. Note that it may continue to block on the input stream but it will really stop the thread as soon as it gets EOF or any byte, and it will be marked as finished.

since
Ant 1.6.3

        finish = true;
        notifyAll();
    
public synchronized voidwaitFor()
This method blocks until the stream pumper finishes.

throws
InterruptedException if interrupted.
see
#isFinished()

        while (!isFinished()) {
            wait();
        }