Author: ggregory
Date: Fri Nov 30 15:30:50 2012
New Revision: 1415693
URL: http://svn.apache.org/viewvc?rev=1415693&view=rev
Log:
<action issue="IO-360" dev="ggregory" type="add">Add API
Charsets.requiredCharsets().</action>
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/CharsetsTestCase.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1415693&r1=1415692&r2=1415693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Fri Nov 30 15:30:50 2012
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
<body>
<!-- The release date is the date RC is cut -->
<release version="2.5" date="201?-??-??" description="New features and bug
fixes.">
+ <action issue="IO-360" dev="ggregory" type="add">
+ Add API Charsets.requiredCharsets().
+ </action>
<action issue="IO-359" dev="ggregory" type="add" due-to="yukoba">
Add IOUtils.skip and skipFully(ReadableByteChannel, long).
</action>
Modified:
commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java?rev=1415693&r1=1415692&r2=1415693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java
(original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/Charsets.java
Fri Nov 30 15:30:50 2012
@@ -18,6 +18,9 @@ package org.apache.commons.io;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
+import java.util.Collections;
+import java.util.SortedMap;
+import java.util.TreeMap;
/**
* Charsets required of every implementation of the Java platform.
@@ -25,8 +28,8 @@ import java.nio.charset.UnsupportedChars
* From the Java documentation <a
href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
* Standard charsets</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. Consult the release
+ * <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. Consult the release
* documentation for your implementation to see if any other encodings are
supported. </cite>
* </p>
*
@@ -57,6 +60,30 @@ public class Charsets {
//
/**
+ * Constructs a sorted map from canonical charset names to charset objects
required of every implementation of the
+ * Java platform.
+ * <p>
+ * From the Java documentation <a
href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">
+ * Standard charsets</a>:
+ * </p>
+ *
+ * @return An immutable, case-insensitive map from canonical charset names
to charset objects.
+ * @see Charset#availableCharsets()
+ * @since 2.5
+ */
+ public static SortedMap<String, Charset> requiredCharsets() {
+ // maybe cache?
+ TreeMap<String, Charset> m = new TreeMap<String,
Charset>(String.CASE_INSENSITIVE_ORDER);
+ m.put(ISO_8859_1.name(), ISO_8859_1);
+ m.put(US_ASCII.name(), US_ASCII);
+ m.put(UTF_16.name(), UTF_16);
+ m.put(UTF_16BE.name(), UTF_16BE);
+ m.put(UTF_16LE.name(), UTF_16LE);
+ m.put(UTF_8.name(), UTF_8);
+ return Collections.unmodifiableSortedMap(m);
+ }
+
+ /**
* Returns the given Charset or the default Charset if the given Charset
is null.
*
* @param charset
Modified:
commons/proper/io/trunk/src/test/java/org/apache/commons/io/CharsetsTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/CharsetsTestCase.java?rev=1415693&r1=1415692&r2=1415693&view=diff
==============================================================================
---
commons/proper/io/trunk/src/test/java/org/apache/commons/io/CharsetsTestCase.java
(original)
+++
commons/proper/io/trunk/src/test/java/org/apache/commons/io/CharsetsTestCase.java
Fri Nov 30 15:30:50 2012
@@ -18,6 +18,7 @@
package org.apache.commons.io;
import java.nio.charset.Charset;
+import java.util.SortedMap;
import junit.framework.Assert;
@@ -31,10 +32,23 @@ import org.junit.Test;
public class CharsetsTestCase {
@Test
+ public void testRequiredCharsets() {
+ final SortedMap<String, Charset> requiredCharsets =
Charsets.requiredCharsets();
+ // test for what we expect to be there as of Java 6
+ // Make sure the object at the given key is the right one
+ Assert.assertEquals(requiredCharsets.get("US-ASCII").name(),
"US-ASCII");
+ Assert.assertEquals(requiredCharsets.get("ISO-8859-1").name(),
"ISO-8859-1");
+ Assert.assertEquals(requiredCharsets.get("UTF-8").name(), "UTF-8");
+ Assert.assertEquals(requiredCharsets.get("UTF-16").name(), "UTF-16");
+ Assert.assertEquals(requiredCharsets.get("UTF-16BE").name(),
"UTF-16BE");
+ Assert.assertEquals(requiredCharsets.get("UTF-16LE").name(),
"UTF-16LE");
+ }
+
+ @Test
public void testIso8859_1() {
Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
}
-
+
@Test
public void testToCharset() {
Assert.assertEquals(Charset.defaultCharset(),
Charsets.toCharset((String) null));