Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8B9FC9ECD for ; Wed, 28 Mar 2012 15:09:41 +0000 (UTC) Received: (qmail 93932 invoked by uid 500); 28 Mar 2012 15:09:41 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 93825 invoked by uid 500); 28 Mar 2012 15:09:41 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 93817 invoked by uid 99); 28 Mar 2012 15:09:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Mar 2012 15:09:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Mar 2012 15:09:39 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 8884223889E0 for ; Wed, 28 Mar 2012 15:09:19 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1306398 - in /commons/proper/codec/trunk/src: main/java/org/apache/commons/codec/binary/Hex.java test/java/org/apache/commons/codec/binary/HexTest.java Date: Wed, 28 Mar 2012 15:09:19 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120328150919.8884223889E0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ggregory Date: Wed Mar 28 15:09:19 2012 New Revision: 1306398 URL: http://svn.apache.org/viewvc?rev=1306398&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/binary/Hex.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java?rev=1306398&r1=1306397&r2=1306398&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/binary/Hex.java Wed Mar 28 15:09:19 2012 @@ -17,11 +17,13 @@ package org.apache.commons.codec.binary; -import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; import org.apache.commons.codec.BinaryDecoder; import org.apache.commons.codec.BinaryEncoder; import org.apache.commons.codec.CharEncoding; +import org.apache.commons.codec.Charsets; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; @@ -36,6 +38,13 @@ import org.apache.commons.codec.EncoderE public class Hex implements BinaryEncoder, BinaryDecoder { /** + * Default charset name is {@link Charsets#UTF_8} + * + * @since 1.7 + */ + public static final Charset DEFAULT_CHARSET = Charsets.UTF_8; + + /** * Default charset name is {@link CharEncoding#UTF_8} * * @since 1.4 @@ -169,25 +178,39 @@ public class Hex implements BinaryEncode return digit; } - private final String charsetName; + private final Charset charset; /** - * Creates a new codec with the default charset name {@link #DEFAULT_CHARSET_NAME} + * Creates a new codec with the default charset name {@link #DEFAULT_CHARSET} */ public Hex() { // use default encoding - this.charsetName = DEFAULT_CHARSET_NAME; + this.charset = DEFAULT_CHARSET; + } + + /** + * Creates a new codec with the given Charset. + * + * @param charset + * the charset. + * @since 1.7 + */ + public Hex(Charset charset) { + this.charset = charset; } /** * Creates a new codec with the given charset name. * - * @param csName + * @param charsetName * the charset name. + * @throws UnsupportedCharsetException + * If the named charset is unavailable * @since 1.4 + * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable */ - public Hex(String csName) { - this.charsetName = csName; + public Hex(String charsetName) { + this(Charset.forName(charsetName)); } /** @@ -203,11 +226,7 @@ public class Hex implements BinaryEncode * @see #decodeHex(char[]) */ public byte[] decode(byte[] array) throws DecoderException { - try { - return decodeHex(new String(array, getCharsetName()).toCharArray()); - } catch (UnsupportedEncodingException e) { - throw new DecoderException(e.getMessage(), e); - } + return decodeHex(new String(array, getCharset()).toCharArray()); } /** @@ -238,19 +257,17 @@ public class Hex implements BinaryEncode * represent any given byte. *

* The conversion from hexadecimal characters to the returned bytes is performed with the charset named by - * {@link #getCharsetName()}. + * {@link #getCharset()}. *

* * @param array * a byte[] to convert to Hex characters * @return A byte[] containing the bytes of the hexadecimal characters - * @throws IllegalStateException - * if the charsetName is invalid. This API throws {@link IllegalStateException} instead of - * {@link UnsupportedEncodingException} for backward compatibility. + * @since 1.7 No longer throws IllegalStateException if the charsetName is invalid. * @see #encodeHex(byte[]) */ public byte[] encode(byte[] array) { - return StringUtils.getBytesUnchecked(encodeHexString(array), getCharsetName()); + return encodeHexString(array).getBytes(this.getCharset()); } /** @@ -259,7 +276,7 @@ public class Hex implements BinaryEncode * characters to represent any given byte. *

* The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by - * {@link #getCharsetName()}. + * {@link #getCharset()}. *

* * @param object @@ -271,23 +288,31 @@ public class Hex implements BinaryEncode */ public Object encode(Object object) throws EncoderException { try { - byte[] byteArray = object instanceof String ? ((String) object).getBytes(getCharsetName()) : (byte[]) object; + byte[] byteArray = object instanceof String ? ((String) object).getBytes(this.getCharset()) : (byte[]) object; return encodeHex(byteArray); } catch (ClassCastException e) { throw new EncoderException(e.getMessage(), e); - } catch (UnsupportedEncodingException e) { - throw new EncoderException(e.getMessage(), e); } } /** + * Gets the charset. + * + * @return the charset. + * @since 1.7 + */ + public Charset getCharset() { + return this.charset; + } + + /** * Gets the charset name. * * @return the charset name. * @since 1.4 */ public String getCharsetName() { - return this.charsetName; + return this.charset.name(); } /** @@ -297,6 +322,6 @@ public class Hex implements BinaryEncode */ @Override public String toString() { - return super.toString() + "[charsetName=" + this.charsetName + "]"; + return super.toString() + "[charsetName=" + this.charset + "]"; } } Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java?rev=1306398&r1=1306397&r2=1306398&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/binary/HexTest.java Wed Mar 28 15:09:19 2012 @@ -24,6 +24,7 @@ import static org.junit.Assert.fail; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; import java.util.Arrays; import java.util.Random; @@ -159,7 +160,7 @@ public class HexTest { Hex utf8Codec = new Hex(); expectedHexString = "48656c6c6f20576f726c64"; byte[] decodedUtf8Bytes = (byte[]) utf8Codec.decode(expectedHexString); - actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharsetName()); + actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharset()); // sanity check: assertEquals(name, sourceString, actualStringFromBytes); // actual check: @@ -168,34 +169,9 @@ public class HexTest { assertEquals(name, sourceString, actualStringFromBytes); } - @Test - public void testCustomCharsetBadNameEncodeByteArray() throws UnsupportedEncodingException { - try { - new Hex(BAD_ENCODING_NAME).encode("Hello World".getBytes("UTF-8")); - fail("Expected " + IllegalStateException.class.getName()); - } catch (IllegalStateException e) { - // Expected - } - } - - @Test - public void testCustomCharsetBadNameEncodeObject() { - try { - new Hex(BAD_ENCODING_NAME).encode("Hello World"); - fail("Expected " + EncoderException.class.getName()); - } catch (EncoderException e) { - // Expected - } - } - - @Test - public void testCustomCharsetBadNameDecodeObject() throws UnsupportedEncodingException { - try { - new Hex(BAD_ENCODING_NAME).decode("Hello World".getBytes("UTF-8")); - fail("Expected " + DecoderException.class.getName()); - } catch (DecoderException e) { - // Expected - } + @Test(expected=UnsupportedCharsetException.class) + public void testCustomCharsetBadName() throws UnsupportedEncodingException { + new Hex(BAD_ENCODING_NAME); } @Test