This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 12b97b14 Add Charsets.toCharset(Charset, Charset). 12b97b14 is described below commit 12b97b145355b381863b6fd7a7f0ab9614f74ab0 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Jun 21 13:51:28 2022 -0400 Add Charsets.toCharset(Charset, Charset). Add Charsets.toCharset(String, Charset). --- src/changes/changes.xml | 4 ++++ src/main/java/org/apache/commons/io/Charsets.java | 27 +++++++++++++++++++++- .../java/org/apache/commons/io/CharsetsTest.java | 14 +++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 63ba6968..f0675cd9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -393,6 +393,10 @@ The <action> type attribute can be add,update,fix,remove. Add IOUtils.closeQuietly(Iterable<Closeable>). Add IOUtils.closeQuietly(Stream<Closeable>). </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add Charsets.toCharset(Charset, Charset). + Add Charsets.toCharset(String, Charset). + </action> <!-- UPDATE --> <action dev="kinow" type="update" due-to="Dependabot, Gary Gregory"> Bump actions/cache from 2.1.6 to 3.0.4 #307, #337. diff --git a/src/main/java/org/apache/commons/io/Charsets.java b/src/main/java/org/apache/commons/io/Charsets.java index d2ba98e5..37275b8c 100644 --- a/src/main/java/org/apache/commons/io/Charsets.java +++ b/src/main/java/org/apache/commons/io/Charsets.java @@ -183,6 +183,18 @@ public class Charsets { return charset == null ? Charset.defaultCharset() : charset; } + /** + * Returns the given charset if non-null, otherwise return defaultCharset. + * + * @param charset The charset to test, may be null. + * @param defaultCharset The charset to return if charset is null, may be null. + * @return a Charset . + * @since 2.12.0 + */ + public static Charset toCharset(final Charset charset, final Charset defaultCharset) { + return charset == null ? defaultCharset : charset; + } + /** * Returns a Charset for the named charset. If the name is null, return the default Charset. * @@ -191,6 +203,19 @@ public class Charsets { * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception). */ public static Charset toCharset(final String charsetName) throws UnsupportedCharsetException { - return charsetName == null ? Charset.defaultCharset() : Charset.forName(charsetName); + return toCharset(charsetName, Charset.defaultCharset()); + } + + /** + * Returns a Charset for the named charset. If the name is null, return the given default Charset. + * + * @param charsetName The name of the requested charset, may be null. + * @param defaultCharset The name charset to return if charsetName is null, may be null. + * @return a Charset for the named charset. + * @throws UnsupportedCharsetException If the named charset is unavailable (unchecked exception). + * @since 2.12.0 + */ + public static Charset toCharset(final String charsetName, final Charset defaultCharset) throws UnsupportedCharsetException { + return charsetName == null ? defaultCharset : Charset.forName(charsetName); } } diff --git a/src/test/java/org/apache/commons/io/CharsetsTest.java b/src/test/java/org/apache/commons/io/CharsetsTest.java index 39cb329f..b68b90b6 100644 --- a/src/test/java/org/apache/commons/io/CharsetsTest.java +++ b/src/test/java/org/apache/commons/io/CharsetsTest.java @@ -27,7 +27,6 @@ import org.junit.jupiter.api.Test; /** * Tests {@link Charsets}. - * */ @SuppressWarnings("deprecation") // testing deprecated code public class CharsetsTest { @@ -51,13 +50,24 @@ public class CharsetsTest { } @Test - public void testToCharset() { + public void testToCharset_String() { assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null)); assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null)); assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset())); assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8)); } + @Test + public void testToCharset_String_Charset() { + assertEquals(null, Charsets.toCharset((String) null, null)); + assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null, Charset.defaultCharset())); + assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null, Charset.defaultCharset())); + assertEquals(null, Charsets.toCharset((Charset) null, null)); + assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset(), Charset.defaultCharset())); + assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8, Charset.defaultCharset())); + assertEquals(StandardCharsets.UTF_8, Charsets.toCharset(StandardCharsets.UTF_8, null)); + } + @Test public void testUsAscii() { assertEquals("US-ASCII", Charsets.US_ASCII.name());