001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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.util.encoders;
019    
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    import java.io.OutputStream;
023    
024    /**
025     * Convert binary data to and from UrlBase64 encoding.  This is identical to
026     * Base64 encoding, except that the padding character is "." and the other
027     * non-alphanumeric characters are "-" and "_" instead of "+" and "/".
028     * <p>
029     * The purpose of UrlBase64 encoding is to provide a compact encoding of binary
030     * data that is safe for use as an URL parameter. Base64 encoding does not
031     * produce encoded values that are safe for use in URLs, since "/" can be
032     * interpreted as a path delimiter; "+" is the encoded form of a space; and
033     * "=" is used to separate a name from the corresponding value in an URL
034     * parameter.
035     */
036    public class UrlBase64
037    {
038        private static final Encoder encoder = new UrlBase64Encoder();
039    
040        /**
041         * Encode the input data producing a URL safe base 64 encoded byte array.
042         *
043         * @return a byte array containing the URL safe base 64 encoded data.
044         */
045        public static byte[] encode(
046            byte[]    data)
047        {
048            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
049    
050            try
051            {
052                encoder.encode(data, 0, data.length, bOut);
053            }
054            catch (IOException e)
055            {
056                throw new RuntimeException("exception encoding URL safe base64 string: " + e.getMessage(), e);
057            }
058    
059            return bOut.toByteArray();
060        }
061    
062        /**
063         * Encode 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         * Decode the URL safe base 64 encoded input data - white space will be ignored.
077         *
078         * @return a byte array representing the decoded data.
079         */
080        public static byte[] decode(
081            byte[]    data)
082        {
083            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
084    
085            try
086            {
087                encoder.decode(data, 0, data.length, bOut);
088            }
089            catch (IOException e)
090            {
091                throw new RuntimeException("exception decoding URL safe base64 string: " + e.getMessage(), e);
092            }
093    
094            return bOut.toByteArray();
095        }
096    
097        /**
098         * decode the URL safe base 64 encoded byte data writing it to the given output stream,
099         * whitespace characters will be ignored.
100         *
101         * @return the number of bytes produced.
102         */
103        public static int decode(
104            byte[]                data,
105            OutputStream    out)
106            throws IOException
107        {
108            return encoder.decode(data, 0, data.length, out);
109        }
110    
111        /**
112         * decode the URL safe base 64 encoded String data - whitespace will be ignored.
113         *
114         * @return a byte array representing the decoded data.
115         */
116        public static byte[] decode(
117            String    data)
118        {
119            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
120    
121            try
122            {
123                encoder.decode(data, bOut);
124            }
125            catch (IOException e)
126            {
127                throw new RuntimeException("exception decoding URL safe base64 string: " + e.getMessage(), e);
128            }
129    
130            return bOut.toByteArray();
131        }
132    
133        /**
134         * Decode the URL safe base 64 encoded String data writing it to the given output stream,
135         * whitespace characters will be ignored.
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    }