You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/03 00:17:12 UTC

[commons-io] 02/02: ReaderInputStream.Builder.setCharset(null) should reset to a default object, not throw an NPE

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

commit 8cc157e22e1eaef233ab44e9068abc4a5ef7007d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jun 2 19:18:37 2023 -0400

    ReaderInputStream.Builder.setCharset(null) should reset to a default
    object, not throw an NPE
---
 src/changes/changes.xml                                        |  3 +++
 .../org/apache/commons/io/build/AbstractStreamBuilder.java     |  2 +-
 .../java/org/apache/commons/io/input/ReaderInputStream.java    |  5 +++--
 .../org/apache/commons/io/input/ReaderInputStreamTest.java     | 10 ++++++++++
 4 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0e4f0029..ba1ddaea 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -73,6 +73,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" due-to="Gary Gregory">
         ReaderInputStream.Builder.setCharsetEncoder(null) should reset to a default object, not throw an NPE.
       </action>
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">
+        ReaderInputStream.Builder.setCharset(null) should reset to a default object, not throw an NPE.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add CharSequenceInputStream.Builder.
diff --git a/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java b/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
index f38bed07..8beccd93 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
@@ -99,7 +99,7 @@ public abstract class AbstractStreamBuilder<T, B extends AbstractStreamBuilder<T
      *
      * @return the Charset, defaults to {@link Charset#defaultCharset()}.
      */
-    protected Charset getCharset() {
+    public Charset getCharset() {
         return charset;
     }
 
diff --git a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
index 01087800..69e0042a 100644
--- a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
@@ -129,8 +129,9 @@ public class ReaderInputStream extends InputStream {
 
         @Override
         public Builder setCharset(final Charset charset) {
-            charsetEncoder = charset.newEncoder();
-            return super.setCharset(charset);
+            super.setCharset(charset);
+            charsetEncoder = getCharset().newEncoder();
+            return this;
         }
 
         /**
diff --git a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
index 565fa1e7..db104daf 100644
--- a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
@@ -242,11 +242,21 @@ public class ReaderInputStreamTest {
         }
     }
 
+    @Test
+    public void testResetCharset() {
+        assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharset((Charset) null).getCharset());
+    }
+
     @Test
     public void testResetCharsetEncoder() {
         assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharsetEncoder(null).getCharsetEncoder());
     }
 
+    @Test
+    public void testResetCharsetName() {
+        assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharset((String) null).getCharset());
+    }
+
     @Test
     public void testUTF16WithSingleByteRead() throws IOException {
         testWithSingleByteRead(TEST_STRING, UTF_16);