001package org.codehaus.plexus.util.cli;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStream;
006import java.io.InputStreamReader;
007import java.io.PrintWriter;
008
009import org.codehaus.plexus.util.IOUtil;
010
011/**
012 * Class to pump the error stream during Process's runtime. Copied from the Ant built-in task.
013 */
014public class StreamPumper extends AbstractStreamHandler {
015
016        private final BufferedReader in;
017        private final StreamConsumer consumer;
018        private final PrintWriter out;
019        private volatile Exception exception = null;
020        private static final int SIZE = 1024;
021
022        public StreamPumper(InputStream in) {
023                this(in, (StreamConsumer) null);
024        }
025
026        public StreamPumper(InputStream in, StreamConsumer consumer) {
027                this(in, null, consumer);
028        }
029
030        public StreamPumper(InputStream in, PrintWriter writer) {
031                this(in, writer, null);
032        }
033
034        public StreamPumper(InputStream in, PrintWriter writer, StreamConsumer consumer) {
035                this.in = new BufferedReader(new InputStreamReader(in), SIZE);
036                this.out = writer;
037                this.consumer = consumer;
038        }
039
040        @Override
041        public void run() {
042                try {
043                        for (String line = in.readLine(); line != null; line = in.readLine()) {
044                                try {
045                                        if (exception == null) {
046                                                consumeLine(line);
047                                        }
048                                } catch (Exception t) {
049                                        exception = t;
050                                }
051
052                                if (out != null) {
053                                        out.println(line);
054                                        out.flush();
055                                }
056                        }
057                } catch (IOException e) {
058                        exception = e;
059                } finally {
060                        IOUtil.close(in);
061                        synchronized (this) {
062                                setDone();
063                                this.notifyAll();
064                        }
065                }
066        }
067
068        public void flush() {
069                if (out != null) {
070                        out.flush();
071                }
072        }
073
074        public void close() {
075                IOUtil.close(out);
076        }
077
078        public Exception getException() {
079                return exception;
080        }
081
082        private void consumeLine(String line) {
083                if (consumer != null && !isDisabled()) {
084                        consumer.consumeLine(line);
085                }
086        }
087}