Use new feature for implementing isSupported(); Javadoc

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1078006 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2011-03-04 15:31:49 +00:00
parent 81b5d12e81
commit 4b615e2319
2 changed files with 41 additions and 82 deletions

View File

@ -17,24 +17,19 @@
package org.apache.commons.lang3; package org.apache.commons.lang3;
import java.io.UnsupportedEncodingException; import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
/** /**
* <p> * <p>Character encoding names required of every implementation of the Java platform.</p>
* Character encoding names required of every implementation of the Java platform.
* </p>
* *
* <p> * <p>According to <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* According to <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * encoding names</a>:</p>
* encoding names</a>:
* <p>
* <cite>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.</cite>
* </p>
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character encoding * <p><cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
* names</a> * release documentation for your implementation to see if any other encodings are supported.</cite></p>
*
* @see <a href="http://download.oracle.com/javase/1.3/docs/guide/intl/encoding.doc.html">JRE character encoding names</a>
* @author Apache Software Foundation * @author Apache Software Foundation
* @since 2.1 * @since 2.1
* @version $Id$ * @version $Id$
@ -42,112 +37,69 @@ import java.io.UnsupportedEncodingException;
public class CharEncoding { public class CharEncoding {
/** /**
* <p> * <p>ISO Latin Alphabet #1, also known as ISO-LATIN-1.</p>
* ISO Latin Alphabet #1, also known as ISO-LATIN-1.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * <p>Every implementation of the Java platform is required to support this character encoding.</p>
* encoding names</a>
*/ */
public static final String ISO_8859_1 = "ISO-8859-1"; public static final String ISO_8859_1 = "ISO-8859-1";
/** /**
* <p> * <p>Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set. * of the Unicode character set.</p>
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * <p>Every implementation of the Java platform is required to support this character encoding.</p>
* encoding names</a>
*/ */
public static final String US_ASCII = "US-ASCII"; public static final String US_ASCII = "US-ASCII";
/** /**
* <p> * <p>Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial
* Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either * byte-order mark (either order accepted on input, big-endian used on output).</p>
* order accepted on input, big-endian used on output).
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * <p>Every implementation of the Java platform is required to support this character encoding.</p>
* encoding names</a>
*/ */
public static final String UTF_16 = "UTF-16"; public static final String UTF_16 = "UTF-16";
/** /**
* <p> * <p>Sixteen-bit Unicode Transformation Format, big-endian byte order.</p>
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * <p>Every implementation of the Java platform is required to support this character encoding.</p>
* encoding names</a>
*/ */
public static final String UTF_16BE = "UTF-16BE"; public static final String UTF_16BE = "UTF-16BE";
/** /**
* <p> * <p>Sixteen-bit Unicode Transformation Format, little-endian byte order.</p>
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * <p>Every implementation of the Java platform is required to support this character encoding.</p>
* encoding names</a>
*/ */
public static final String UTF_16LE = "UTF-16LE"; public static final String UTF_16LE = "UTF-16LE";
/** /**
* <p> * <p>Eight-bit Unicode Transformation Format.</p>
* Eight-bit Unicode Transformation Format.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * <p>Every implementation of the Java platform is required to support this character encoding.</p>
* encoding names</a>
*/ */
public static final String UTF_8 = "UTF-8"; public static final String UTF_8 = "UTF-8";
//-----------------------------------------------------------------------
/** /**
* <p> * <p>Returns whether the named charset is supported.</p>
* Returns whether the named charset is supported.
* </p>
* <p>
* This is similar to <a
* href="http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.html#isSupported(java.lang.String)">
* java.nio.charset.Charset.isSupported(String)</a>
* </p>
* *
* @param name * <p>This is similar to <a
* the name of the requested charset; may be either a canonical name or an alias * href="http://download.oracle.com/javase/1.4.2/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
* @return <code>true</code> if, and only if, support for the named charset is available in the current Java * java.nio.charset.Charset.isSupported(String)</a> but handles more formats</p>
* virtual machine
* *
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character * @param name the name of the requested charset; may be either a canonical name or an alias, null returns false
* encoding names</a> * @return {@code true} if the charset is available in the current Java virtual machine
*/ */
public static boolean isSupported(String name) { public static boolean isSupported(String name) {
if (name == null) { if (name == null) {
return false; return false;
} }
try { try {
new String(ArrayUtils.EMPTY_BYTE_ARRAY, name); return Charset.isSupported(name);
} catch (UnsupportedEncodingException e) { } catch (IllegalCharsetNameException ex) {
return false; return false;
} }
return true;
} }
} }

View File

@ -17,10 +17,11 @@
package org.apache.commons.lang3; 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 junit.framework.TestCase;
import static org.apache.commons.lang3.JavaVersion.*;
/** /**
* Tests CharEncoding. * Tests CharEncoding.
* *
@ -54,6 +55,12 @@ public class CharEncodingTest extends TestCase {
} }
} }
public void testSupported() {
assertTrue(CharEncoding.isSupported("UTF8"));
assertTrue(CharEncoding.isSupported("UTF-8"));
assertTrue(CharEncoding.isSupported("ASCII"));
}
public void testNotSupported() { public void testNotSupported() {
assertFalse(CharEncoding.isSupported(null)); assertFalse(CharEncoding.isSupported(null));
assertFalse(CharEncoding.isSupported("")); assertFalse(CharEncoding.isSupported(""));