1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.codec.net;
19
20 import java.io.UnsupportedEncodingException;
21
22 import org.apache.commons.codec.DecoderException;
23 import org.apache.commons.codec.EncoderException;
24 import org.apache.commons.codec.StringDecoder;
25 import org.apache.commons.codec.StringEncoder;
26 import org.apache.commons.codec.binary.Base64;
27
28 /**
29 * <p>
30 * Identical to the Base64 encoding defined by <a href="http://www.ietf.org/rfc/rfc1521.txt">RFC
31 * 1521</a> and allows a character set to be specified.
32 * </p>
33 *
34 * <p>
35 * <a href="http://www.ietf.org/rfc/rfc1522.txt">RFC 1522</a> describes techniques to allow the encoding of non-ASCII
36 * text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message
37 * handling software.
38 * </p>
39 *
40 * @see <a href="http://www.ietf.org/rfc/rfc1522.txt">MIME (Multipurpose Internet Mail Extensions) Part Two: Message
41 * Header Extensions for Non-ASCII Text</a>
42 *
43 * @author Apache Software Foundation
44 * @since 1.3
45 * @version $Id: BCodec.java 480406 2006-11-29 04:56:58Z bayard $
46 */
47 public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
48 /**
49 * The default charset used for string decoding and encoding.
50 */
51 private String charset = CharacterEncodingNames.UTF8;
52
53 /**
54 * Default constructor.
55 */
56 public BCodec() {
57 super();
58 }
59
60 /**
61 * Constructor which allows for the selection of a default charset
62 *
63 * @param charset
64 * the default string charset to use.
65 *
66 * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
67 * encoding names</a>
68 */
69 public BCodec(final String charset) {
70 super();
71 this.charset = charset;
72 }
73
74 protected String getEncoding() {
75 return "B";
76 }
77
78 protected byte[] doEncoding(byte[] bytes) {
79 if (bytes == null) {
80 return null;
81 }
82 return Base64.encodeBase64(bytes);
83 }
84
85 protected byte[] doDecoding(byte[] bytes) {
86 if (bytes == null) {
87 return null;
88 }
89 return Base64.decodeBase64(bytes);
90 }
91
92 /**
93 * Encodes a string into its Base64 form using the specified charset. Unsafe characters are escaped.
94 *
95 * @param value
96 * string to convert to Base64 form
97 * @param charset
98 * the charset for <code>value</code>
99 * @return Base64 string
100 *
101 * @throws EncoderException
102 * thrown if a failure condition is encountered during the encoding process.
103 */
104 public String encode(final String value, final String charset) throws EncoderException {
105 if (value == null) {
106 return null;
107 }
108 try {
109 return encodeText(value, charset);
110 } catch (UnsupportedEncodingException e) {
111 throw new EncoderException(e.getMessage());
112 }
113 }
114
115 /**
116 * Encodes a string into its Base64 form using the default charset. Unsafe characters are escaped.
117 *
118 * @param value
119 * string to convert to Base64 form
120 * @return Base64 string
121 *
122 * @throws EncoderException
123 * thrown if a failure condition is encountered during the encoding process.
124 */
125 public String encode(String value) throws EncoderException {
126 if (value == null) {
127 return null;
128 }
129 return encode(value, getDefaultCharset());
130 }
131
132 /**
133 * Decodes a Base64 string into its original form. Escaped characters are converted back to their original
134 * representation.
135 *
136 * @param value
137 * Base64 string to convert into its original form
138 *
139 * @return original string
140 *
141 * @throws DecoderException
142 * A decoder exception is thrown if a failure condition is encountered during the decode process.
143 */
144 public String decode(String value) throws DecoderException {
145 if (value == null) {
146 return null;
147 }
148 try {
149 return decodeText(value);
150 } catch (UnsupportedEncodingException e) {
151 throw new DecoderException(e.getMessage());
152 }
153 }
154
155 /**
156 * Encodes an object into its Base64 form using the default charset. Unsafe characters are escaped.
157 *
158 * @param value
159 * object to convert to Base64 form
160 * @return Base64 object
161 *
162 * @throws EncoderException
163 * thrown if a failure condition is encountered during the encoding process.
164 */
165 public Object encode(Object value) throws EncoderException {
166 if (value == null) {
167 return null;
168 } else if (value instanceof String) {
169 return encode((String) value);
170 } else {
171 throw new EncoderException("Objects of type "
172 + value.getClass().getName()
173 + " cannot be encoded using BCodec");
174 }
175 }
176
177 /**
178 * Decodes a Base64 object into its original form. Escaped characters are converted back to their original
179 * representation.
180 *
181 * @param value
182 * Base64 object to convert into its original form
183 *
184 * @return original object
185 *
186 * @throws DecoderException
187 * Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is
188 * encountered during the decode process.
189 */
190 public Object decode(Object value) throws DecoderException {
191 if (value == null) {
192 return null;
193 } else if (value instanceof String) {
194 return decode((String) value);
195 } else {
196 throw new DecoderException("Objects of type "
197 + value.getClass().getName()
198 + " cannot be decoded using BCodec");
199 }
200 }
201
202 /**
203 * The default charset used for string decoding and encoding.
204 *
205 * @return the default string charset.
206 */
207 public String getDefaultCharset() {
208 return this.charset;
209 }
210 }