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 b96c1aef383fa4d514dcd1ee3125d5f2121cb8f7 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jun 2 19:14:39 2023 -0400 ReaderInputStream.Builder.setCharsetEncoder(null) should reset to a default object, not throw an NPE. --- src/changes/changes.xml | 3 +++ .../java/org/apache/commons/io/input/ReaderInputStream.java | 10 +++++++--- .../org/apache/commons/io/input/ReaderInputStreamTest.java | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5826c514..0e4f0029 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -70,6 +70,9 @@ The <action> type attribute can be add,update,fix,remove. <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 e4d9c0fb..01087800 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 @@ public class ReaderInputStream extends InputStream { 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 @@ public class ReaderInputStream extends InputStream { /** * 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 b938e621..565fa1e7 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 @@ package org.apache.commons.io.input; 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; @@ -241,6 +242,11 @@ public class ReaderInputStreamTest { } } + @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);