KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)TraceInputStream.java 1.7 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 FilterInputStream that writes the bytes
34  * being read from the given input stream into the given output
35  * stream. This class is typically used to provide a trace of
36  * the data that is being retrieved from an input stream.
37  *
38  * @author John Mani
39  */

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

53     public TraceInputStream(InputStream in, OutputStream traceOut) {
54     super(in);
55     this.traceOut = traceOut;
56     }
57
58     /**
59      * Set trace mode.
60      * @param trace the trace mode
61      */

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

70     public void setQuote(boolean quote) {
71     this.quote = quote;
72     }
73
74     /**
75      * Reads the next byte of data from this input stream. Returns
76      * <code>-1</code> if no data is available. Writes out the read
77      * byte into the trace stream, if trace mode is <code>true</code>
78      */

79     public int read() throws IOException {
80     int b = in.read();
81     if (trace && b != -1) {
82         if (quote)
83         writeByte(b);
84         else
85         traceOut.write(b);
86     }
87     return b;
88     }
89
90     /**
91      * Reads up to <code>len</code> bytes of data from this input stream
92      * into an array of bytes. Returns <code>-1</code> if no more data
93      * is available. Writes out the read bytes into the trace stream, if
94      * trace mode is <code>true</code>
95      */

96     public int read(byte b[], int off, int len) throws IOException {
97     int count = in.read(b, off, len);
98     if (trace && count != -1) {
99         if (quote) {
100         for (int i = 0; i < count; i++)
101             writeByte(b[off + i]);
102         } else
103         traceOut.write(b, off, count);
104     }
105     return count;
106     }
107
108     /**
109      * Write a byte in a way that every byte value is printable ASCII.
110      */

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