ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to
the platform default instead of throwing an NullPointerException.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -126,6 +126,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-751" dev="ggregory" type="fix" due-to="Gary Gregory, Richard Cyganiak">
When deleting symlinks, File/PathUtils.deleteDirectory() changes file permissions of the target.
</action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to the platform default instead of throwing an NullPointerException.
+ </action>
<!-- ADD -->
<action issue="IO-726" dev="ggregory" type="fix" due-to="shollander, Gary Gregory">
Add MemoryMappedFileInputStream #215.

MODIFY
src/main/java/org/apache/commons/io/input/ReaderInputStream.java
src/main/java/org/apache/commons/io/input/ReaderInputStream.java
import java.nio.charset.CodingErrorAction;
import java.util.Objects;
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.charset.CharsetEncoders;
+
/**
* {@link InputStream} implementation that reads a character stream from a {@link Reader} and transforms it to a byte
* 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) {
public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) {
// @formatter:off
this(reader,
- charset.newEncoder()
+ Charsets.toCharset(charset).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE),
bufferSize);
@@ -168,14 +171,14 @@ public ReaderInputStream(final Reader reader, final CharsetEncoder charsetEncode
* Constructs a new {@link ReaderInputStream}.
*
* @param reader the target {@link Reader}
- * @param charsetEncoder the charset encoder
+ * @param charsetEncoder the charset encoder, null defauls to the default Charset encoder.
* @param bufferSize the size of the input buffer in number of characters
* @since 2.1
*/
public ReaderInputStream(final Reader reader, final CharsetEncoder charsetEncoder, final int bufferSize) {
this.reader = reader;
- this.charsetEncoder = charsetEncoder;
- this.encoderIn = CharBuffer.allocate(checkMinBufferSize(charsetEncoder, bufferSize));
+ this.charsetEncoder = CharsetEncoders.toCharsetEncoder(charsetEncoder);
+ this.encoderIn = CharBuffer.allocate(checkMinBufferSize(this.charsetEncoder, bufferSize));
this.encoderIn.flip();
this.encoderOut = ByteBuffer.allocate(128);
this.encoderOut.flip();
@@ -196,11 +199,11 @@ public ReaderInputStream(final Reader reader, final String charsetName) {
* Constructs a new {@link ReaderInputStream}.
*
* @param reader the target {@link Reader}
- * @param charsetName the name of the charset encoding
+ * @param charsetName the name of the charset encoding, null maps to the default Charset.
* @param bufferSize the size of the input buffer in number of characters
*/
public ReaderInputStream(final Reader reader, final String charsetName, final int bufferSize) {
- this(reader, Charset.forName(charsetName), bufferSize);
+ this(reader, Charsets.toCharset(charsetName), bufferSize);
}
/**
@@ -244,6 +247,15 @@ private void fillBuffer() throws IOException {
encoderOut.flip();
}
+ /**
+ * Gets the CharsetEncoder.
+ *
+ * @return the CharsetEncoder.
+ */
+ CharsetEncoder getCharsetEncoder() {
+ return charsetEncoder;
+ }
+
/**
* Read a single byte.
*

MODIFY
src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
@@ -120,6 +120,39 @@ public void testCodingErrorAction() throws IOException {
}
}
+ @Test
+ @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
+ public void testConstructNullCharsetEncoder() throws IOException {
+ final Charset charset = Charset.defaultCharset();
+ final CharsetEncoder encoder = null;
+ try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
+ IOUtils.toByteArray(in);
+ assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+ }
+ }
+
+ @Test
+ @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
+ public void testConstructNullCharset() throws IOException {
+ final Charset charset = Charset.defaultCharset();
+ final Charset encoder = null;
+ try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
+ IOUtils.toByteArray(in);
+ assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+ }
+ }
+
+ @Test
+ @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
+ public void testConstructNullCharsetNameEncoder() throws IOException {
+ final Charset charset = Charset.defaultCharset();
+ final String encoder = null;
+ try (ReaderInputStream in = new ReaderInputStream(new StringReader("ABC"), encoder, (int) ReaderInputStream.minBufferSize(charset.newEncoder()))) {
+ IOUtils.toByteArray(in);
+ assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+ }
+ }
+
@Test
public void testLargeUTF8WithBufferedRead() throws IOException {
testWithBufferedRead(LARGE_TEST_STRING, "UTF-8");

ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to
the platform default instead of throwing an NullPointerException.