KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > io > Tee


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * @version 1.00 April 1, 2000
26  * @author Byron Nevins
27  */

28
29 package com.sun.enterprise.util.io;
30
31 import java.io.*;
32 //import java.util.*;
33
//import java.util.jar.*;
34
//import java.util.zip.*;
35

36
37 public class Tee extends PrintStream
38 {
39
40     /////////////////////////////////////////////////////////////////
41

42     private Tee(PrintStream ps)
43     {
44         super(ps);
45     }
46
47
48     /////////////////////////////////////////////////////////////////
49

50     // Starts copying stdout and
51
//stderr to the file f.
52
public static void start(String JavaDoc f) throws IOException
53     {
54         // Save old settings.
55
oldStdout = System.out;
56         oldStderr = System.err;
57
58         // Create/Open logfile.
59
logfile = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)));
60
61         // Start redirecting the output.
62
System.setOut(new Tee(System.out));
63         System.setErr(new Tee(System.err));
64     }
65
66
67     /////////////////////////////////////////////////////////////////
68

69     // Restores the original settings.
70
public static void stop()
71     {
72         System.setOut(oldStdout);
73         System.setErr(oldStderr);
74         
75         try
76         {
77             logfile.close();
78         }
79         catch (Exception JavaDoc e)
80         {
81             e.printStackTrace();
82         }
83     }
84
85
86     /////////////////////////////////////////////////////////////////
87

88     // PrintStream override.
89
public void write(int b)
90     {
91         try
92         {
93             logfile.write(b);
94         }
95         catch (Exception JavaDoc e)
96         {
97             e.printStackTrace();
98             setError();
99         }
100         super.write(b);
101     }
102
103     /////////////////////////////////////////////////////////////////
104

105     // PrintStream override.
106
public void write(byte buf[], int off, int len)
107     {
108         try
109         {
110             logfile.write(buf, off, len);
111         }
112         catch (Exception JavaDoc e)
113         {
114             e.printStackTrace();
115             setError();
116         }
117         super.write(buf, off, len);
118     }
119
120     /////////////////////////////////////////////////////////////////
121

122     static OutputStream logfile;
123     static PrintStream oldStdout;
124     static PrintStream oldStderr;
125
126     /////////////////////////////////////////////////////////////////
127

128     public static void main(String JavaDoc[] args)
129     {
130         try
131         {
132             // Start capturing characters
133
//into the log file.
134
Tee.start("log.txt");
135
136             // Test it.
137
System.out.println(
138             "Here's is some stuff to stdout.");
139             System.err.println(
140             "Here's is some stuff to stderr.");
141             System.out.println(
142             "Let's throw an exception...");
143             new Exception JavaDoc().printStackTrace();
144         }
145         catch (Exception JavaDoc e)
146         {
147             e.printStackTrace();
148         }
149         finally
150         {
151             // Stop capturing characters
152
//into the log file
153
// and restore old setup.
154
Tee.stop();
155         }
156     }
157 }
158
Popular Tags