📄 redirector.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs;import java.io.File;import java.io.Reader;import java.io.InputStream;import java.io.IOException;import java.io.PrintStream;import java.io.OutputStream;import java.io.StringReader;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PipedOutputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.Arrays;import java.util.Vector;import org.apache.tools.ant.Project;import org.apache.tools.ant.ProjectComponent;import org.apache.tools.ant.Task;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.filters.util.ChainReaderHelper;import org.apache.tools.ant.util.StringUtils;import org.apache.tools.ant.util.TeeOutputStream;import org.apache.tools.ant.util.ReaderInputStream;import org.apache.tools.ant.util.LeadPipeInputStream;import org.apache.tools.ant.util.LazyFileOutputStream;import org.apache.tools.ant.util.OutputStreamFunneler;import org.apache.tools.ant.util.ConcatFileInputStream;import org.apache.tools.ant.util.KeepAliveOutputStream;/** * The Redirector class manages the setup and connection of * input and output redirection for an Ant project component. * * @since Ant 1.6 */public class Redirector { private static final int ONE_SECOND = 1000; private static final String DEFAULT_ENCODING = System.getProperty("file.encoding"); private class PropertyOutputStream extends ByteArrayOutputStream { private String property; private boolean closed = false; PropertyOutputStream(String property) { super(); this.property = property; } public void close() throws IOException { if (!closed && !(append && appendProperties)) { setPropertyFromBAOS(this, property); closed = true; } } } /** * The file(s) from which standard input is being taken. * If > 1, files' content will be concatenated in the order received. */ private File[] input; /** * The file(s) receiving standard output. Will also receive standard error * unless standard error is redirected or logError is true. */ private File[] out; /** * The file(s) to which standard error is being redirected */ private File[] error; /** * Indicates if standard error should be logged to Ant's log system * rather than the output. This has no effect if standard error is * redirected to a file or property. */ private boolean logError = false; /** * Buffer used to capture output for storage into a property */ private PropertyOutputStream baos = null; /** * Buffer used to capture error output for storage into a property */ private PropertyOutputStream errorBaos = null; /** The name of the property into which output is to be stored */ private String outputProperty; /** The name of the property into which error output is to be stored */ private String errorProperty; /** String from which input is taken */ private String inputString; /** Flag which indicates if error and output files are to be appended. */ private boolean append = false; /** Flag which indicates that output should be always sent to the log */ private boolean alwaysLog = false; /** Flag which indicates whether files should be created even when empty. */ private boolean createEmptyFiles = true; /** The task for which this redirector is working */ private ProjectComponent managingTask; /** The stream for output data */ private OutputStream outputStream = null; /** The stream for error output */ private OutputStream errorStream = null; /** The stream for input */ private InputStream inputStream = null; /** Stream which is used for line oriented output */ private PrintStream outPrintStream = null; /** Stream which is used for line oriented error output */ private PrintStream errorPrintStream = null; /** The output filter chains */ private Vector outputFilterChains; /** The error filter chains */ private Vector errorFilterChains; /** The input filter chains */ private Vector inputFilterChains; /** The output encoding */ private String outputEncoding = DEFAULT_ENCODING; /** The error encoding */ private String errorEncoding = DEFAULT_ENCODING; /** The input encoding */ private String inputEncoding = DEFAULT_ENCODING; /** Whether to complete properties settings **/ private boolean appendProperties = true; /** The thread group used for starting <code>StreamPumper</code> threads */ private ThreadGroup threadGroup = new ThreadGroup("redirector"); /** whether to log the inputstring */ private boolean logInputString = true; /** * Create a redirector instance for the given task * * @param managingTask the task for which the redirector is to work */ public Redirector(Task managingTask) { this((ProjectComponent) managingTask); } /** * Create a redirector instance for the given task * * @param managingTask the project component for which the * redirector is to work * @since Ant 1.6.3 */ public Redirector(ProjectComponent managingTask) { this.managingTask = managingTask; } /** * Set the input to use for the task * * @param input the file from which input is read. */ public void setInput(File input) { setInput((input == null) ? null : new File[] {input}); } /** * Set the input to use for the task * * @param input the files from which input is read. */ public synchronized void setInput(File[] input) { this.input = input; } /** * Set the string to use as input * * @param inputString the string which is used as the input source */ public synchronized void setInputString(String inputString) { this.inputString = inputString; } /** * Set whether to include the value of the input string in log messages. * Defaults to true. * @param logInputString true or false. * @since Ant 1.7 */ public void setLogInputString(boolean logInputString) { this.logInputString = logInputString; } /** * Set a stream to use as input. * * @param inputStream the stream from which input will be read * @since Ant 1.6.3 */ /*public*/ void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } /** * File the output of the process is redirected to. If error is not * redirected, it too will appear in the output * * @param out the file to which output stream is written */ public void setOutput(File out) { setOutput((out == null) ? null : new File[] {out}); } /** * Files the output of the process is redirected to. If error is not * redirected, it too will appear in the output * * @param out the files to which output stream is written */ public synchronized void setOutput(File[] out) { this.out = out; } /** * Set the output encoding. * * @param outputEncoding <code>String</code>. */ public synchronized void setOutputEncoding(String outputEncoding) { if (outputEncoding == null) { throw new IllegalArgumentException( "outputEncoding must not be null"); } else { this.outputEncoding = outputEncoding; } } /** * Set the error encoding. * * @param errorEncoding <code>String</code>. */ public synchronized void setErrorEncoding(String errorEncoding) { if (errorEncoding == null) { throw new IllegalArgumentException( "errorEncoding must not be null"); } else { this.errorEncoding = errorEncoding; } } /** * Set the input encoding. * * @param inputEncoding <code>String</code>. */ public synchronized void setInputEncoding(String inputEncoding) { if (inputEncoding == null) { throw new IllegalArgumentException( "inputEncoding must not be null"); } else { this.inputEncoding = inputEncoding; } } /** * Controls whether error output of exec is logged. This is only useful * when output is being redirected and error output is desired in the * Ant log * * @param logError if true the standard error is sent to the Ant log system * and not sent to output. */ public synchronized void setLogError(boolean logError) { this.logError = logError; } /** * This <code>Redirector</code>'s subordinate * <code>PropertyOutputStream</code>s will not set their respective * properties <code>while (appendProperties && append)</code>. * * @param appendProperties whether to append properties. */ public synchronized void setAppendProperties(boolean appendProperties) { this.appendProperties = appendProperties; } /** * Set the file to which standard error is to be redirected. * * @param error the file to which error is to be written */ public void setError(File error) { setError((error == null) ? null : new File[] {error}); } /** * Set the files to which standard error is to be redirected. * * @param error the file to which error is to be written */ public synchronized void setError(File[] error) { this.error = error; } /** * Property name whose value should be set to the output of * the process. * * @param outputProperty the name of the property to be set with the * task's output. */ public synchronized void setOutputProperty(String outputProperty) { if (outputProperty == null || !(outputProperty.equals(this.outputProperty))) { this.outputProperty = outputProperty; baos = null; } } /** * Whether output should be appended to or overwrite an existing file. * Defaults to false. * * @param append if true output and error streams are appended to their * respective files, if specified. */ public synchronized void setAppend(boolean append) { this.append = append; } /** * If true, (error and non-error) output will be "teed", redirected * as specified while being sent to Ant's logging mechanism as if no * redirection had taken place. Defaults to false. * @param alwaysLog <code>boolean</code> * @since Ant 1.6.3 */ public synchronized void setAlwaysLog(boolean alwaysLog) { this.alwaysLog = alwaysLog; } /** * Whether output and error files should be created even when empty. * Defaults to true. * @param createEmptyFiles <code>boolean</code>. */ public synchronized void setCreateEmptyFiles(boolean createEmptyFiles) { this.createEmptyFiles = createEmptyFiles; } /** * Property name whose value should be set to the error of * the process. * * @param errorProperty the name of the property to be set * with the error output. */ public synchronized void setErrorProperty(String errorProperty) { if (errorProperty == null || !(errorProperty.equals(this.errorProperty))) { this.errorProperty = errorProperty; errorBaos = null; } } /** * Set the input <code>FilterChain</code>s. * * @param inputFilterChains <code>Vector</code> containing <code>FilterChain</code>. */ public synchronized void setInputFilterChains(Vector inputFilterChains) { this.inputFilterChains = inputFilterChains; } /** * Set the output <code>FilterChain</code>s. * * @param outputFilterChains <code>Vector</code> containing <code>FilterChain</code>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -