View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.javamail.util;
21  
22  import java.io.FilterInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  import org.apache.geronimo.mail.util.QuotedPrintableEncoderStream;
28  
29  /**
30   * @version $Rev: 668614 $ $Date: 2008-06-17 07:04:26 -0400 (Tue, 17 Jun 2008) $
31   */
32  public class TraceInputStream extends FilterInputStream {
33      // the current debug setting
34      protected boolean debug = false;
35  
36      // the target trace output stream.
37      protected OutputStream traceStream;
38  
39      /**
40       * Construct a debug trace stream.
41       * 
42       * @param in
43       *            The source input stream.
44       * @param traceStream
45       *            The side trace stream to which trace data gets written.
46       * @param encode
47       *            Indicates whether we wish the Trace data to be Q-P encoded.
48       */
49      public TraceInputStream(InputStream in, OutputStream traceStream, boolean debug, boolean encode) {
50          super(in);
51          this.debug = debug;
52          if (encode) {
53              this.traceStream = new QuotedPrintableEncoderStream(traceStream);
54          } else {
55              this.traceStream = traceStream;
56          }
57      }
58  
59      /**
60       * Set the current setting of the debug trace stream debug flag.
61       * 
62       * @param d
63       *            The new debug flag settings.
64       */
65      public void setDebug(boolean d) {
66          debug = d;
67      }
68  
69      /**
70       * Reads up to len bytes of data from this input stream, placing them directly 
71       * into the provided byte array. 
72       * 
73       * @param b   the provided data buffer. 
74       * @param off the starting offset within the buffer for placing the data. 
75       * @param len the maximum number of bytes to read. 
76       * @return    that number of bytes that have been read and copied into the 
77       *            buffer or -1 if an end of stream occurs. 
78       * @exception IOException for any I/O errors. 
79       */
80      public int read(byte b[], int off, int len) throws IOException {
81          int count = in.read(b, off, len);
82          if (debug && count > 0) {
83              traceStream.write(b, off, count);
84          }
85          return count;
86      }
87  
88      /**
89       * Read the next byte of data from the input stream, returning it as an 
90       * int value.  Returns -1 if the end of stream is detected. 
91       * 
92       * @return The next byte of data or -1 to indicate end-of-stream.      
93       * @exception IOException for any I/O errors
94       */
95      public int read() throws IOException {
96          int b = in.read();
97          if (debug) {
98              traceStream.write(b);
99          }
100         return b;
101     }
102 }