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