Skip to content

Commit 12b97b1

Browse files
author
Gary Gregory
committed
Add Charsets.toCharset(Charset, Charset).
Add Charsets.toCharset(String, Charset).
1 parent 94f0f78 commit 12b97b1

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ The <action> type attribute can be add,update,fix,remove.
393393
Add IOUtils.closeQuietly(Iterable&lt;Closeable&gt;).
394394
Add IOUtils.closeQuietly(Stream&lt;Closeable&gt;).
395395
</action>
396+
<action dev="ggregory" type="add" due-to="Gary Gregory">
397+
Add Charsets.toCharset(Charset, Charset).
398+
Add Charsets.toCharset(String, Charset).
399+
</action>
396400
<!-- UPDATE -->
397401
<action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
398402
Bump actions/cache from 2.1.6 to 3.0.4 #307, #337.

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ public static Charset toCharset(final Charset charset) {
183183
return charset == null ? Charset.defaultCharset() : charset;
184184
}
185185

186+
/**
187+
* Returns the given charset if non-null, otherwise return defaultCharset.
188+
*
189+
* @param charset The charset to test, may be null.
190+
* @param defaultCharset The charset to return if charset is null, may be null.
191+
* @return a Charset .
192+
* @since 2.12.0
193+
*/
194+
public static Charset toCharset(final Charset charset, final Charset defaultCharset) {
195+
return charset == null ? defaultCharset : charset;
196+
}
197+
186198
/**
187199
* Returns a Charset for the named charset. If the name is null, return the default Charset.
188200
*
@@ -191,6 +203,19 @@ public static Charset toCharset(final Charset charset) {
191203
* @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception).
192204
*/
193205
public static Charset toCharset(final String charsetName) throws UnsupportedCharsetException {
194-
return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName);
206+
return toCharset(charsetName, Charset.defaultCharset());
207+
}
208+
209+
/**
210+
* Returns a Charset for the named charset. If the name is null, return the given default Charset.
211+
*
212+
* @param charsetName The name of the requested charset, may be null.
213+
* @param defaultCharset The name charset to return if charsetName is null, may be null.
214+
* @return a Charset for the named charset.
215+
* @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception).
216+
* @since 2.12.0
217+
*/
218+
public static Charset toCharset(final String charsetName, final Charset defaultCharset) throws UnsupportedCharsetException {
219+
return charsetName == null ? defaultCharset : Charset.forName(charsetName);
195220
}
196221
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
/**
2929
* Tests {@link Charsets}.
30-
*
3130
*/
3231
@SuppressWarnings("deprecation") // testing deprecated code
3332
public class CharsetsTest {
@@ -51,13 +50,24 @@ public void testRequiredCharsets() {
5150
}
5251

5352
@Test
54-
public void testToCharset() {
53+
public void testToCharset_String() {
5554
assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null));
5655
assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null));
5756
assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset()));
5857
assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8));
5958
}
6059

60+
@Test
61+
public void testToCharset_String_Charset() {
62+
assertEquals(null, Charsets.toCharset((String) null, null));
63+
assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null, Charset.defaultCharset()));
64+
assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null, Charset.defaultCharset()));
65+
assertEquals(null, Charsets.toCharset((Charset) null, null));
66+
assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset(), Charset.defaultCharset()));
67+
assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8, Charset.defaultCharset()));
68+
assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8, null));
69+
}
70+
6171
@Test
6272
public void testUsAscii() {
6373
assertEquals("US-ASCII", Charsets.US_ASCII.name());

0 commit comments

Comments
 (0)