001    /**
002     *
003     * Copyright 2003-2006 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    public class UUEncode {
025        private static final Encoder encoder = new UUEncoder();
026    
027        /**
028         * encode the input data producing a UUEncoded byte array.
029         *
030         * @return a byte array containing the UUEncoded data.
031         */
032        public static byte[] encode(
033            byte[]    data)
034        {
035            return encode(data, 0, data.length);
036        }
037    
038        /**
039         * encode the input data producing a UUEncoded byte array.
040         *
041         * @return a byte array containing the UUEncoded data.
042         */
043        public static byte[] encode(
044            byte[]    data,
045            int       off,
046            int       length)
047        {
048            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
049    
050            try
051            {
052                encoder.encode(data, off, length, bOut);
053            }
054            catch (IOException e)
055            {
056                throw new RuntimeException("exception encoding UUEncoded string: " + e);
057            }
058    
059            return bOut.toByteArray();
060        }
061    
062        /**
063         * UUEncode the byte data writing it to the given output stream.
064         *
065         * @return the number of bytes produced.
066         */
067        public static int encode(
068            byte[]         data,
069            OutputStream   out)
070            throws IOException
071        {
072            return encoder.encode(data, 0, data.length, out);
073        }
074    
075        /**
076         * UUEncode the byte data writing it to the given output stream.
077         *
078         * @return the number of bytes produced.
079         */
080        public static int encode(
081            byte[]         data,
082            int            off,
083            int            length,
084            OutputStream   out)
085            throws IOException
086        {
087            return encoder.encode(data, 0, data.length, out);
088        }
089    
090        /**
091         * decode the UUEncoded input data. It is assumed the input data is valid.
092         *
093         * @return a byte array representing the decoded data.
094         */
095        public static byte[] decode(
096            byte[]    data)
097        {
098            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
099    
100            try
101            {
102                encoder.decode(data, 0, data.length, bOut);
103            }
104            catch (IOException e)
105            {
106                throw new RuntimeException("exception decoding UUEncoded string: " + e);
107            }
108    
109            return bOut.toByteArray();
110        }
111    
112        /**
113         * decode the UUEncided String data.
114         *
115         * @return a byte array representing the decoded data.
116         */
117        public static byte[] decode(
118            String    data)
119        {
120            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
121    
122            try
123            {
124                encoder.decode(data, bOut);
125            }
126            catch (IOException e)
127            {
128                throw new RuntimeException("exception decoding UUEncoded string: " + e);
129            }
130    
131            return bOut.toByteArray();
132        }
133    
134        /**
135         * decode the UUEncoded encoded String data writing it to the given output stream.
136         *
137         * @return the number of bytes produced.
138         */
139        public static int decode(
140            String          data,
141            OutputStream    out)
142            throws IOException
143        {
144            return encoder.decode(data, out);
145        }
146    }
147