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