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 932ca4b  CharSequenceInputStream maps null Charset and Charset name to 
the platform default instead of throwing a NullPointerException.
932ca4b is described below

commit 932ca4bcc178372388f6f64089c211a3daf351a0
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Tue Jan 11 08:50:54 2022 -0500

    CharSequenceInputStream maps null Charset and Charset name to the
    platform default instead of throwing a NullPointerException.
---
 src/changes/changes.xml                               |  5 ++++-
 .../commons/io/input/CharSequenceInputStream.java     | 19 +++++++++++++++----
 .../commons/io/input/CharSequenceInputStreamTest.java | 18 +++++++++++++++++-
 3 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 690bac6..2229fd3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -127,7 +127,10 @@ The <action> type attribute can be add,update,fix,remove.
         When deleting symlinks, File/PathUtils.deleteDirectory() changes file 
permissions of the target.
       </action>
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
-        ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder 
to the platform default instead of throwing an NullPointerException.
+        ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder 
to the platform default instead of throwing a NullPointerException.
+      </action>
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">
+        CharSequenceInputStream maps null Charset and Charset name to the 
platform default instead of throwing a NullPointerException.
       </action>
       <!-- ADD -->
       <action issue="IO-726" dev="ggregory" type="fix" due-to="shollander, 
Gary Gregory">
diff --git 
a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java 
b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
index 4c30c7e..0ccbe50 100644
--- a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
@@ -30,6 +30,8 @@ import java.nio.charset.CoderResult;
 import java.nio.charset.CodingErrorAction;
 import java.util.Objects;
 
+import org.apache.commons.io.Charsets;
+
 /**
  * Implements an {@link InputStream} to read from String, StringBuffer, 
StringBuilder or CharBuffer.
  * <p>
@@ -66,13 +68,13 @@ public class CharSequenceInputStream extends InputStream {
      * Constructs a new instance.
      *
      * @param cs the input character sequence.
-     * @param charset the character set name to use.
+     * @param charset the character set name to use, null maps to the default 
Charset.
      * @param bufferSize the buffer size to use.
      * @throws IllegalArgumentException if the buffer is not large enough to 
hold a complete character.
      */
     public CharSequenceInputStream(final CharSequence cs, final Charset 
charset, final int bufferSize) {
         // @formatter:off
-        this.charsetEncoder = charset.newEncoder()
+        this.charsetEncoder = Charsets.toCharset(charset).newEncoder()
             .onMalformedInput(CodingErrorAction.REPLACE)
             .onUnmappableCharacter(CodingErrorAction.REPLACE);
         // @formatter:on
@@ -99,12 +101,12 @@ public class CharSequenceInputStream extends InputStream {
      * Constructs a new instance.
      *
      * @param cs the input character sequence.
-     * @param charset the character set name to use.
+     * @param charset the character set name to use, null maps to the default 
Charset.
      * @param bufferSize the buffer size to use.
      * @throws IllegalArgumentException if the buffer is not large enough to 
hold a complete character.
      */
     public CharSequenceInputStream(final CharSequence cs, final String 
charset, final int bufferSize) {
-        this(cs, Charset.forName(charset), bufferSize);
+        this(cs, Charsets.toCharset(charset), bufferSize);
     }
 
     /**
@@ -143,6 +145,15 @@ public class CharSequenceInputStream extends InputStream {
     }
 
     /**
+     * Gets the CharsetEncoder.
+     *
+     * @return the CharsetEncoder.
+     */
+    CharsetEncoder getCharsetEncoder() {
+        return charsetEncoder;
+    }
+
+    /**
      * {@inheritDoc}
      * @param readlimit max read limit (ignored).
      */
diff --git 
a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java 
b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
index 4e19570..2d04daa 100644
--- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
@@ -61,7 +61,7 @@ public class CharSequenceInputStreamTest {
         return Charsets.requiredCharsets().keySet();
     }
 
-private boolean isAvailabilityTestableForCharset(final String csName) {
+    private boolean isAvailabilityTestableForCharset(final String csName) {
         return Charset.forName(csName).canEncode()
                 && !"COMPOUND_TEXT".equalsIgnoreCase(csName) && 
!"x-COMPOUND_TEXT".equalsIgnoreCase(csName)
                 && 
!isOddBallLegacyCharsetThatDoesNotSupportFrenchCharacters(csName);
@@ -75,6 +75,22 @@ private boolean isAvailabilityTestableForCharset(final 
String csName) {
     }
 
     @Test
+    public void testNullCharset() throws IOException {
+        try (CharSequenceInputStream in = new CharSequenceInputStream("A", 
(Charset) null)) {
+            IOUtils.toByteArray(in);
+            assertEquals(Charset.defaultCharset(), 
in.getCharsetEncoder().charset());
+        }
+    }
+
+    @Test
+    public void testNullCharsetName() throws IOException {
+        try (CharSequenceInputStream in = new CharSequenceInputStream("A", 
(String) null)) {
+            IOUtils.toByteArray(in);
+            assertEquals(Charset.defaultCharset(), 
in.getCharsetEncoder().charset());
+        }
+    }
+
+    @Test
     public void testAvailable() throws Exception {
         for (final String csName : Charset.availableCharsets().keySet()) {
             // prevent java.lang.UnsupportedOperationException at 
sun.nio.cs.ext.ISO2022_CN.newEncoder.

Reply via email to