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.mail.util;
021    
022    import java.io.IOException;
023    import java.io.OutputStream;
024    import java.io.FilterOutputStream;
025    
026    /**
027     * An implementation of a FilterOutputStream that encodes the
028     * stream data in Q-P encoding format.  This version does the
029     * encoding "on the fly" rather than encoding a single block of
030     * data.  Since this version is intended for use by the MimeUtilty class,
031     * it also handles line breaks in the encoded data.
032     *
033     * @version $Rev: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $
034     */
035    public class QuotedPrintableEncoderStream extends FilterOutputStream {
036        // our hex encoder utility class.
037        protected QuotedPrintableEncoder encoder;
038    
039        // our default for line breaks
040        protected static final int DEFAULT_LINEBREAK = 76;
041    
042        // the instance line break value
043        protected int lineBreak;
044    
045        /**
046         * Create a Base64 encoder stream that wraps a specifed stream
047         * using the default line break size.
048         *
049         * @param out    The wrapped output stream.
050         */
051        public QuotedPrintableEncoderStream(OutputStream out) {
052            this(out, DEFAULT_LINEBREAK);
053        }
054    
055    
056        public QuotedPrintableEncoderStream(OutputStream out, int lineBreak) {
057            super(out);
058            // lines are processed only in multiple of 4, so round this down.
059            this.lineBreak = (lineBreak / 4) * 4 ;
060    
061            // create an encoder configured to this amount
062            encoder = new QuotedPrintableEncoder(out, this.lineBreak);
063        }
064    
065    
066        public void write(int ch) throws IOException {
067            // have the encoder do the heavy lifting here.
068            encoder.encode(ch);
069        }
070    
071        public void write(byte [] data) throws IOException {
072            write(data, 0, data.length);
073        }
074    
075        public void write(byte [] data, int offset, int length) throws IOException {
076            // the encoder does the heavy lifting here.
077            encoder.encode(data, offset, length);
078        }
079    
080        public void close() throws IOException {
081            out.close();
082        }
083    
084        public void flush() throws IOException {
085            out.flush();
086        }
087    }
088    
089