001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.javamail.util;
021    
022    import java.io.FilterOutputStream;
023    import java.io.IOException;
024    import java.io.OutputStream;
025    
026    import org.apache.geronimo.mail.util.QuotedPrintableEncoderStream;
027    
028    /**
029     * @version $Rev: 668615 $ $Date: 2008-06-17 07:06:18 -0400 (Tue, 17 Jun 2008) $
030     */
031    public class TraceOutputStream extends FilterOutputStream {
032        // the current debug setting
033        protected boolean debug = false;
034    
035        // the target trace output stream.
036        protected OutputStream traceStream;
037    
038        /**
039         * Construct a debug trace stream.
040         *
041         * @param out
042         *            The target out put stream.
043         * @param traceStream
044         *            The side trace stream to which trace data gets written.
045         * @param encode
046         *            Indicates whether we wish the Trace data to be Q-P encoded.
047         */
048        public TraceOutputStream(OutputStream out, OutputStream traceStream, boolean debug, boolean encode) {
049            super(out);
050            this.debug = debug;
051            if (encode) {
052                this.traceStream = new QuotedPrintableEncoderStream(traceStream);
053            } else {
054                this.traceStream = traceStream;
055            }
056        }
057    
058        /**
059         * Set the current setting of the debug trace stream debug flag.
060         *
061         * @param d
062         *            The new debug flag settings.
063         */
064        public void setDebug(boolean d) {
065            debug = d;
066        }
067    
068    
069        /**
070         * Write a single byte to the output stream.
071         * 
072         * @param b      The byte to be written.
073         * 
074         * @exception IOException
075         *                   thrown for any I/O errors.
076         */
077        public void write(int b) throws IOException {
078            if (debug) {
079                traceStream.write(b);
080            }
081            super.write(b);
082        }
083    }