1 /**
2 *
3 * Copyright 2006 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.javamail.util;
19
20 import java.io.FilterInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import org.apache.geronimo.mail.util.QuotedPrintableEncoderStream;
26
27 /**
28 * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
29 */
30 public class TraceInputStream extends FilterInputStream {
31
32 protected boolean debug = false;
33
34
35 protected OutputStream traceStream;
36
37 /**
38 * Construct a debug trace stream.
39 *
40 * @param in
41 * The source input stream.
42 * @param traceStream
43 * The side trace stream to which trace data gets written.
44 * @param encode
45 * Indicates whether we wish the Trace data to be Q-P encoded.
46 */
47 public TraceInputStream(InputStream in, OutputStream traceStream, boolean debug, boolean encode) {
48 super(in);
49 this.debug = debug;
50 if (encode) {
51 this.traceStream = new QuotedPrintableEncoderStream(traceStream);
52 } else {
53 this.traceStream = traceStream;
54 }
55 }
56
57 /**
58 * Set the current setting of the debug trace stream debug flag.
59 *
60 * @param d
61 * The new debug flag settings.
62 */
63 public void setDebug(boolean d) {
64 debug = d;
65 }
66
67 /**
68 * Reads up to <code>len</code> bytes of data from this input stream into
69 * an array of bytes. This method blocks until some input is available.
70 * <p>
71 * This method simply performs <code>in.read(b, off, len)</code> and
72 * returns the result.
73 *
74 * @param b
75 * the buffer into which the data is read.
76 * @param off
77 * the start offset of the data.
78 * @param len
79 * the maximum number of bytes read.
80 * @return the total number of bytes read into the buffer, or
81 * <code>-1</code> if there is no more data because the end of the
82 * stream has been reached.
83 * @exception IOException
84 * if an I/O error occurs.
85 * @see java.io.FilterInputStream#in
86 */
87 public int read(byte b[], int off, int len) throws IOException {
88 int count = in.read(b, off, len);
89 if (debug && count > 0) {
90 traceStream.write(b, off, count);
91 }
92 return count;
93 }
94
95 /**
96 * Reads the next byte of data from this input stream. The value byte is
97 * returned as an <code>int</code> in the range <code>0</code> to
98 * <code>255</code>. If no byte is available because the end of the
99 * stream has been reached, the value <code>-1</code> is returned. This
100 * method blocks until input data is available, the end of the stream is
101 * detected, or an exception is thrown.
102 * <p>
103 * This method simply performs <code>in.read()</code> and returns the
104 * result.
105 *
106 * @return the next byte of data, or <code>-1</code> if the end of the
107 * stream is reached.
108 * @exception IOException
109 * if an I/O error occurs.
110 * @see java.io.FilterInputStream#in
111 */
112 public int read() throws IOException {
113 int b = in.read();
114 if (debug) {
115 traceStream.write(b);
116 }
117 return b;
118 }
119 }