001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: OutputLoop.java,v $
023   Revision 1.7  2004/05/05 21:36:35  markl
024   comment block updates
025
026   Revision 1.6  2003/01/19 09:36:58  markl
027   Removed deprecated calls.
028
029   Revision 1.5  2001/03/12 06:01:18  markl
030   Javadoc cleanup.
031
032   Revision 1.4  2001/03/12 01:58:42  markl
033   Source code cleanup.
034
035   Revision 1.3  1999/10/05 02:44:39  markl
036   Added setActive() method.
037
038   Revision 1.2  1999/01/10 03:34:00  markl
039   added GPL header & RCS tag
040   ----------------------------------------------------------------------------
041*/
042
043package kiwi.io;
044
045import java.io.*;
046
047/** A class for redirecting a program's standard output stream back into
048  * itself. An output loop can be used to redirect the output of
049  * <code>println()</code> methods to a graphical console or log file.
050  *
051  * @author Mark Lindner
052  */
053
054public class OutputLoop
055  {
056  private PipedInputStream pipein;
057  private PipedOutputStream pipeout;
058  private PrintStream oldout, newout;
059
060  /** Construct a new <code>OutputLoop</code>.
061    *
062    * @exception java.io.IOException If an error occurred while creating the
063    * pipe.
064    */
065
066  public OutputLoop() throws IOException
067    {
068    pipeout = new PipedOutputStream();
069    pipein = new PipedInputStream(pipeout);
070
071    oldout = System.out;
072    newout = new PrintStream(pipeout); // un-deprecated in JDK 1.2
073    }
074
075  /** Get the stream from which output can be read. */
076
077  public InputStream getInputStream()
078    {
079    return(pipein);
080    }
081
082  /** Turn the loop on or off. While the loop is on, standard output is
083   * directed to the loop; if it is off, it's directed to the console.
084   *
085   * @param active A flag specifying whether the loop should be turned on
086   * or off.
087   */
088
089  public void setActive(boolean active)
090    {
091    if(active)
092      System.setOut(newout);
093    else
094      System.setOut(oldout);
095    }
096
097  /** Dispose of the loop. Turns the loop off, destroys the pipe, and
098    * reconnects the standard output stream to the console.
099    */
100
101  public void dispose()
102    {
103    setActive(false);
104    
105    try
106      {
107      pipein.close();
108      pipeout.close();
109      }
110    catch(IOException e)
111      {
112      }
113    finally
114      {
115      pipein = null;
116      pipeout = null;
117      }
118    }
119  
120  }
121
122/* end of source file */