/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer: Florent BENOIT
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.examples.util;

import java.io.PrintStream;

/**
 * Allow to redirect the System.out stream or System.err
 * @author Florent Benoit
 */
public class JPrintStream extends PrintStream {


    /**
     * Old PrintStream
     */
    protected PrintStream printStream = null;


    /**
     * Text which was sent to this stream
     */
    protected StringBuffer buffer = null;

    /**
     * Constructor. Build a new JPrintStream.
     * @param printStream stream to use
     */
    public JPrintStream(PrintStream printStream) {
        super(printStream);
        this.buffer = new StringBuffer();
        this.printStream = printStream;
        System.setOut(this);
    }

    /**
     * Return the buffer which is used
     * @return buffer which is used
     */
    public StringBuffer getStringBuffer() {
        return buffer;
    }


    /**
     * Print to the stream and log it
     * @param x text to log
     */
    public void println(String x) {
        buffer.append(x);
        // Don't print info
        //printStream.println(x);
    }

    /**
     * Print to the stream and log it
     * @param x text to log
     */
    public void println(Object x) {
        buffer.append(x.toString());
    }

    /**
     * Remove all the information logged
     */
    public void reset() {
        buffer = new StringBuffer();
    }

    /**
     * Set the stream back
     */
    public void remove() {
        System.setOut(printStream);
    }
}
