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.FilterInputStream;
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.OutputStream;
026    
027    import org.apache.geronimo.mail.util.QuotedPrintableEncoderStream;
028    
029    /**
030     * @version $Rev: 668614 $ $Date: 2008-06-17 13:04:26 +0200 (Di, 17. Jun 2008) $
031     */
032    public class TraceInputStream extends FilterInputStream {
033        // the current debug setting
034        protected boolean debug = false;
035    
036        // the target trace output stream.
037        protected OutputStream traceStream;
038    
039        /**
040         * Construct a debug trace stream.
041         * 
042         * @param in
043         *            The source input stream.
044         * @param traceStream
045         *            The side trace stream to which trace data gets written.
046         * @param encode
047         *            Indicates whether we wish the Trace data to be Q-P encoded.
048         */
049        public TraceInputStream(InputStream in, OutputStream traceStream, boolean debug, boolean encode) {
050            super(in);
051            this.debug = debug;
052            if (encode) {
053                this.traceStream = new QuotedPrintableEncoderStream(traceStream);
054            } else {
055                this.traceStream = traceStream;
056            }
057        }
058    
059        /**
060         * Set the current setting of the debug trace stream debug flag.
061         * 
062         * @param d
063         *            The new debug flag settings.
064         */
065        public void setDebug(boolean d) {
066            debug = d;
067        }
068    
069        /**
070         * Reads up to len bytes of data from this input stream, placing them directly 
071         * into the provided byte array. 
072         * 
073         * @param b   the provided data buffer. 
074         * @param off the starting offset within the buffer for placing the data. 
075         * @param len the maximum number of bytes to read. 
076         * @return    that number of bytes that have been read and copied into the 
077         *            buffer or -1 if an end of stream occurs. 
078         * @exception IOException for any I/O errors. 
079         */
080        public int read(byte b[], int off, int len) throws IOException {
081            int count = in.read(b, off, len);
082            if (debug && count > 0) {
083                traceStream.write(b, off, count);
084            }
085            return count;
086        }
087    
088        /**
089         * Read the next byte of data from the input stream, returning it as an 
090         * int value.  Returns -1 if the end of stream is detected. 
091         * 
092         * @return The next byte of data or -1 to indicate end-of-stream.      
093         * @exception IOException for any I/O errors
094         */
095        public int read() throws IOException {
096            int b = in.read();
097            if (debug) {
098                traceStream.write(b);
099            }
100            return b;
101        }
102    }