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