View Javadoc

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.james.mime4j.codec;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  /**
27   * Utility methods related to codecs.
28   */
29  public class CodecUtil {
30      
31      static final int DEFAULT_ENCODING_BUFFER_SIZE = 1024;
32      
33      /**
34       * Copies the contents of one stream to the other.
35       * @param in not null
36       * @param out not null
37       * @throws IOException
38       */
39      public static void copy(final InputStream in, final OutputStream out) throws IOException {
40          final byte[] buffer = new byte[DEFAULT_ENCODING_BUFFER_SIZE];
41          int inputLength;
42          while (-1 != (inputLength = in.read(buffer))) {
43              out.write(buffer, 0, inputLength);
44          }
45      }
46      
47      /**
48       * Encodes the given stream using Quoted-Printable.
49       * This assumes that stream is binary and therefore escapes
50       * all line endings.
51       * @param in not null
52       * @param out not null
53       * @throws IOException
54       */
55      public static void encodeQuotedPrintableBinary(final InputStream in, final OutputStream out) throws IOException {
56          
57          QuotedPrintableEncoder encoder = new QuotedPrintableEncoder(DEFAULT_ENCODING_BUFFER_SIZE, true);
58          encoder.encode(in, out);
59      }
60      
61      /**
62       * Encodes the given stream using Quoted-Printable.
63       * This assumes that stream is text and therefore does not escape
64       * all line endings.
65       * @param in not null
66       * @param out not null
67       * @throws IOException
68       */
69      public static void encodeQuotedPrintable(final InputStream in, final OutputStream out) throws IOException {
70          final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder(DEFAULT_ENCODING_BUFFER_SIZE, false);
71          encoder.encode(in, out);
72      }
73      
74      /**
75       * Encodes the given stream using base64.
76       *
77       * @param in not null
78       * @param out not null
79       * @throws IOException if an I/O error occurs
80       */
81      public static void encodeBase64(final InputStream in, final OutputStream out) throws IOException {
82          Base64OutputStream b64Out = new Base64OutputStream(out);
83          copy(in, b64Out);
84          b64Out.close();
85      }
86      
87      /**
88       * Wraps the given stream in a Quoted-Printable encoder.
89       * @param out not null
90       * @return encoding outputstream 
91       * @throws IOException
92       */
93      public static OutputStream wrapQuotedPrintable(final OutputStream out, boolean binary) throws IOException {
94          return new QuotedPrintableOutputStream(out, binary);
95      }
96      
97      /**
98       * Wraps the given stream in a Base64 encoder.
99       * @param out not null
100      * @return encoding outputstream 
101      * @throws IOException
102      */
103     public static OutputStream wrapBase64(final OutputStream out) throws IOException {
104         return new Base64OutputStream(out);
105     }
106 
107 }