001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.mail.util;
019
020 import java.io.ByteArrayOutputStream;
021 import java.io.IOException;
022 import java.io.OutputStream;
023
024 /**
025 * Encoder for RFC1891 xtext.
026 *
027 * xtext strings are defined as
028 *
029 * xtext = *( xchar / hexchar )
030 *
031 * where
032 *
033 * xchar is any ASCII character in the range 33-126, EXCEPT
034 * the characters "+" and "=".
035 *
036 * hexchar is an ASCII "+" followed by two upper case
037 * hexadecimal digits.
038 */
039 public class XText
040 {
041 private static final Encoder encoder = new XTextEncoder();
042
043 /**
044 * encode the input data producing an xtext encoded byte array.
045 *
046 * @return a byte array containing the xtext encoded data.
047 */
048 public static byte[] encode(
049 byte[] data)
050 {
051 return encode(data, 0, data.length);
052 }
053
054 /**
055 * encode the input data producing an xtext encoded byte array.
056 *
057 * @return a byte array containing the xtext encoded data.
058 */
059 public static byte[] encode(
060 byte[] data,
061 int off,
062 int length)
063 {
064 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
065
066 try
067 {
068 encoder.encode(data, off, length, bOut);
069 }
070 catch (IOException e)
071 {
072 throw new RuntimeException("exception encoding xtext string: " + e);
073 }
074
075 return bOut.toByteArray();
076 }
077
078 /**
079 * xtext encode the byte data writing it to the given output stream.
080 *
081 * @return the number of bytes produced.
082 */
083 public static int encode(
084 byte[] data,
085 OutputStream out)
086 throws IOException
087 {
088 return encoder.encode(data, 0, data.length, out);
089 }
090
091 /**
092 * extext encode the byte data writing it to the given output stream.
093 *
094 * @return the number of bytes produced.
095 */
096 public static int encode(
097 byte[] data,
098 int off,
099 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