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.james.mime4j.codec;
021    
022    import java.io.FilterOutputStream;
023    import java.io.IOException;
024    import java.io.OutputStream;
025    
026    /**
027     * Performs Quoted-Printable encoding on an underlying stream.
028     */
029    public class QuotedPrintableOutputStream extends FilterOutputStream {
030        
031        private QuotedPrintableEncoder encoder;
032        private boolean closed = false;
033    
034        public QuotedPrintableOutputStream(OutputStream out, boolean binary) {
035            super(out);
036            encoder = new QuotedPrintableEncoder(CodecUtil.DEFAULT_ENCODING_BUFFER_SIZE, binary);
037            encoder.initEncoding(out);
038        }
039    
040        @Override
041        public void close() throws IOException {
042            if (closed)
043                return;
044    
045            try {
046                encoder.completeEncoding();
047                // do not close the wrapped stream
048            } finally {
049                closed = true;
050            }
051        }
052    
053        @Override
054        public void flush() throws IOException {
055            encoder.flushOutput();
056        }
057    
058        @Override
059        public void write(int b) throws IOException {
060            this.write(new byte[] { (byte) b }, 0, 1);
061        }
062    
063        @Override
064        public void write(byte[] b, int off, int len) throws IOException {
065            if (closed) {
066                throw new IOException("QuotedPrintableOutputStream has been closed");
067            }
068    
069            encoder.encodeChunk(b, off, len);
070        }
071    
072    }