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