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 public class QuotedPrintableEncoderStream extends FilterOutputStream {
034 // our hex encoder utility class.
035 protected QuotedPrintableEncoder encoder;
036
037 // our default for line breaks
038 protected static final int DEFAULT_LINEBREAK = 76;
039
040 // the instance line break value
041 protected int lineBreak;
042
043 /**
044 * Create a Base64 encoder stream that wraps a specifed stream
045 * using the default line break size.
046 *
047 * @param out The wrapped output stream.
048 */
049 public QuotedPrintableEncoderStream(OutputStream out) {
050 this(out, DEFAULT_LINEBREAK);
051 }
052
053
054 public QuotedPrintableEncoderStream(OutputStream out, int lineBreak) {
055 super(out);
056 // lines are processed only in multiple of 4, so round this down.
057 this.lineBreak = (lineBreak / 4) * 4 ;
058
059 // create an encoder configured to this amount
060 encoder = new QuotedPrintableEncoder(out, this.lineBreak);
061 }
062
063
064 public void write(int ch) throws IOException {
065 // have the encoder do the heavy lifting here.
066 encoder.encode(ch);
067 }
068
069 public void write(byte [] data) throws IOException {
070 write(data, 0, data.length);
071 }
072
073 public void write(byte [] data, int offset, int length) throws IOException {
074 // the encoder does the heavy lifting here.
075 encoder.encode(data, offset, length);
076 }
077
078 public void close() throws IOException {
079 out.close();
080 }
081
082 public void flush() throws IOException {
083 out.flush();
084 }
085 }
086
087