You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/03/28 17:09:19 UTC
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
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.
* <p>
* The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
- * {@link #getCharsetName()}.
+ * {@link #getCharset()}.
* </p>
*
* @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.
* <p>
* The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
- * {@link #getCharsetName()}.
+ * {@link #getCharset()}.
* </p>
*
* @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