001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.mail.util;
019    
020    import java.io.IOException;
021    import java.io.OutputStream;
022    import java.io.FilterOutputStream;
023    
024    /**
025     * An implementation of a FilterOutputStream that encodes the
026     * stream data in Q-P encoding format.  This version does the
027     * encoding "on the fly" rather than encoding a single block of
028     * data.  Since this version is intended for use by the MimeUtilty class,
029     * it also handles line breaks in the encoded data.
030     */
031    public class QuotedPrintableEncoderStream extends FilterOutputStream {
032        // our hex encoder utility class.
033        protected QuotedPrintableEncoder encoder;
034    
035        // our default for line breaks
036        protected static final int DEFAULT_LINEBREAK = 76;
037    
038        // the instance line break value
039        protected int lineBreak;
040    
041        /**
042         * Create a Base64 encoder stream that wraps a specifed stream
043         * using the default line break size.
044         *
045         * @param out    The wrapped output stream.
046         */
047        public QuotedPrintableEncoderStream(OutputStream out) {
048            this(out, DEFAULT_LINEBREAK);
049        }
050    
051    
052        public QuotedPrintableEncoderStream(OutputStream out, int lineBreak) {
053            super(out);
054            // lines are processed only in multiple of 4, so round this down.
055            this.lineBreak = (lineBreak / 4) * 4 ;
056    
057            // create an encoder configured to this amount
058            encoder = new QuotedPrintableEncoder(out, this.lineBreak);
059        }
060    
061    
062        public void write(int ch) throws IOException {
063            // have the encoder do the heavy lifting here.
064            encoder.encode(ch);
065        }
066    
067        public void write(byte [] data) throws IOException {
068            write(data, 0, data.length);
069        }
070    
071        public void write(byte [] data, int offset, int length) throws IOException {
072            // the encoder does the heavy lifting here.
073            encoder.encode(data, offset, length);
074        }
075    
076        public void close() throws IOException {
077            out.close();
078        }
079    
080        public void flush() throws IOException {
081            out.flush();
082        }
083    }
084    
085