Add Charsets.toCharset(Charset, Charset).
Add Charsets.toCharset(String, Charset).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -393,6 +393,10 @@ The <action> type attribute can be add,update,fix,remove.
Add IOUtils.closeQuietly(Iterable&lt;Closeable&gt;).
Add IOUtils.closeQuietly(Stream&lt;Closeable&gt;).
</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.

MODIFY
src/main/java/org/apache/commons/io/Charsets.java
src/main/java/org/apache/commons/io/Charsets.java
@@ -183,6 +183,18 @@ public static Charset toCharset(final Charset charset) {
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 static Charset toCharset(final Charset charset) {
* @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);
}
}

MODIFY
src/test/java/org/apache/commons/io/CharsetsTest.java
src/test/java/org/apache/commons/io/CharsetsTest.java
/**
* Tests {@link Charsets}.
- *
*/
@SuppressWarnings("deprecation") // testing deprecated code
public class CharsetsTest {
@@ -51,13 +50,24 @@ public void testRequiredCharsets() {
}
@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());

Add Charsets.toCharset(Charset, Charset).

Add Charsets.toCharset(String, Charset).