Skip to content

Commit e6fb479

Browse files
committed
ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to
the platform default instead of throwing an NullPointerException.
1 parent 7ffb81b commit e6fb479

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ The <action> type attribute can be add,update,fix,remove.
126126
<action issue="IO-751" dev="ggregory" type="fix" due-to="Gary Gregory, Richard Cyganiak">
127127
When deleting symlinks, File/PathUtils.deleteDirectory() changes file permissions of the target.
128128
</action>
129+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
130+
ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to the platform default instead of throwing an NullPointerException.
131+
</action>
129132
<!-- ADD -->
130133
<action issue="IO-726" dev="ggregory" type="fix" due-to="shollander, Gary Gregory">
131134
Add MemoryMappedFileInputStream #215.

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.nio.charset.CodingErrorAction;
3030
import java.util.Objects;
3131

32+
import org.apache.commons.io.Charsets;
33+
import org.apache.commons.io.charset.CharsetEncoders;
34+
3235
/**
3336
* {@link InputStream} implementation that reads a character stream from a {@link Reader} and transforms it to a byte
3437
* stream using a specified charset encoding. The stream is transformed using a {@link CharsetEncoder} object,
@@ -146,7 +149,7 @@ public ReaderInputStream(final Reader reader, final Charset charset) {
146149
public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) {
147150
// @formatter:off
148151
this(reader,
149-
charset.newEncoder()
152+
Charsets.toCharset(charset).newEncoder()
150153
.onMalformedInput(CodingErrorAction.REPLACE)
151154
.onUnmappableCharacter(CodingErrorAction.REPLACE),
152155
bufferSize);
@@ -168,14 +171,14 @@ public ReaderInputStream(final Reader reader, final CharsetEncoder charsetEncode
168171
* Constructs a new {@link ReaderInputStream}.
169172
*
170173
* @param reader the target {@link Reader}
171-
* @param charsetEncoder the charset encoder
174+
* @param charsetEncoder the charset encoder, null defauls to the default Charset encoder.
172175
* @param bufferSize the size of the input buffer in number of characters
173176
* @since 2.1
174177
*/
175178
public ReaderInputStream(final Reader reader, final CharsetEncoder charsetEncoder, final int bufferSize) {
176179
this.reader = reader;
177-
this.charsetEncoder = charsetEncoder;
178-
this.encoderIn = CharBuffer.allocate(checkMinBufferSize(charsetEncoder, bufferSize));
180+
this.charsetEncoder = CharsetEncoders.toCharsetEncoder(charsetEncoder);
181+
this.encoderIn = CharBuffer.allocate(checkMinBufferSize(this.charsetEncoder, bufferSize));
179182
this.encoderIn.flip();
180183
this.encoderOut = ByteBuffer.allocate(128);
181184
this.encoderOut.flip();
@@ -196,11 +199,11 @@ public ReaderInputStream(final Reader reader, final String charsetName) {
196199
* Constructs a new {@link ReaderInputStream}.
197200
*
198201
* @param reader the target {@link Reader}
199-
* @param charsetName the name of the charset encoding
202+
* @param charsetName the name of the charset encoding, null maps to the default Charset.
200203
* @param bufferSize the size of the input buffer in number of characters
201204
*/
202205
public ReaderInputStream(final Reader reader, final String charsetName, final int bufferSize) {
203-
this(reader, Charset.forName(charsetName), bufferSize);
206+
this(reader, Charsets.toCharset(charsetName), bufferSize);
204207
}
205208

206209
/**
@@ -244,6 +247,15 @@ private void fillBuffer() throws IOException {
244247
encoderOut.flip();
245248
}
246249

250+
/**
251+
* Gets the CharsetEncoder.
252+
*
253+
* @return the CharsetEncoder.
254+
*/
255+
CharsetEncoder getCharsetEncoder() {
256+
return charsetEncoder;
257+
}
258+
247259
/**
248260
* Read a single byte.
249261
*

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,39 @@ public void testCodingErrorAction() throws IOException {
120120
}
121121
}
122122

123+
@Test
124+
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
125+
public void testConstructNullCharsetEncoder() throws IOException {
126+
final Charset charset = Charset.defaultCharset();
127+
final CharsetEncoder encoder = null;
128+
try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
129+
IOUtils.toByteArray(in);
130+
assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
131+
}
132+
}
133+
134+
@Test
135+
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
136+
public void testConstructNullCharset() throws IOException {
137+
final Charset charset = Charset.defaultCharset();
138+
final Charset encoder = null;
139+
try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
140+
IOUtils.toByteArray(in);
141+
assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
142+
}
143+
}
144+
145+
@Test
146+
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
147+
public void testConstructNullCharsetNameEncoder() throws IOException {
148+
final Charset charset = Charset.defaultCharset();
149+
final String encoder = null;
150+
try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
151+
IOUtils.toByteArray(in);
152+
assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
153+
}
154+
}
155+
123156
@Test
124157
public void testLargeUTF8WithBufferedRead() throws IOException {
125158
testWithBufferedRead(LARGE_TEST_STRING, "UTF-8");

0 commit comments

Comments
 (0)