Search in sources :

Example 1 with LeadPipeInputStream

use of org.apache.tools.ant.util.LeadPipeInputStream in project ant by apache.

the class Redirector method createStreams.

/**
 * Create the input, error and output streams based on the configuration
 * options.
 */
public void createStreams() {
    synchronized (outMutex) {
        outStreams();
        if (alwaysLogOut || outputStream == null) {
            final OutputStream outputLog = new LogOutputStream(managingTask, Project.MSG_INFO);
            outputStream = (outputStream == null) ? outputLog : new TeeOutputStream(outputLog, outputStream);
        }
        if ((outputFilterChains != null && outputFilterChains.size() > 0) || !(outputEncoding.equalsIgnoreCase(inputEncoding))) {
            try {
                final LeadPipeInputStream snk = new LeadPipeInputStream();
                snk.setManagingComponent(managingTask);
                InputStream outPumpIn = snk;
                Reader reader = new InputStreamReader(outPumpIn, inputEncoding);
                if (outputFilterChains != null && outputFilterChains.size() > 0) {
                    final ChainReaderHelper helper = new ChainReaderHelper();
                    helper.setProject(managingTask.getProject());
                    helper.setPrimaryReader(reader);
                    helper.setFilterChains(outputFilterChains);
                    reader = helper.getAssembledReader();
                }
                outPumpIn = new ReaderInputStream(reader, outputEncoding);
                final Thread t = new Thread(threadGroup, new StreamPumper(outPumpIn, outputStream, true), "output pumper");
                t.setPriority(Thread.MAX_PRIORITY);
                outputStream = new PipedOutputStream(snk);
                t.start();
            } catch (final IOException eyeOhEx) {
                throw new BuildException("error setting up output stream", eyeOhEx);
            }
        }
    }
    synchronized (errMutex) {
        errorStreams();
        if (alwaysLogErr || errorStream == null) {
            final OutputStream errorLog = new LogOutputStream(managingTask, Project.MSG_WARN);
            errorStream = (errorStream == null) ? errorLog : new TeeOutputStream(errorLog, errorStream);
        }
        if ((errorFilterChains != null && errorFilterChains.size() > 0) || !(errorEncoding.equalsIgnoreCase(inputEncoding))) {
            try {
                final LeadPipeInputStream snk = new LeadPipeInputStream();
                snk.setManagingComponent(managingTask);
                InputStream errPumpIn = snk;
                Reader reader = new InputStreamReader(errPumpIn, inputEncoding);
                if (errorFilterChains != null && errorFilterChains.size() > 0) {
                    final ChainReaderHelper helper = new ChainReaderHelper();
                    helper.setProject(managingTask.getProject());
                    helper.setPrimaryReader(reader);
                    helper.setFilterChains(errorFilterChains);
                    reader = helper.getAssembledReader();
                }
                errPumpIn = new ReaderInputStream(reader, errorEncoding);
                final Thread t = new Thread(threadGroup, new StreamPumper(errPumpIn, errorStream, true), "error pumper");
                t.setPriority(Thread.MAX_PRIORITY);
                errorStream = new PipedOutputStream(snk);
                t.start();
            } catch (final IOException eyeOhEx) {
                throw new BuildException("error setting up error stream", eyeOhEx);
            }
        }
    }
    synchronized (inMutex) {
        // whatever warnings are needed
        if (input != null && input.length > 0) {
            managingTask.log("Redirecting input from file" + ((input.length == 1) ? "" : "s"), Project.MSG_VERBOSE);
            try {
                inputStream = new ConcatFileInputStream(input);
            } catch (final IOException eyeOhEx) {
                throw new BuildException(eyeOhEx);
            }
            ((ConcatFileInputStream) inputStream).setManagingComponent(managingTask);
        } else if (inputString != null) {
            final StringBuffer buf = new StringBuffer("Using input ");
            if (logInputString) {
                buf.append('"').append(inputString).append('"');
            } else {
                buf.append("string");
            }
            managingTask.log(buf.toString(), Project.MSG_VERBOSE);
            inputStream = new ByteArrayInputStream(inputString.getBytes());
        }
        if (inputStream != null && inputFilterChains != null && inputFilterChains.size() > 0) {
            final ChainReaderHelper helper = new ChainReaderHelper();
            helper.setProject(managingTask.getProject());
            try {
                helper.setPrimaryReader(new InputStreamReader(inputStream, inputEncoding));
            } catch (final IOException eyeOhEx) {
                throw new BuildException("error setting up input stream", eyeOhEx);
            }
            helper.setFilterChains(inputFilterChains);
            inputStream = new ReaderInputStream(helper.getAssembledReader(), inputEncoding);
        }
    }
}
Also used : TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) InputStreamReader(java.io.InputStreamReader) LeadPipeInputStream(org.apache.tools.ant.util.LeadPipeInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.apache.tools.ant.util.ReaderInputStream) ConcatFileInputStream(org.apache.tools.ant.util.ConcatFileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeepAliveOutputStream(org.apache.tools.ant.util.KeepAliveOutputStream) OutputStream(java.io.OutputStream) LazyFileOutputStream(org.apache.tools.ant.util.LazyFileOutputStream) PipedOutputStream(java.io.PipedOutputStream) TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) ConcatFileInputStream(org.apache.tools.ant.util.ConcatFileInputStream) ReaderInputStream(org.apache.tools.ant.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LeadPipeInputStream(org.apache.tools.ant.util.LeadPipeInputStream) ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper) BuildException(org.apache.tools.ant.BuildException)

Aggregations

BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 BuildException (org.apache.tools.ant.BuildException)1 ChainReaderHelper (org.apache.tools.ant.filters.util.ChainReaderHelper)1 ConcatFileInputStream (org.apache.tools.ant.util.ConcatFileInputStream)1 KeepAliveOutputStream (org.apache.tools.ant.util.KeepAliveOutputStream)1 LazyFileOutputStream (org.apache.tools.ant.util.LazyFileOutputStream)1 LeadPipeInputStream (org.apache.tools.ant.util.LeadPipeInputStream)1 ReaderInputStream (org.apache.tools.ant.util.ReaderInputStream)1 TeeOutputStream (org.apache.tools.ant.util.TeeOutputStream)1