KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > examples > util > JPrintStream


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * 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,v 1.1 2003/08/18 07:28:00 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.examples.util;
28
29 import java.io.PrintStream JavaDoc;
30
31 /**
32  * Allow to redirect the System.out stream or System.err
33  * @author Florent Benoit
34  */

35 public class JPrintStream extends PrintStream JavaDoc {
36
37
38     /**
39      * Old PrintStream
40      */

41     protected PrintStream JavaDoc printStream = null;
42
43
44     /**
45      * Text which was sent to this stream
46      */

47     protected StringBuffer JavaDoc buffer = null;
48
49     /**
50      * Constructor. Build a new JPrintStream.
51      * @param printStream stream to use
52      */

53     public JPrintStream(PrintStream JavaDoc printStream) {
54         super(printStream);
55         this.buffer = new StringBuffer JavaDoc();
56         this.printStream = printStream;
57         System.setOut(this);
58     }
59
60     /**
61      * Return the buffer which is used
62      * @return buffer which is used
63      */

64     public StringBuffer JavaDoc getStringBuffer() {
65         return buffer;
66     }
67
68
69     /**
70      * Print to the stream and log it
71      * @param x text to log
72      */

73     public void println(String JavaDoc x) {
74         buffer.append(x);
75         // Don't print info
76
//printStream.println(x);
77
}
78
79     /**
80      * Print to the stream and log it
81      * @param x text to log
82      */

83     public void println(Object JavaDoc x) {
84         buffer.append(x.toString());
85     }
86
87     /**
88      * Remove all the information logged
89      */

90     public void reset() {
91         buffer = new StringBuffer JavaDoc();
92     }
93
94     /**
95      * Set the stream back
96      */

97     public void remove() {
98         System.setOut(printStream);
99     }
100 }
101
Popular Tags