1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.geronimo.mail.util;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.io.FilterOutputStream;
25
26 /**
27 * An implementation of a FilterOutputStream that encodes the
28 * stream data in Q-P encoding format. This version does the
29 * encoding "on the fly" rather than encoding a single block of
30 * data. Since this version is intended for use by the MimeUtilty class,
31 * it also handles line breaks in the encoded data.
32 */
33 public class QuotedPrintableEncoderStream extends FilterOutputStream {
34 // our hex encoder utility class.
35 protected QuotedPrintableEncoder encoder;
36
37 // our default for line breaks
38 protected static final int DEFAULT_LINEBREAK = 76;
39
40 // the instance line break value
41 protected int lineBreak;
42
43 /**
44 * Create a Base64 encoder stream that wraps a specifed stream
45 * using the default line break size.
46 *
47 * @param out The wrapped output stream.
48 */
49 public QuotedPrintableEncoderStream(OutputStream out) {
50 this(out, DEFAULT_LINEBREAK);
51 }
52
53
54 public QuotedPrintableEncoderStream(OutputStream out, int lineBreak) {
55 super(out);
56 // lines are processed only in multiple of 4, so round this down.
57 this.lineBreak = (lineBreak / 4) * 4 ;
58
59 // create an encoder configured to this amount
60 encoder = new QuotedPrintableEncoder(out, this.lineBreak);
61 }
62
63
64 public void write(int ch) throws IOException {
65 // have the encoder do the heavy lifting here.
66 encoder.encode(ch);
67 }
68
69 public void write(byte [] data) throws IOException {
70 write(data, 0, data.length);
71 }
72
73 public void write(byte [] data, int offset, int length) throws IOException {
74 // the encoder does the heavy lifting here.
75 encoder.encode(data, offset, length);
76 }
77
78 public void close() throws IOException {
79 out.close();
80 }
81
82 public void flush() throws IOException {
83 out.flush();
84 }
85 }
86
87