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 3ad3bdb8 [IO-781] Fix CharSequenceInputStream coding exception 
handling (#537)
3ad3bdb8 is described below

commit 3ad3bdb8dba2bc4275666e89553f767f565ead6e
Author: Marcono1234 <[email protected]>
AuthorDate: Tue Dec 26 19:57:25 2023 +0100

    [IO-781] Fix CharSequenceInputStream coding exception handling (#537)
---
 .../apache/commons/io/input/CharSequenceInputStream.java   |  1 +
 .../commons/io/input/CharSequenceInputStreamTest.java      | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git 
a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java 
b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
index 5b53f5d2..195244eb 100644
--- a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java
@@ -186,6 +186,7 @@ public class CharSequenceInputStream extends InputStream {
             // Reset everything without filling the buffer
             // so the same exception can be thrown again later.
             this.bBuf.clear();
+            this.bBuf.flip();
             this.cBuf.rewind();
         }
     }
diff --git 
a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java 
b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
index cbdfc9eb..2c2827d7 100644
--- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
@@ -20,6 +20,7 @@ import static 
org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
@@ -31,7 +32,9 @@ import java.nio.CharBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CoderResult;
+import java.nio.charset.CodingErrorAction;
 import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnmappableCharacterException;
 import java.util.Random;
 
 import org.apache.commons.io.CharsetsTest;
@@ -524,4 +527,15 @@ public class CharSequenceInputStreamTest {
             assertEquals(-1, r.read(), csName);
         }
     }
+
+    @Test
+    public void testCharacterCodingException() throws IOException {
+        final Charset charset = StandardCharsets.US_ASCII;
+        final CharSequenceInputStream in = CharSequenceInputStream.builder()
+            
.setCharsetEncoder(charset.newEncoder().onUnmappableCharacter(CodingErrorAction.REPORT))
+            .setCharSequence("\u0080")
+            .get();
+        assertEquals(0, in.available());
+        assertThrows(UnmappableCharacterException.class, in::read);
+    }
 }

Reply via email to