001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  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, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.encoders;
020    
021    import java.io.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.io.OutputStream;
024    
025    public class Base64
026    {
027        private static final Encoder encoder = new Base64Encoder();
028    
029        /**
030         * encode the input data producing a base 64 encoded byte array.
031         *
032         * @return a byte array containing the base 64 encoded data.
033         */
034        public static byte[] encode(
035            byte[]    data)
036        {
037            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
038    
039            try
040            {
041                encoder.encode(data, 0, data.length, bOut);
042            }
043            catch (IOException e)
044            {
045                throw new RuntimeException("exception encoding base64 string: " + e);
046            }
047    
048            return bOut.toByteArray();
049        }
050    
051        /**
052         * Encode the byte data to base 64 writing it to the given output stream.
053         *
054         * @return the number of bytes produced.
055         */
056        public static int encode(
057            byte[]                data,
058            OutputStream    out)
059            throws IOException
060        {
061            return encoder.encode(data, 0, data.length, out);
062        }
063    
064        /**
065         * Encode the byte data to base 64 writing it to the given output stream.
066         *
067         * @return the number of bytes produced.
068         */
069        public static int encode(
070            byte[]                data,
071            int                    off,
072            int                    length,
073            OutputStream    out)
074            throws IOException
075        {
076            return encoder.encode(data, off, length, out);
077        }
078    
079        /**
080         * decode the base 64 encoded input data. It is assumed the input data is valid.
081         *
082         * @return a byte array representing the decoded data.
083         */
084        public static byte[] decode(
085            byte[]    data)
086        {
087            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
088    
089            try
090            {
091                encoder.decode(data, 0, data.length, bOut);
092            }
093            catch (IOException e)
094            {
095                throw new RuntimeException("exception decoding base64 string: " + e);
096            }
097    
098            return bOut.toByteArray();
099        }
100    
101        /**
102         * decode the base 64 encoded String data - whitespace will be ignored.
103         *
104         * @return a byte array representing the decoded data.
105         */
106        public static byte[] decode(
107            String    data)
108        {
109            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
110    
111            try
112            {
113                encoder.decode(data, bOut);
114            }
115            catch (IOException e)
116            {
117                throw new RuntimeException("exception decoding base64 string: " + e);
118            }
119    
120            return bOut.toByteArray();
121        }
122    
123        /**
124         * decode the base 64 encoded String data writing it to the given output stream,
125         * whitespace characters will be ignored.
126         *
127         * @return the number of bytes produced.
128         */
129        public static int decode(
130            String                data,
131            OutputStream    out)
132            throws IOException
133        {
134            return encoder.decode(data, out);
135        }
136    }