Skip to content

Commit a084b0e

Browse files
committed
[IO-318] Add Charset sister APIs to method that take a String charset name.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1307568 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4ea616e commit a084b0e

3 files changed

Lines changed: 145 additions & 10 deletions

File tree

src/main/java/org/apache/commons/io/Charsets.java

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626
* @since 2.3
2727
*/
2828
public class Charsets {
29+
//
30+
// This class should only contain Charset instances for required encodings. This guarantees that it will load correctly and
31+
// without delay on all Java platforms.
32+
//
2933

3034
/**
3135
* Returns the given Charset or the default Charset if the given Charset is null.
3236
*
3337
* @param charset
3438
* A charset or null.
3539
* @return the given Charset or the default Charset if the given Charset is null
36-
* @since 2.3
3740
*/
3841
public static Charset toCharset(Charset charset) {
3942
return charset == null ? Charset.defaultCharset() : charset;
@@ -47,10 +50,79 @@ public static Charset toCharset(Charset charset) {
4750
* @return a Charset for the named charset
4851
* @throws UnsupportedCharsetException
4952
* If the named charset is unavailable
50-
* @since 2.3
5153
*/
5254
public static Charset toCharset(String charset) {
5355
return charset == null ? Charset.defaultCharset() : Charset.forName(charset);
5456
}
5557

58+
/**
59+
* CharEncodingISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1. </p>
60+
* <p>
61+
* Every implementation of the Java platform is required to support this character encoding.
62+
* </p>
63+
*
64+
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
65+
*/
66+
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
67+
68+
/**
69+
* <p>
70+
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
71+
* </p>
72+
* <p>
73+
* Every implementation of the Java platform is required to support this character encoding.
74+
* </p>
75+
*
76+
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
77+
*/
78+
public static final Charset US_ASCII = Charset.forName("US-ASCII");
79+
80+
/**
81+
* <p>
82+
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
83+
* (either order accepted on input, big-endian used on output)
84+
* </p>
85+
* <p>
86+
* Every implementation of the Java platform is required to support this character encoding.
87+
* </p>
88+
*
89+
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
90+
*/
91+
public static final Charset UTF_16 = Charset.forName("UTF-16");
92+
93+
/**
94+
* <p>
95+
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
96+
* </p>
97+
* <p>
98+
* Every implementation of the Java platform is required to support this character encoding.
99+
* </p>
100+
*
101+
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
102+
*/
103+
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
104+
105+
/**
106+
* <p>
107+
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
108+
* </p>
109+
* <p>
110+
* Every implementation of the Java platform is required to support this character encoding.
111+
* </p>
112+
*
113+
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
114+
*/
115+
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
116+
117+
/**
118+
* <p>
119+
* Eight-bit Unicode Transformation Format.
120+
* </p>
121+
* <p>
122+
* Every implementation of the Java platform is required to support this character encoding.
123+
* </p>
124+
*
125+
* @see <a href="http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
126+
*/
127+
public static final Charset UTF_8 = Charset.forName("UTF-8");
56128
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io;
19+
20+
import java.nio.charset.Charset;
21+
22+
import junit.framework.Assert;
23+
24+
import org.junit.Test;
25+
26+
/**
27+
* Tests {@link Charsets}.
28+
*
29+
* @version $Id: CharEncodingTest.java 1298985 2012-03-09 19:12:49Z ggregory $
30+
*/
31+
public class CharsetsTestCase {
32+
33+
@Test
34+
public void testToCharset() {
35+
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
36+
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
37+
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
38+
Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
39+
}
40+
41+
@Test
42+
public void testIso8859_1() {
43+
Assert.assertEquals("ISO-8859-1", Charsets.ISO_8859_1.name());
44+
}
45+
46+
@Test
47+
public void testUsAscii() {
48+
Assert.assertEquals("US-ASCII", Charsets.US_ASCII.name());
49+
}
50+
51+
@Test
52+
public void testUtf16() {
53+
Assert.assertEquals("UTF-16", Charsets.UTF_16.name());
54+
}
55+
56+
@Test
57+
public void testUtf16Be() {
58+
Assert.assertEquals("UTF-16BE", Charsets.UTF_16BE.name());
59+
}
60+
61+
@Test
62+
public void testUtf16Le() {
63+
Assert.assertEquals("UTF-16LE", Charsets.UTF_16LE.name());
64+
}
65+
66+
@Test
67+
public void testUtf8() {
68+
Assert.assertEquals("UTF-8", Charsets.UTF_8.name());
69+
}
70+
71+
}

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
import org.apache.commons.io.testtools.FileBasedTestCase;
4747
import org.junit.Assert;
4848

49-
// Note: jdk1.2 dependency
50-
5149
/**
5250
* This is used to test IOUtils for correctness. The following checks are performed:
5351
* <ul>
@@ -291,12 +289,6 @@ public void testToByteArray_Reader() throws IOException {
291289
Assert.assertArrayEquals(expecteds, actuals);
292290
}
293291

294-
public void testToCharset() {
295-
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
296-
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
297-
Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
298-
Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8")));
299-
}
300292
public void testInputStreamToByteArray() throws Exception {
301293
FileInputStream fin = new FileInputStream(m_testFile);
302294
try {

0 commit comments

Comments
 (0)