Skip to content

Commit 8cc157e

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

4 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ The <action> type attribute can be add,update,fix,remove.
7373
<action dev="ggregory" type="fix" due-to="Gary Gregory">
7474
ReaderInputStream.Builder.setCharsetEncoder(null) should reset to a default object, not throw an NPE.
7575
</action>
76+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
77+
ReaderInputStream.Builder.setCharset(null) should reset to a default object, not throw an NPE.
78+
</action>
7679
<!-- ADD -->
7780
<action dev="ggregory" type="add" due-to="Gary Gregory">
7881
Add CharSequenceInputStream.Builder.

src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected CharSequence getCharSequence() throws IOException {
9999
*
100100
* @return the Charset, defaults to {@link Charset#defaultCharset()}.
101101
*/
102-
protected Charset getCharset() {
102+
public Charset getCharset() {
103103
return charset;
104104
}
105105

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ CharsetEncoder getCharsetEncoder() {
129129

130130
@Override
131131
public Builder setCharset(final Charset charset) {
132-
charsetEncoder = charset.newEncoder();
133-
return super.setCharset(charset);
132+
super.setCharset(charset);
133+
charsetEncoder = getCharset().newEncoder();
134+
return this;
134135
}
135136

136137
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,21 @@ public void testReadZeroEmptyString() throws Exception {
242242
}
243243
}
244244

245+
@Test
246+
public void testResetCharset() {
247+
assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharset((Charset) null).getCharset());
248+
}
249+
245250
@Test
246251
public void testResetCharsetEncoder() {
247252
assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharsetEncoder(null).getCharsetEncoder());
248253
}
249254

255+
@Test
256+
public void testResetCharsetName() {
257+
assertNotNull(ReaderInputStream.builder().setReader(new StringReader("\uD800")).setCharset((String) null).getCharset());
258+
}
259+
250260
@Test
251261
public void testUTF16WithSingleByteRead() throws IOException {
252262
testWithSingleByteRead(TEST_STRING, UTF_16);

0 commit comments

Comments
 (0)