donaldp 02/04/25 18:59:33 Added: aut/src/java/org/apache/aut/jprocess StdioRedirector.java Log: Move redirector to jprocess package and out of excalibur.io Revision Changes Path 1.1 jakarta-ant-myrmidon/aut/src/java/org/apache/aut/jprocess/StdioRedirector.java Index: StdioRedirector.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ package org.apache.aut.jprocess; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; import org.apache.avalon.excalibur.io.DemuxInputStream; import org.apache.avalon.excalibur.io.DemuxOutputStream; /** * This is a utility class that makes it easy to install redirecting * streams into the System.in, System.out and System.err streams. * The streams will redirect to streams that are associated with the * current thread. A Stream becomes associated with the current thread * when a user calls one of the bind*() methods or by inheriting the * parent threads stream when thread is initially created. The default * streams will redirect to the standard input, standard output and * standard error of process. * * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> * @version $Revision: 1.1 $ $Date: 2002/04/26 01:59:33 $ */ public final class StdioRedirector { private static final String NOT_INSTALLED_ERR = "StdioRedirector not installed"; private static DemuxInputStream m_input; private static DemuxOutputStream m_output; private static DemuxOutputStream m_error; /** * Private constructor to block instantiation. */ private StdioRedirector() { } /** * Install the redirecting streams into * System.in, System.out and System.err * and hook them up to internal streams. * * @return true if installed, false if already installed */ public static boolean install() throws IOException { if( m_input == System.in ) { //Already installed so lets ignore return false; } m_input = new DemuxInputStream(); m_output = new DemuxOutputStream(); m_error = new DemuxOutputStream(); m_input.bindStream( new FileInputStream( FileDescriptor.in ) ); m_output.bindStream( new FileOutputStream( FileDescriptor.out ) ); m_error.bindStream( new FileOutputStream( FileDescriptor.err ) ); final PrintStream output = new PrintStream( m_output, true ); final PrintStream error = new PrintStream( m_error, true ); System.setIn( m_input ); System.setOut( output ); System.setErr( error ); return true; } /** * Uninstall the redirecting streams and * replace them with streams that point to * FileDescriptor.in, FileDescriptor.out and * FileDescriptor.err. * * @return true if uninstalled, false if already uninstalled */ public static boolean uninstall() throws IOException { if( m_input != System.in ) { //Already uninstalled so lets ignore return false; } final FileInputStream input = new FileInputStream( FileDescriptor.in ); final FileOutputStream output = new FileOutputStream( FileDescriptor.out ); final FileOutputStream error = new FileOutputStream( FileDescriptor.err ); final PrintStream outputStream = new PrintStream( output, true ); final PrintStream errorStream = new PrintStream( error, true ); System.setIn( input ); System.setOut( outputStream ); System.setErr( errorStream ); return true; } /** * Bind the specified stream to standard input in this thread. * * @param input the stream to bind * @throws SecurityException if don't have permission to bind to input * @throws IllegalStateException if redirector not installed */ public static void bindInput( final InputStream input ) throws SecurityException, IllegalStateException { if( null == input ) { throw new NullPointerException( "input" ); } if( null == m_input ) { throw new IllegalStateException( NOT_INSTALLED_ERR ); } final SecurityManager manager = System.getSecurityManager(); if( null != manager ) { final RuntimePermission permission = new RuntimePermission( "StdioRedirector.setIn" ); manager.checkPermission( permission ); } m_input.bindStream( input ); } /** * Bind the specified stream to standard output in this thread. * * @param output the stream to bind * @throws SecurityException if don't have permission to bind to output * @throws IllegalStateException if redirector not installed */ public static void bindOutput( final OutputStream output ) throws SecurityException, IllegalStateException { if( null == output ) { throw new NullPointerException( "output" ); } if( null == m_output ) { throw new IllegalStateException( NOT_INSTALLED_ERR ); } final SecurityManager manager = System.getSecurityManager(); if( null != manager ) { final RuntimePermission permission = new RuntimePermission( "StdioRedirector.setOut" ); manager.checkPermission( permission ); } m_output.bindStream( output ); } /** * Bind the specified stream to standard error in this thread. * * @param error the stream to bind * @throws SecurityException if don't have permission to bind to error * @throws IllegalStateException if redirector not installed */ public static void bindError( final OutputStream error ) throws SecurityException, IllegalStateException { if( null == error ) { throw new NullPointerException( "error" ); } if( null == m_error ) { throw new IllegalStateException( NOT_INSTALLED_ERR ); } final SecurityManager manager = System.getSecurityManager(); if( null != manager ) { final RuntimePermission permission = new RuntimePermission( "StdioRedirector.setErr" ); manager.checkPermission( permission ); } m_error.bindStream( error ); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
