From 4b615e2319a5f9e7f7d3b69d1bb87f4e80eba356 Mon Sep 17 00:00:00 2001
From: Stephen Colebourne
- * Character encoding names required of every implementation of the Java platform.
- * Character encoding names required of every implementation of the Java platform.
- * According to JRE character
- * encoding names:
- *
- * Every implementation of the Java platform is required to support the following character encodings. Consult the
- * release documentation for your implementation to see if any other encodings are supported.
- *
According to JRE character + * encoding names:
* - * @see JRE character encoding - * names + *Every implementation of the Java platform is required to support the following character encodings. Consult the + * release documentation for your implementation to see if any other encodings are supported.
+ * + * @see JRE character encoding names * @author Apache Software Foundation * @since 2.1 * @version $Id$ @@ -42,112 +37,69 @@ public class CharEncoding { /** - *- * ISO Latin Alphabet #1, also known as ISO-LATIN-1. - *
- *- * Every implementation of the Java platform is required to support this character encoding. - *
+ *ISO Latin Alphabet #1, also known as ISO-LATIN-1.
* - * @see JRE character - * encoding names + *Every implementation of the Java platform is required to support this character encoding.
*/ public static final String ISO_8859_1 = "ISO-8859-1"; /** - *- * Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. - *
- *- * Every implementation of the Java platform is required to support this character encoding. - *
+ *Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block + * of the Unicode character set.
* - * @see JRE character - * encoding names + *Every implementation of the Java platform is required to support this character encoding.
*/ public static final String US_ASCII = "US-ASCII"; /** - *- * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either - * order accepted on input, big-endian used on output). - *
- *- * Every implementation of the Java platform is required to support this character encoding. - *
+ *Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial + * byte-order mark (either order accepted on input, big-endian used on output).
* - * @see JRE character - * encoding names + *Every implementation of the Java platform is required to support this character encoding.
*/ public static final String UTF_16 = "UTF-16"; /** - *- * Sixteen-bit Unicode Transformation Format, big-endian byte order. - *
- *- * Every implementation of the Java platform is required to support this character encoding. - *
+ *Sixteen-bit Unicode Transformation Format, big-endian byte order.
* - * @see JRE character - * encoding names + *Every implementation of the Java platform is required to support this character encoding.
*/ public static final String UTF_16BE = "UTF-16BE"; /** - *- * Sixteen-bit Unicode Transformation Format, little-endian byte order. - *
- *- * Every implementation of the Java platform is required to support this character encoding. - *
+ *Sixteen-bit Unicode Transformation Format, little-endian byte order.
* - * @see JRE character - * encoding names + *Every implementation of the Java platform is required to support this character encoding.
*/ public static final String UTF_16LE = "UTF-16LE"; /** - *- * Eight-bit Unicode Transformation Format. - *
- *- * Every implementation of the Java platform is required to support this character encoding. - *
+ *Eight-bit Unicode Transformation Format.
* - * @see JRE character - * encoding names + *Every implementation of the Java platform is required to support this character encoding.
*/ public static final String UTF_8 = "UTF-8"; + //----------------------------------------------------------------------- /** - *- * Returns whether the named charset is supported. - *
- *- * This is similar to - * java.nio.charset.Charset.isSupported(String) - *
+ *Returns whether the named charset is supported.
* - * @param name - * the name of the requested charset; may be either a canonical name or an alias - * @returntrue
if, and only if, support for the named charset is available in the current Java
- * virtual machine
+ * This is similar to + * java.nio.charset.Charset.isSupported(String) but handles more formats
* - * @see JRE character - * encoding names + * @param name the name of the requested charset; may be either a canonical name or an alias, null returns false + * @return {@code true} if the charset is available in the current Java virtual machine */ public static boolean isSupported(String name) { if (name == null) { return false; } try { - new String(ArrayUtils.EMPTY_BYTE_ARRAY, name); - } catch (UnsupportedEncodingException e) { + return Charset.isSupported(name); + } catch (IllegalCharsetNameException ex) { return false; } - return true; } } diff --git a/src/test/java/org/apache/commons/lang3/CharEncodingTest.java b/src/test/java/org/apache/commons/lang3/CharEncodingTest.java index 1eee1b3b7..91c21e428 100644 --- a/src/test/java/org/apache/commons/lang3/CharEncodingTest.java +++ b/src/test/java/org/apache/commons/lang3/CharEncodingTest.java @@ -17,10 +17,11 @@ package org.apache.commons.lang3; +import static org.apache.commons.lang3.JavaVersion.JAVA_1_1; +import static org.apache.commons.lang3.JavaVersion.JAVA_1_2; +import static org.apache.commons.lang3.JavaVersion.JAVA_1_3; import junit.framework.TestCase; -import static org.apache.commons.lang3.JavaVersion.*; - /** * Tests CharEncoding. * @@ -54,6 +55,12 @@ public void testMustBeSupportedJava1_3_1() { } } + public void testSupported() { + assertTrue(CharEncoding.isSupported("UTF8")); + assertTrue(CharEncoding.isSupported("UTF-8")); + assertTrue(CharEncoding.isSupported("ASCII")); + } + public void testNotSupported() { assertFalse(CharEncoding.isSupported(null)); assertFalse(CharEncoding.isSupported(""));