KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > util > TraceOutputStream


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)TraceOutputStream.java 1.5 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.util;
29
30 import java.io.*;
31
32 /**
33  * This class is a subclass of DataOutputStream that copies the
34  * data being written into the DataOutputStream into another output
35  * stream. This class is used here to provide a debug trace of the
36  * stuff thats being written out into the DataOutputStream.
37  *
38  * @author John Mani
39  */

40
41 public class TraceOutputStream extends FilterOutputStream {
42     private boolean trace = false;
43     private boolean quote = false;
44     private OutputStream traceOut;
45
46     /**
47      * Creates an output stream filter built on top of the specified
48      * underlying output stream.
49      *
50      * @param out the underlying output stream.
51      * @param traceOut the trace stream.
52      */

53     public TraceOutputStream(OutputStream out, OutputStream traceOut) {
54     super(out);
55     this.traceOut = traceOut;
56     }
57
58     /**
59      * Set the trace mode.
60      */

61     public void setTrace(boolean trace) {
62     this.trace = trace;
63     }
64
65     /**
66      * Set quote mode.
67      * @param quote the quote mode
68      */

69     public void setQuote(boolean quote) {
70     this.quote = quote;
71     }
72
73     /**
74      * Writes the specified <code>byte</code> to this output stream.
75      * Writes out the byte into the trace stream if the trace mode
76      * is <code>true</code>
77      */

78     public void write(int b) throws IOException {
79     if (trace) {
80         if (quote)
81         writeByte(b);
82         else
83         traceOut.write(b);
84     }
85     out.write(b);
86     }
87         
88     /**
89      * Writes <code>b.length</code> bytes to this output stream.
90      * Writes out the bytes into the trace stream if the trace
91      * mode is <code>true</code>
92      */

93     public void write(byte b[], int off, int len) throws IOException {
94     if (trace) {
95         if (quote) {
96         for (int i = 0; i < len; i++)
97             writeByte(b[off + i]);
98         } else
99         traceOut.write(b, off, len);
100     }
101     out.write(b, off, len);
102     }
103
104     /**
105      * Write a byte in a way that every byte value is printable ASCII.
106      */

107     private final void writeByte(int b) throws IOException {
108     b &= 0xff;
109     if (b > 0x7f) {
110         traceOut.write('M');
111         traceOut.write('-');
112         b &= 0x7f;
113     }
114     if (b == '\r') {
115         traceOut.write('\\');
116         traceOut.write('r');
117     } else if (b == '\n') {
118         traceOut.write('\\');
119         traceOut.write('n');
120         traceOut.write('\n');
121     } else if (b == '\t') {
122         traceOut.write('\\');
123         traceOut.write('t');
124     } else if (b < ' ') {
125         traceOut.write('^');
126         traceOut.write('@' + b);
127     } else {
128         traceOut.write(b);
129     }
130     }
131 }
132
Popular Tags