1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Copyright 2001-2004 The Apache Software Foundation.
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * you may not use this file except in compliance with the License.
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * You may obtain a copy of the License at
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * See the License for the specific language governing permissions and
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * limitations under the License.
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.commons.codec.net;
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.UnsupportedEncodingException;
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.DecoderException;
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.EncoderException;
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.StringDecoder;
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.StringEncoder;
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.binary.Base64;
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Identical to the Base64 encoding defined by <a href="http://www.ietf.org/rfc/rfc1521.txt">RFC
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 1521</a> and allows a character set to be specified.
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * </p>
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> describes techniques to allow the encoding of non-ASCII
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * handling software.
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * </p>
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: Message
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *          Header Extensions for Non-ASCII Text</a>
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Apache Software Foundation
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 1.3
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @version $Id: BCodec.java,v 1.5 2004/04/13 22:46:37 ggregory Exp $
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The default charset used for string decoding and encoding.
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private String charset = StringEncodings.UTF8;
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Default constructor.
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public BCodec() {
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Constructor which allows for the selection of a default charset
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param charset
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the default string charset to use.
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *          encoding names</a>
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public BCodec(final String charset) {
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.charset = charset;
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected String getEncoding() {
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return "B";
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected byte[] doEncoding(byte[] bytes) throws EncoderException {
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (bytes == null) {
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return Base64.encodeBase64(bytes);
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    protected byte[] doDecoding(byte[] bytes) throws DecoderException {
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (bytes == null) {
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return Base64.decodeBase64(bytes);
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Encodes a string into its Base64 form using the specified charset. Unsafe characters are escaped.
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  string to convert to Base64 form
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param charset
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  the charset for pString
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return Base64 string
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws EncoderException
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  thrown if a failure condition is encountered during the encoding process.
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String encode(final String value, final String charset) throws EncoderException {
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (value == null) {
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return encodeText(value, charset);
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } catch (UnsupportedEncodingException e) {
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new EncoderException(e.getMessage());
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Encodes a string into its Base64 form using the default charset. Unsafe characters are escaped.
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  string to convert to Base64 form
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return Base64 string
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws EncoderException
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  thrown if a failure condition is encountered during the encoding process.
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String encode(String value) throws EncoderException {
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (value == null) {
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return encode(value, getDefaultCharset());
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a Base64 string into its original form. Escaped characters are converted back to their original
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * representation.
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  Base64 string to convert into its original form
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return original string
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws DecoderException
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  A decoder exception is thrown if a failure condition is encountered during the decode process.
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String decode(String value) throws DecoderException {
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (value == null) {
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return decodeText(value);
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } catch (UnsupportedEncodingException e) {
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new DecoderException(e.getMessage());
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Encodes an object into its Base64 form using the default charset. Unsafe characters are escaped.
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  object to convert to Base64 form
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return Base64 object
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws EncoderException
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  thrown if a failure condition is encountered during the encoding process.
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object encode(Object value) throws EncoderException {
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (value == null) {
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else if (value instanceof String) {
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return encode((String) value);
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new EncoderException("Objects of type "
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                + value.getClass().getName()
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                + " cannot be encoded using BCodec");
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Decodes a Base64 object into its original form. Escaped characters are converted back to their original
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * representation.
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param value
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  Base64 object to convert into its original form
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return original object
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws DecoderException
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *                  A decoder exception is thrown if a failure condition is encountered during the decode process.
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public Object decode(Object value) throws DecoderException {
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (value == null) {
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return null;
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else if (value instanceof String) {
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return decode((String) value);
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new DecoderException("Objects of type "
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                + value.getClass().getName()
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                + " cannot be decoded using BCodec");
196069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
197069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
198069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
199069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
200069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * The default charset used for string decoding and encoding.
201069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
202069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return the default string charset.
203069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
204069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public String getDefaultCharset() {
205069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return this.charset;
206069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
207069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
208