View Javadoc

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.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import org.apache.geronimo.mail.util.QuotedPrintableEncoderStream;
25  
26  /**
27   * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
28   */
29  public class TraceOutputStream extends FilterOutputStream {
30      // the current debug setting
31      protected boolean debug = false;
32  
33      // the target trace output stream.
34      protected OutputStream traceStream;
35  
36      /**
37       * Construct a debug trace stream.
38       *
39       * @param out
40       *            The target out put stream.
41       * @param traceStream
42       *            The side trace stream to which trace data gets written.
43       * @param encode
44       *            Indicates whether we wish the Trace data to be Q-P encoded.
45       */
46      public TraceOutputStream(OutputStream out, OutputStream traceStream, boolean debug, boolean encode) {
47          super(out);
48          this.debug = debug;
49          if (encode) {
50              this.traceStream = new QuotedPrintableEncoderStream(traceStream);
51          } else {
52              this.traceStream = traceStream;
53          }
54      }
55  
56      /**
57       * Set the current setting of the debug trace stream debug flag.
58       *
59       * @param d
60       *            The new debug flag settings.
61       */
62      public void setDebug(boolean d) {
63          debug = d;
64      }
65  
66  
67      /**
68       * Writes the specified <code>byte</code> to this output stream.
69       * <p>
70       * The <code>write</code> method of <code>FilterOutputStream</code>
71       * calls the <code>write</code> method of its underlying output stream,
72       * that is, it performs <tt>out.write(b)</tt>.
73       * <p>
74       * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
75       *
76       * @param b
77       *            the <code>byte</code>.
78       * @exception IOException
79       *                if an I/O error occurs.
80       */
81      public void write(int b) throws IOException {
82          if (debug) {
83              traceStream.write(b);
84          }
85          super.write(b);
86      }
87  }