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;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
/**
* <p>
* Character encoding names required of every implementation of the Java platform.
* </p>
* <p>Character encoding names required of every implementation of the Java platform.</p>
*
* <p>
* 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>
* <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>
* <p>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>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character 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>
*
* @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
* @since 2.1
* @version $Id$
@ -42,112 +37,69 @@
public class CharEncoding {
/**
* <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>
* <p>ISO Latin Alphabet #1, also known as ISO-LATIN-1.</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* <p>Every implementation of the Java platform is required to support this character encoding.</p>
*/
public static final String ISO_8859_1 = "ISO-8859-1";
/**
* <p>
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* <p>Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block
* of the Unicode character set.</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* <p>Every implementation of the Java platform is required to support this character encoding.</p>
*/
public static final String US_ASCII = "US-ASCII";
/**
* <p>
* 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).
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* <p>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).</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* <p>Every implementation of the Java platform is required to support this character encoding.</p>
*/
public static final String UTF_16 = "UTF-16";
/**
* <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>
* <p>Sixteen-bit Unicode Transformation Format, big-endian byte order.</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* <p>Every implementation of the Java platform is required to support this character encoding.</p>
*/
public static final String UTF_16BE = "UTF-16BE";
/**
* <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>
* <p>Sixteen-bit Unicode Transformation Format, little-endian byte order.</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* <p>Every implementation of the Java platform is required to support this character encoding.</p>
*/
public static final String UTF_16LE = "UTF-16LE";
/**
* <p>
* Eight-bit Unicode Transformation Format.
* </p>
* <p>
* Every implementation of the Java platform is required to support this character encoding.
* </p>
* <p>Eight-bit Unicode Transformation Format.</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* <p>Every implementation of the Java platform is required to support this character encoding.</p>
*/
public static final String UTF_8 = "UTF-8";
//-----------------------------------------------------------------------
/**
* <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>
* <p>Returns whether the named charset is supported.</p>
*
* @param name
* the name of the requested charset; may be either a canonical name or an alias
* @return <code>true</code> if, and only if, support for the named charset is available in the current Java
* virtual machine
* <p>This is similar to <a
* href="http://download.oracle.com/javase/1.4.2/docs/api/java/nio/charset/Charset.html#isSupported%28java.lang.String%29">
* java.nio.charset.Charset.isSupported(String)</a> but handles more formats</p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
* encoding names</a>
* @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;
}
}

View File

@ -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(""));