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.OutputStream;
23  
24  /**
25   * Encoder for RFC1891 xtext.
26   *
27   * xtext strings are defined as
28   *
29   *    xtext = *( xchar / hexchar )
30   *
31   * where
32   *
33   *    xchar is any ASCII character in the range 33-126, EXCEPT
34   *    the characters "+" and "=".
35   *
36   *    hexchar is an ASCII "+" followed by two upper case
37   *    hexadecimal digits.
38   */
39  public class XText
40  {
41      private static final Encoder encoder = new XTextEncoder();
42  
43      /**
44       * encode the input data producing an xtext  encoded byte array.
45       *
46       * @return a byte array containing the xtext encoded data.
47       */
48      public static byte[] encode(
49          byte[]    data)
50      {
51          return encode(data, 0, data.length);
52      }
53  
54      /**
55       * encode the input data producing an xtext encoded byte array.
56       *
57       * @return a byte array containing the xtext encoded data.
58       */
59      public static byte[] encode(
60          byte[]    data,
61          int       off,
62          int       length)
63      {
64          ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
65  
66          try
67          {
68              encoder.encode(data, off, length, bOut);
69          }
70          catch (IOException e)
71          {
72              throw new RuntimeException("exception encoding xtext string: " + e);
73          }
74  
75          return bOut.toByteArray();
76      }
77  
78      /**
79       * xtext encode the byte data writing it to the given output stream.
80       *
81       * @return the number of bytes produced.
82       */
83      public static int encode(
84          byte[]         data,
85          OutputStream   out)
86          throws IOException
87      {
88          return encoder.encode(data, 0, data.length, out);
89      }
90  
91      /**
92       * extext encode the byte data writing it to the given output stream.
93       *
94       * @return the number of bytes produced.
95       */
96      public static int encode(
97          byte[]         data,
98          int            off,
99          int            length,
100         OutputStream   out)
101         throws IOException
102     {
103         return encoder.encode(data, 0, data.length, out);
104     }
105 
106     /**
107      * decode the xtext encoded input data. It is assumed the input data is valid.
108      *
109      * @return a byte array representing the decoded data.
110      */
111     public static byte[] decode(
112         byte[]    data)
113     {
114         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
115 
116         try
117         {
118             encoder.decode(data, 0, data.length, bOut);
119         }
120         catch (IOException e)
121         {
122             throw new RuntimeException("exception decoding xtext string: " + e);
123         }
124 
125         return bOut.toByteArray();
126     }
127 
128     /**
129      * decode the xtext encoded String data - whitespace will be ignored.
130      *
131      * @return a byte array representing the decoded data.
132      */
133     public static byte[] decode(
134         String    data)
135     {
136         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
137 
138         try
139         {
140             encoder.decode(data, bOut);
141         }
142         catch (IOException e)
143         {
144             throw new RuntimeException("exception decoding xtext string: " + e);
145         }
146 
147         return bOut.toByteArray();
148     }
149 
150     /**
151      * decode the xtext encoded String data writing it to the given output stream,
152      * whitespace characters will be ignored.
153      *
154      * @return the number of bytes produced.
155      */
156     public static int decode(
157         String          data,
158         OutputStream    out)
159         throws IOException
160     {
161         return encoder.decode(data, out);
162     }
163 }
164