Author: ggregory Date: Wed Mar 28 15:27:09 2012 New Revision: 1306412 URL: http://svn.apache.org/viewvc?rev=1306412&view=rev Log: [CODEC-136] Use Charset objects when possible, create Charsets for required character encodings.
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/BCodec.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QCodec.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/BCodecTest.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/QCodecTest.java Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/BCodec.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/BCodec.java?rev=1306412&r1=1306411&r2=1306412&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/BCodec.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/BCodec.java Wed Mar 28 15:27:09 2012 @@ -18,8 +18,10 @@ package org.apache.commons.codec.net; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; -import org.apache.commons.codec.CharEncoding; +import org.apache.commons.codec.Charsets; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; import org.apache.commons.codec.StringDecoder; @@ -49,13 +51,13 @@ public class BCodec extends RFC1522Codec /** * The default charset used for string decoding and encoding. */ - private final String charset; + private final Charset charset; /** * Default constructor. */ public BCodec() { - this(CharEncoding.UTF_8); + this(Charsets.UTF_8); } /** @@ -66,11 +68,24 @@ public class BCodec extends RFC1522Codec * * @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a> */ - public BCodec(final String charset) { - super(); + public BCodec(final Charset charset) { this.charset = charset; } + /** + * Constructor which allows for the selection of a default charset + * + * @param charsetName + * the default charset to use. + * @throws UnsupportedCharsetException + * If the named charset is unavailable + * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable + * @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a> + */ + public BCodec(final String charsetName) { + this(Charset.forName(charsetName)); + } + @Override protected String getEncoding() { return "B"; @@ -104,12 +119,31 @@ public class BCodec extends RFC1522Codec * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ + public String encode(final String value, final Charset charset) throws EncoderException { + if (value == null) { + return null; + } + return encodeText(value, charset); + } + + /** + * Encodes a string into its Base64 form using the specified charset. Unsafe characters are escaped. + * + * @param value + * string to convert to Base64 form + * @param charset + * the charset for <code>value</code> + * @return Base64 string + * + * @throws EncoderException + * thrown if a failure condition is encountered during the encoding process. + */ public String encode(final String value, final String charset) throws EncoderException { if (value == null) { return null; } try { - return encodeText(value, charset); + return this.encodeText(value, charset); } catch (UnsupportedEncodingException e) { throw new EncoderException(e.getMessage(), e); } @@ -129,7 +163,7 @@ public class BCodec extends RFC1522Codec if (value == null) { return null; } - return encode(value, getDefaultCharset()); + return encode(value, this.getCharset()); } /** @@ -147,7 +181,7 @@ public class BCodec extends RFC1522Codec return null; } try { - return decodeText(value); + return this.decodeText(value); } catch (UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } @@ -201,11 +235,21 @@ public class BCodec extends RFC1522Codec } /** - * The default charset used for string decoding and encoding. + * Gets the default charset name used for string decoding and encoding. * - * @return the default string charset. + * @return the default charset name + * @since 1.7 */ - public String getDefaultCharset() { + public Charset getCharset() { return this.charset; } + + /** + * Gets the default charset name used for string decoding and encoding. + * + * @return the default charset name + */ + public String getDefaultCharset() { + return this.charset.name(); + } } Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QCodec.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QCodec.java?rev=1306412&r1=1306411&r2=1306412&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QCodec.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/QCodec.java Wed Mar 28 15:27:09 2012 @@ -18,6 +18,8 @@ package org.apache.commons.codec.net; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; import java.util.BitSet; import org.apache.commons.codec.CharEncoding; @@ -50,7 +52,7 @@ public class QCodec extends RFC1522Codec /** * The default charset used for string decoding and encoding. */ - private final String charset; + private final Charset charset; /** * BitSet of printable characters as defined in RFC 1522. @@ -121,11 +123,25 @@ public class QCodec extends RFC1522Codec * * @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a> */ - public QCodec(final String charset) { + public QCodec(final Charset charset) { super(); this.charset = charset; } + /** + * Constructor which allows for the selection of a default charset + * + * @param charsetName + * the charset to use. + * @throws UnsupportedCharsetException + * If the named charset is unavailable + * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable + * @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a> + */ + public QCodec(final String charsetName) { + this(Charset.forName(charsetName)); + } + @Override protected String getEncoding() { return "Q"; @@ -186,6 +202,25 @@ public class QCodec extends RFC1522Codec * @throws EncoderException * thrown if a failure condition is encountered during the encoding process. */ + public String encode(final String pString, final Charset charset) throws EncoderException { + if (pString == null) { + return null; + } + return encodeText(pString, charset); + } + + /** + * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. + * + * @param pString + * string to convert to quoted-printable form + * @param charset + * the charset for pString + * @return quoted-printable string + * + * @throws EncoderException + * thrown if a failure condition is encountered during the encoding process. + */ public String encode(final String pString, final String charset) throws EncoderException { if (pString == null) { return null; @@ -211,7 +246,7 @@ public class QCodec extends RFC1522Codec if (pString == null) { return null; } - return encode(pString, getDefaultCharset()); + return encode(pString, getCharset()); } /** @@ -285,15 +320,25 @@ public class QCodec extends RFC1522Codec } /** - * The default charset used for string decoding and encoding. + * Gets the default charset name used for string decoding and encoding. * - * @return the default string charset. + * @return the default charset name + * @since 1.7 */ - public String getDefaultCharset() { + public Charset getCharset() { return this.charset; } /** + * Gets the default charset name used for string decoding and encoding. + * + * @return the default charset name + */ + public String getDefaultCharset() { + return this.charset.name(); + } + + /** * Tests if optional transformation of SPACE characters is to be used * * @return <code>true</code> if SPACE characters are to be transformed, <code>false</code> otherwise Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java?rev=1306412&r1=1306411&r2=1306412&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/net/RFC1522Codec.java Wed Mar 28 15:27:09 2012 @@ -18,6 +18,7 @@ package org.apache.commons.codec.net; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; @@ -74,12 +75,10 @@ abstract class RFC1522Codec { * * @throws EncoderException thrown if there is an error condition during the Encoding * process. - * @throws UnsupportedEncodingException thrown if charset is not supported - * * @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a> */ - protected String encodeText(final String text, final String charset) - throws EncoderException, UnsupportedEncodingException + protected String encodeText(final String text, final Charset charset) + throws EncoderException { if (text == null) { return null; @@ -88,15 +87,41 @@ abstract class RFC1522Codec { buffer.append(PREFIX); buffer.append(charset); buffer.append(SEP); - buffer.append(getEncoding()); + buffer.append(this.getEncoding()); buffer.append(SEP); - byte [] rawdata = doEncoding(text.getBytes(charset)); - buffer.append(StringUtils.newStringUsAscii(rawdata)); + byte [] rawData = this.doEncoding(text.getBytes(charset)); + buffer.append(StringUtils.newStringUsAscii(rawData)); buffer.append(POSTFIX); return buffer.toString(); } /** + * Applies an RFC 1522 compliant encoding scheme to the given string of text with the + * given charset. This method constructs the "encoded-word" header common to all the + * RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete + * class to perform the specific encoding. + * + * @param text a string to encode + * @param charsetName the charset to use + * + * @return RFC 1522 compliant "encoded-word" + * + * @throws EncoderException thrown if there is an error condition during the Encoding + * process. + * @throws UnsupportedEncodingException if charset is not available + * + * @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a> + */ + protected String encodeText(final String text, final String charsetName) + throws EncoderException, UnsupportedEncodingException + { + if (text == null) { + return null; + } + return this.encodeText(text, Charset.forName(charsetName)); + } + + /** * Applies an RFC 1522 compliant decoding scheme to the given string of text. This method * processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes * {@link #doEncoding(byte [])} method of a concrete class to perform the specific decoding. Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/BCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/BCodecTest.java?rev=1306412&r1=1306411&r2=1306412&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/BCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/BCodecTest.java Wed Mar 28 15:27:09 2012 @@ -21,6 +21,8 @@ import static org.junit.Assert.assertEqu import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; +import java.nio.charset.UnsupportedCharsetException; + import org.apache.commons.codec.CharEncoding; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; @@ -123,21 +125,9 @@ public class BCodecTest { } } - @Test + @Test(expected=UnsupportedCharsetException.class) public void testInvalidEncoding() { - BCodec bcodec = new BCodec("NONSENSE"); - try { - bcodec.encode("Hello there!"); - fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked."); - } catch (EncoderException ee) { - // Exception expected, test segment passes. - } - try { - bcodec.decode("=?NONSENSE?B?Hello there!?="); - fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked."); - } catch (DecoderException ee) { - // Exception expected, test segment passes. - } + new BCodec("NONSENSE"); } @Test Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/QCodecTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/QCodecTest.java?rev=1306412&r1=1306411&r2=1306412&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/QCodecTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/net/QCodecTest.java Wed Mar 28 15:27:09 2012 @@ -24,6 +24,8 @@ import static org.junit.Assert.assertNul import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.nio.charset.UnsupportedCharsetException; + import org.apache.commons.codec.CharEncoding; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; @@ -150,21 +152,9 @@ public class QCodecTest { } - @Test + @Test(expected=UnsupportedCharsetException.class) public void testInvalidEncoding() { - QCodec qcodec = new QCodec("NONSENSE"); - try { - qcodec.encode("Hello there!"); - fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); - } catch (EncoderException ee) { - // Exception expected, test segment passes. - } - try { - qcodec.decode("=?NONSENSE?Q?Hello there!?="); - fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked."); - } catch (DecoderException ee) { - // Exception expected, test segment passes. - } + new QCodec("NONSENSE"); } @Test