You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/01/11 13:33:59 UTC

[commons-io] branch master updated: ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to the platform default instead of throwing an NullPointerException.

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


The following commit(s) were added to refs/heads/master by this push:
     new e6fb479  ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to the platform default instead of throwing an NullPointerException.
e6fb479 is described below

commit e6fb4794603ce0ffced5fd5115a4a2302f3d754a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 08:33:57 2022 -0500

    ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to
    the platform default instead of throwing an NullPointerException.
---
 src/changes/changes.xml                            |  3 ++
 .../apache/commons/io/input/ReaderInputStream.java | 24 ++++++++++++----
 .../commons/io/input/ReaderInputStreamTest.java    | 33 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c6d4778..690bac6 100644
--- a/src/changes/changes.xml
+++ b/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.
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 9933f98..f2fb01f 100644
--- a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
@@ -29,6 +29,9 @@ import java.nio.charset.CoderResult;
 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 class ReaderInputStream extends InputStream {
     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 class ReaderInputStream extends InputStream {
      * 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 class ReaderInputStream extends InputStream {
      * 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);
     }
 
     /**
@@ -245,6 +248,15 @@ public class ReaderInputStream extends InputStream {
     }
 
     /**
+     * Gets the CharsetEncoder.
+     *
+     * @return the CharsetEncoder.
+     */
+    CharsetEncoder getCharsetEncoder() {
+        return charsetEncoder;
+    }
+
+    /**
      * Read a single byte.
      *
      * @return either the byte read or {@code -1} if the end of the stream has been reached
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 73a521e..f6750ff 100644
--- a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
@@ -121,6 +121,39 @@ public class ReaderInputStreamTest {
     }
 
     @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");
     }