📄 redirector.java
字号:
*/ public synchronized void setOutputFilterChains(Vector outputFilterChains) { this.outputFilterChains = outputFilterChains; } /** * Set the error <code>FilterChain</code>s. * * @param errorFilterChains <code>Vector</code> containing <code>FilterChain</code>. */ public synchronized void setErrorFilterChains(Vector errorFilterChains) { this.errorFilterChains = errorFilterChains; } /** * Set a property from a ByteArrayOutputStream * * @param baos contains the property value. * @param propertyName the property name. * * @exception IOException if the value cannot be read form the stream. */ private void setPropertyFromBAOS(ByteArrayOutputStream baos, String propertyName) throws IOException { BufferedReader in = new BufferedReader(new StringReader(Execute.toString(baos))); String line = null; StringBuffer val = new StringBuffer(); while ((line = in.readLine()) != null) { if (val.length() != 0) { val.append(StringUtils.LINE_SEP); } val.append(line); } managingTask.getProject().setNewProperty(propertyName, val.toString()); } /** * Create the input, error and output streams based on the * configuration options. */ public synchronized void createStreams() { outStreams(); errorStreams(); if (alwaysLog || outputStream == null) { OutputStream outputLog = new LogOutputStream(managingTask, Project.MSG_INFO); outputStream = (outputStream == null) ? outputLog : new TeeOutputStream(outputLog, outputStream); } if (alwaysLog || errorStream == null) { OutputStream errorLog = new LogOutputStream(managingTask, Project.MSG_WARN); errorStream = (errorStream == null) ? errorLog : new TeeOutputStream(errorLog, errorStream); } if ((outputFilterChains != null && outputFilterChains.size() > 0) || !(outputEncoding.equalsIgnoreCase(inputEncoding))) { try { LeadPipeInputStream snk = new LeadPipeInputStream(); snk.setManagingComponent(managingTask); InputStream outPumpIn = snk; Reader reader = new InputStreamReader(outPumpIn, inputEncoding); if (outputFilterChains != null && outputFilterChains.size() > 0) { ChainReaderHelper helper = new ChainReaderHelper(); helper.setProject(managingTask.getProject()); helper.setPrimaryReader(reader); helper.setFilterChains(outputFilterChains); reader = helper.getAssembledReader(); } outPumpIn = new ReaderInputStream(reader, outputEncoding); Thread t = new Thread(threadGroup, new StreamPumper( outPumpIn, outputStream, true), "output pumper"); t.setPriority(Thread.MAX_PRIORITY); outputStream = new PipedOutputStream(snk); t.start(); } catch (IOException eyeOhEx) { throw new BuildException( "error setting up output stream", eyeOhEx); } } if ((errorFilterChains != null && errorFilterChains.size() > 0) || !(errorEncoding.equalsIgnoreCase(inputEncoding))) { try { LeadPipeInputStream snk = new LeadPipeInputStream(); snk.setManagingComponent(managingTask); InputStream errPumpIn = snk; Reader reader = new InputStreamReader(errPumpIn, inputEncoding); if (errorFilterChains != null && errorFilterChains.size() > 0) { ChainReaderHelper helper = new ChainReaderHelper(); helper.setProject(managingTask.getProject()); helper.setPrimaryReader(reader); helper.setFilterChains(errorFilterChains); reader = helper.getAssembledReader(); } errPumpIn = new ReaderInputStream(reader, errorEncoding); Thread t = new Thread(threadGroup, new StreamPumper( errPumpIn, errorStream, true), "error pumper"); t.setPriority(Thread.MAX_PRIORITY); errorStream = new PipedOutputStream(snk); t.start(); } catch (IOException eyeOhEx) { throw new BuildException( "error setting up error stream", eyeOhEx); } } // if input files are specified, inputString and inputStream are ignored; // classes that work with redirector attributes can enforce // 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 (IOException eyeOhEx) { throw new BuildException(eyeOhEx); } ((ConcatFileInputStream) inputStream).setManagingComponent(managingTask); } else if (inputString != null) { 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) { ChainReaderHelper helper = new ChainReaderHelper(); helper.setProject(managingTask.getProject()); try { helper.setPrimaryReader( new InputStreamReader(inputStream, inputEncoding)); } catch (IOException eyeOhEx) { throw new BuildException( "error setting up input stream", eyeOhEx); } helper.setFilterChains(inputFilterChains); inputStream = new ReaderInputStream( helper.getAssembledReader(), inputEncoding); } } /** outStreams */ private void outStreams() { if (out != null && out.length > 0) { String logHead = new StringBuffer("Output ").append( ((append) ? "appended" : "redirected")).append( " to ").toString(); outputStream = foldFiles(out, logHead, Project.MSG_VERBOSE); } if (outputProperty != null) { if (baos == null) { baos = new PropertyOutputStream(outputProperty); managingTask.log("Output redirected to property: " + outputProperty, Project.MSG_VERBOSE); } //shield it from being closed by a filtering StreamPumper OutputStream keepAliveOutput = new KeepAliveOutputStream(baos); outputStream = (outputStream == null) ? keepAliveOutput : new TeeOutputStream(outputStream, keepAliveOutput); } else { baos = null; } } private void errorStreams() { if (error != null && error.length > 0) { String logHead = new StringBuffer("Error ").append( ((append) ? "appended" : "redirected")).append( " to ").toString(); errorStream = foldFiles(error, logHead, Project.MSG_VERBOSE); } else if (!(logError || outputStream == null)) { long funnelTimeout = 0L; OutputStreamFunneler funneler = new OutputStreamFunneler(outputStream, funnelTimeout); try { outputStream = funneler.getFunnelInstance(); errorStream = funneler.getFunnelInstance(); } catch (IOException eyeOhEx) { throw new BuildException( "error splitting output/error streams", eyeOhEx); } } if (errorProperty != null) { if (errorBaos == null) { errorBaos = new PropertyOutputStream(errorProperty); managingTask.log("Error redirected to property: " + errorProperty, Project.MSG_VERBOSE); } //shield it from being closed by a filtering StreamPumper OutputStream keepAliveError = new KeepAliveOutputStream(errorBaos); errorStream = (error == null || error.length == 0) ? keepAliveError : new TeeOutputStream(errorStream, keepAliveError); } else { errorBaos = null; } } /** * Create the StreamHandler to use with our Execute instance. * * @return the execute stream handler to manage the input, output and * error streams. * * @throws BuildException if the execute stream handler cannot be created. */ public synchronized ExecuteStreamHandler createHandler() throws BuildException { createStreams(); return new PumpStreamHandler(outputStream, errorStream, inputStream); } /** * Pass output sent to System.out to specified output. * * @param output the data to be output */ protected synchronized void handleOutput(String output) { if (outPrintStream == null) { outPrintStream = new PrintStream(outputStream); } outPrintStream.print(output); } /** * Handle an input request * * @param buffer the buffer into which data is to be read. * @param offset the offset into the buffer at which data is stored. * @param length the amount of data to read * * @return the number of bytes read * * @exception IOException if the data cannot be read */ protected synchronized int handleInput(byte[] buffer, int offset, int length) throws IOException { if (inputStream == null) { return managingTask.getProject().defaultInput(buffer, offset, length); } else { return inputStream.read(buffer, offset, length); } } /** * Process data due to a flush operation. * * @param output the data being flushed. */ protected synchronized void handleFlush(String output) { if (outPrintStream == null) { outPrintStream = new PrintStream(outputStream); } outPrintStream.print(output); outPrintStream.flush(); } /** * Process error output * * @param output the error output data. */ protected synchronized void handleErrorOutput(String output) { if (errorPrintStream == null) { errorPrintStream = new PrintStream(errorStream); } errorPrintStream.print(output); } /** * Handle a flush operation on the error stream * * @param output the error information being flushed. */ protected synchronized void handleErrorFlush(String output) { if (errorPrintStream == null) { errorPrintStream = new PrintStream(errorStream); } errorPrintStream.print(output); } /** * Get the output stream for the redirector * * @return the redirector's output stream or null if no output * has been configured */ public synchronized OutputStream getOutputStream() { return outputStream; } /** * Get the error stream for the redirector * * @return the redirector's error stream or null if no output * has been configured */ public synchronized OutputStream getErrorStream() { return errorStream; } /** * Get the input stream for the redirector * * @return the redirector's input stream or null if no output * has been configured */ public synchronized InputStream getInputStream() { return inputStream; } /** * Complete redirection. * * This operation will close any streams and create any specified * property values. * * @throws IOException if the output properties cannot be read from their * output streams. */ public synchronized void complete() throws IOException { System.out.flush(); System.err.flush(); if (inputStream != null) { inputStream.close(); } outputStream.flush(); outputStream.close(); errorStream.flush(); errorStream.close(); //wait for the StreamPumpers to finish while (threadGroup.activeCount() > 0) { try { managingTask.log("waiting for " + threadGroup.activeCount() + " Threads:", Project.MSG_DEBUG); Thread[] thread = new Thread[threadGroup.activeCount()]; threadGroup.enumerate(thread); for (int i = 0; i < thread.length && thread[i] != null; i++) { try { managingTask.log(thread[i].toString(), Project.MSG_DEBUG); } catch (NullPointerException enPeaEx) { // Ignore exception } } wait(ONE_SECOND); } catch (InterruptedException eyeEx) { // Ignore exception } } setProperties(); inputStream = null; outputStream = null; errorStream = null; outPrintStream = null; errorPrintStream = null; } /** * Notify the <code>Redirector</code> that it is now okay * to set any output and/or error properties. */ public synchronized void setProperties() { if (baos != null) { try { baos.close(); } catch (IOException eyeOhEx) { // Ignore exception } } if (errorBaos != null) { try { errorBaos.close(); } catch (IOException eyeOhEx) { // Ignore exception } } } private OutputStream foldFiles(File[] file, String logHead, int loglevel) { OutputStream result = new LazyFileOutputStream(file[0], append, createEmptyFiles); managingTask.log(logHead + file[0], loglevel); char[] c = new char[logHead.length()]; Arrays.fill(c, ' '); String indent = new String(c); for (int i = 1; i < file.length; i++) { outputStream = new TeeOutputStream(outputStream, new LazyFileOutputStream(file[i], append, createEmptyFiles)); managingTask.log(indent + file[i], loglevel); } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -