CharSequenceInputStream maps null Charset and Charset name to the
platform default instead of throwing a NullPointerException.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -127,7 +127,10 @@ The <action> type attribute can be add,update,fix,remove.
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.
+ ReaderInputStream maps null Charset, Charset name, and CharsetEmcoder to the platform default instead of throwing a NullPointerException.
+ </action>
+ <action dev="ggregory" type="fix" due-to="Gary Gregory">
+ CharSequenceInputStream maps null Charset and Charset name to the platform default instead of throwing a NullPointerException.
</action>
<!-- ADD -->
<action issue="IO-726" dev="ggregory" type="fix" due-to="shollander, Gary Gregory">

MODIFY
src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
import java.nio.charset.CodingErrorAction;
import java.util.Objects;
+import org.apache.commons.io.Charsets;
+
/**
* Implements an {@link InputStream} to read from String, StringBuffer, StringBuilder or CharBuffer.
* <p>
@@ -66,13 +68,13 @@ public CharSequenceInputStream(final CharSequence cs, final Charset charset) {
* Constructs a new instance.
*
* @param cs the input character sequence.
- * @param charset the character set name to use.
+ * @param charset the character set name to use, null maps to the default Charset.
* @param bufferSize the buffer size to use.
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character.
*/
public CharSequenceInputStream(final CharSequence cs, final Charset charset, final int bufferSize) {
// @formatter:off
- this.charsetEncoder = charset.newEncoder()
+ this.charsetEncoder = Charsets.toCharset(charset).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
// @formatter:on
@@ -99,12 +101,12 @@ public CharSequenceInputStream(final CharSequence cs, final String charset) {
* Constructs a new instance.
*
* @param cs the input character sequence.
- * @param charset the character set name to use.
+ * @param charset the character set name to use, null maps to the default Charset.
* @param bufferSize the buffer size to use.
* @throws IllegalArgumentException if the buffer is not large enough to hold a complete character.
*/
public CharSequenceInputStream(final CharSequence cs, final String charset, final int bufferSize) {
- this(cs, Charset.forName(charset), bufferSize);
+ this(cs, Charsets.toCharset(charset), bufferSize);
}
/**
@@ -142,6 +144,15 @@ private void fillBuffer() throws CharacterCodingException {
this.bBuf.flip();
}
+ /**
+ * Gets the CharsetEncoder.
+ *
+ * @return the CharsetEncoder.
+ */
+ CharsetEncoder getCharsetEncoder() {
+ return charsetEncoder;
+ }
+
/**
* {@inheritDoc}
* @param readlimit max read limit (ignored).

MODIFY
src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
@@ -74,6 +74,22 @@ private boolean isOddBallLegacyCharsetThatDoesNotSupportFrenchCharacters(final S
"Shift_JIS".equalsIgnoreCase(csName);
}
+ @Test
+ public void testNullCharset() throws IOException {
+ try (CharSequenceInputStream in = new CharSequenceInputStream("A", (Charset) null)) {
+ IOUtils.toByteArray(in);
+ assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+ }
+ }
+
+ @Test
+ public void testNullCharsetName() throws IOException {
+ try (CharSequenceInputStream in = new CharSequenceInputStream("A", (String) null)) {
+ IOUtils.toByteArray(in);
+ assertEquals(Charset.defaultCharset(), in.getCharsetEncoder().charset());
+ }
+ }
+
@Test
public void testAvailable() throws Exception {
for (final String csName : Charset.availableCharsets().keySet()) {

CharSequenceInputStream maps null Charset and Charset name to the
platform default instead of throwing a NullPointerException.