01: /*
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 1999 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or 1any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * Initial developer: Florent BENOIT
22: * --------------------------------------------------------------------------
23: * $Id: JPrintStream.java 3038 2003-08-18 07:28:00Z benoitf $
24: * --------------------------------------------------------------------------
25: */
26:
27: package org.objectweb.jonas.examples.util;
28:
29: import java.io.PrintStream;
30:
31: /**
32: * Allow to redirect the System.out stream or System.err
33: * @author Florent Benoit
34: */
35: public class JPrintStream extends PrintStream {
36:
37: /**
38: * Old PrintStream
39: */
40: protected PrintStream printStream = null;
41:
42: /**
43: * Text which was sent to this stream
44: */
45: protected StringBuffer buffer = null;
46:
47: /**
48: * Constructor. Build a new JPrintStream.
49: * @param printStream stream to use
50: */
51: public JPrintStream(PrintStream printStream) {
52: super (printStream);
53: this .buffer = new StringBuffer();
54: this .printStream = printStream;
55: System.setOut(this );
56: }
57:
58: /**
59: * Return the buffer which is used
60: * @return buffer which is used
61: */
62: public StringBuffer getStringBuffer() {
63: return buffer;
64: }
65:
66: /**
67: * Print to the stream and log it
68: * @param x text to log
69: */
70: public void println(String x) {
71: buffer.append(x);
72: // Don't print info
73: //printStream.println(x);
74: }
75:
76: /**
77: * Print to the stream and log it
78: * @param x text to log
79: */
80: public void println(Object x) {
81: buffer.append(x.toString());
82: }
83:
84: /**
85: * Remove all the information logged
86: */
87: public void reset() {
88: buffer = new StringBuffer();
89: }
90:
91: /**
92: * Set the stream back
93: */
94: public void remove() {
95: System.setOut(printStream);
96: }
97: }
|