ReaderInputStream.Builder.setCharsetEncoder(null) should reset to a
default object, not throw an NPE.
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5826c51..0e4f002 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -70,6 +70,9 @@
<action dev="ggregory" type="fix" due-to="Shai Shapira, Gary Gregory" issue="IO-798">
DeferredFileOutputStream throws exception when system temp dir is a symlink.
</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ ReaderInputStream.Builder.setCharsetEncoder(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/input/ReaderInputStream.java b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
index e4d9c0f..0108780 100644
--- a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
@@ -123,6 +123,10 @@
return new ReaderInputStream(checkOrigin().getReader(getCharset()), charsetEncoder, getBufferSize());
}
+ CharsetEncoder getCharsetEncoder() {
+ return charsetEncoder;
+ }
+
@Override
public Builder setCharset(final Charset charset) {
charsetEncoder = charset.newEncoder();
@@ -132,12 +136,12 @@
/**
* Sets the charset encoder.
*
- * @param charsetEncoder the charset encoder.
+ * @param charsetEncoder the charset encoder, null resets to a default encoder.
* @return this
*/
public Builder setCharsetEncoder(final CharsetEncoder charsetEncoder) {
- this.charsetEncoder = charsetEncoder;
- super.setCharset(charsetEncoder.charset());
+ this.charsetEncoder = CharsetEncoders.toCharsetEncoder(charsetEncoder);
+ super.setCharset(this.charsetEncoder.charset());
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 b938e62..565fa1e 100644
--- a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
@@ -18,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -242,6 +243,11 @@
}
@Test
+ public void testResetCharsetEncoder() {
+ assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharsetEncoder(null).getCharsetEncoder());
+ }
+
+ @Test
public void testUTF16WithSingleByteRead() throws IOException {
testWithSingleByteRead(TEST_STRING, UTF_16);
}