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