View Javadoc

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