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.geronimo.mail.util;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  public class QuotedPrintable {
28      // NOTE:  the QuotedPrintableEncoder class needs to keep some active state about what's going on with
29      // respect to line breaks and significant white spaces.  This makes it difficult to keep one static
30      // instance of the decode around for reuse.
31  
32  
33      /**
34       * encode the input data producing a Q-P encoded byte array.
35       *
36       * @return a byte array containing the Q-P encoded data.
37       */
38      public static byte[] encode(
39          byte[]    data)
40      {
41          return encode(data, 0, data.length);
42      }
43  
44      /**
45       * encode the input data producing a Q-P encoded byte array.
46       *
47       * @return a byte array containing the Q-P encoded data.
48       */
49      public static byte[] encode(
50          byte[]    data,
51          int       off,
52          int       length)
53      {
54          QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
55  
56          ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
57  
58          try
59          {
60              encoder.encode(data, off, length, bOut);
61          }
62          catch (IOException e)
63          {
64              throw new RuntimeException("exception encoding Q-P encoded string: " + e);
65          }
66  
67          return bOut.toByteArray();
68      }
69  
70      /**
71       * Q-P encode the byte data writing it to the given output stream.
72       *
73       * @return the number of bytes produced.
74       */
75      public static int encode(
76          byte[]         data,
77          OutputStream   out)
78          throws IOException
79      {
80          QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
81  
82          return encoder.encode(data, 0, data.length, out);
83      }
84  
85      /**
86       * Q-P encode the byte data writing it to the given output stream.
87       *
88       * @return the number of bytes produced.
89       */
90      public static int encode(
91          byte[]         data,
92          int            off,
93          int            length,
94          OutputStream   out)
95          throws IOException
96      {
97          QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
98          return encoder.encode(data, 0, data.length, out);
99      }
100 
101     /**
102      * decode the Q-P encoded input data. It is assumed the input data is valid.
103      *
104      * @return a byte array representing the decoded data.
105      */
106     public static byte[] decode(
107         byte[]    data)
108     {
109         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
110 
111         QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
112         try
113         {
114             encoder.decode(data, 0, data.length, bOut);
115         }
116         catch (IOException e)
117         {
118             throw new RuntimeException("exception decoding Q-P encoded string: " + e);
119         }
120 
121         return bOut.toByteArray();
122     }
123 
124     /**
125      * decode the UUEncided String data.
126      *
127      * @return a byte array representing the decoded data.
128      */
129     public static byte[] decode(
130         String    data)
131     {
132         QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
133         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
134 
135         try
136         {
137             encoder.decode(data, bOut);
138         }
139         catch (IOException e)
140         {
141             throw new RuntimeException("exception decoding Q-P encoded string: " + e);
142         }
143 
144         return bOut.toByteArray();
145     }
146 
147     /**
148      * decode the Q-P encoded encoded String data writing it to the given output stream.
149      *
150      * @return the number of bytes produced.
151      */
152     public static int decode(
153         String          data,
154         OutputStream    out)
155         throws IOException
156     {
157         QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
158         return encoder.decode(data, out);
159     }
160 
161     /**
162      * decode the base Q-P encoded String data writing it to the given output stream,
163      * whitespace characters will be ignored.
164      *
165      * @param data   The array data to decode.
166      * @param out    The output stream for the data.
167      *
168      * @return the number of bytes produced.
169      * @exception IOException
170      */
171     public static int decode(byte [] data, OutputStream out) throws IOException
172     {
173         QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
174         return encoder.decode(data, 0, data.length, out);
175     }
176 }
177