Skip to content

Commit b96c1ae

Browse files
committed
ReaderInputStream.Builder.setCharsetEncoder(null) should reset to a
default object, not throw an NPE.
1 parent fad416c commit b96c1ae

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ The <action> type attribute can be add,update,fix,remove.
7070
<action dev="ggregory" type="fix" due-to="Shai Shapira, Gary Gregory" issue="IO-798">
7171
DeferredFileOutputStream throws exception when system temp dir is a symlink.
7272
</action>
73+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
74+
ReaderInputStream.Builder.setCharsetEncoder(null) should reset to a default object, not throw an NPE.
75+
</action>
7376
<!-- ADD -->
7477
<action dev="ggregory" type="add" due-to="Gary Gregory">
7578
Add CharSequenceInputStream.Builder.

src/main/java/org/apache/commons/io/input/ReaderInputStream.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public ReaderInputStream get() throws IOException {
123123
return new ReaderInputStream(checkOrigin().getReader(getCharset()), charsetEncoder, getBufferSize());
124124
}
125125

126+
CharsetEncoder getCharsetEncoder() {
127+
return charsetEncoder;
128+
}
129+
126130
@Override
127131
public Builder setCharset(final Charset charset) {
128132
charsetEncoder = charset.newEncoder();
@@ -132,12 +136,12 @@ public Builder setCharset(final Charset charset) {
132136
/**
133137
* Sets the charset encoder.
134138
*
135-
* @param charsetEncoder the charset encoder.
139+
* @param charsetEncoder the charset encoder, null resets to a default encoder.
136140
* @return this
137141
*/
138142
public Builder setCharsetEncoder(final CharsetEncoder charsetEncoder) {
139-
this.charsetEncoder = charsetEncoder;
140-
super.setCharset(charsetEncoder.charset());
143+
this.charsetEncoder = CharsetEncoders.toCharsetEncoder(charsetEncoder);
144+
super.setCharset(this.charsetEncoder.charset());
141145
return this;
142146
}
143147

src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324

@@ -241,6 +242,11 @@ public void testReadZeroEmptyString() throws Exception {
241242
}
242243
}
243244

245+
@Test
246+
public void testResetCharsetEncoder() {
247+
assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharsetEncoder(null).getCharsetEncoder());
248+
}
249+
244250
@Test
245251
public void testUTF16WithSingleByteRead() throws IOException {
246252
testWithSingleByteRead(TEST_STRING, UTF_16);

0 commit comments

Comments
 (0)