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/30 20:22:19 UTC
svn commit: r1307569 - in /commons/proper/codec/trunk/src:
main/java/org/apache/commons/codec/Charsets.java
test/java/org/apache/commons/codec/CharsetsTest.java
Author: ggregory
Date: Fri Mar 30 18:22:18 2012
New Revision: 1307569
URL: http://svn.apache.org/viewvc?rev=1307569&view=rev
Log:
Add methods from IO.
Modified:
commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/Charsets.java
commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/CharsetsTest.java
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/Charsets.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/Charsets.java?rev=1307569&r1=1307568&r2=1307569&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/Charsets.java (original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/Charsets.java Fri Mar 30 18:22:18 2012
@@ -17,6 +17,7 @@
package org.apache.commons.codec;
import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
/**
* Charsets required of every implementation of the Java platform.
@@ -54,10 +55,36 @@ import java.nio.charset.Charset;
* @version $Id: CharEncoding.java 1173287 2011-09-20 18:16:19Z ggregory $
*/
public class Charsets {
+
//
// This class should only contain Charset instances for required encodings. This guarantees that it will load correctly and
// without delay on all Java platforms.
//
+
+ /**
+ * Returns the given Charset or the default Charset if the given Charset is null.
+ *
+ * @param charset
+ * A charset or null.
+ * @return the given Charset or the default Charset if the given Charset is null
+ */
+ public static Charset toCharset(Charset charset) {
+ return charset == null ? Charset.defaultCharset() : charset;
+ }
+
+ /**
+ * Returns a Charset for the named charset. If the name is null, return the default Charset.
+ *
+ * @param charset
+ * The name of the requested charset, may be null.
+ * @return a Charset for the named charset
+ * @throws UnsupportedCharsetException
+ * If the named charset is unavailable
+ */
+ public static Charset toCharset(String charset) {
+ return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
+ }
+
/**
* CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. </p>
* <p>
Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/CharsetsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/CharsetsTest.java?rev=1307569&r1=1307568&r2=1307569&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/CharsetsTest.java (original)
+++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/CharsetsTest.java Fri Mar 30 18:22:18 2012
@@ -17,6 +17,8 @@
package org.apache.commons.codec;
+import java.nio.charset.Charset;
+
import junit.framework.Assert;
import org.junit.Test;
@@ -29,6 +31,14 @@ import org.junit.Test;
public class CharsetsTest {
@Test
+ public void testToCharset() {
+ Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
+ Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
+ Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
+ Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
+ }
+
+ @Test
public void testIso8859_1() {
Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
}