1818
1919import static org .junit .jupiter .api .Assertions .assertEquals ;
2020import static org .junit .jupiter .api .Assertions .assertTrue ;
21+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2122
2223import java .io .CharArrayReader ;
2324import java .io .IOException ;
25+ import java .io .InputStream ;
2426import java .io .StringReader ;
27+ import java .nio .charset .CharacterCodingException ;
2528import java .nio .charset .Charset ;
29+ import java .nio .charset .CharsetEncoder ;
30+ import java .nio .charset .CodingErrorAction ;
2631import java .nio .charset .StandardCharsets ;
2732import java .util .Random ;
33+ import java .util .concurrent .TimeUnit ;
2834
2935import org .junit .jupiter .api .Test ;
36+ import org .junit .jupiter .api .Timeout ;
3037
3138public class ReaderInputStreamTest {
3239 private static final String TEST_STRING = "\u00e0 peine arriv\u00e9 s nous entr\u00e2 mes dans sa chambre" ;
3340 private static final String LARGE_TEST_STRING ;
3441
3542 static {
3643 final StringBuilder buffer = new StringBuilder ();
37- for (int i = 0 ; i < 100 ; i ++) {
44+ for (int i = 0 ; i < 100 ; i ++) {
3845 buffer .append (TEST_STRING );
3946 }
4047 LARGE_TEST_STRING = buffer .toString ();
@@ -48,7 +55,7 @@ public class ReaderInputStreamTest {
4855 @ Test
4956 public void testCharsetMismatchInfiniteLoop () throws IOException {
5057 // Input is UTF-8 bytes: 0xE0 0xB2 0xA0
51- final char [] inputChars = { (char ) 0xE0 , (char ) 0xB2 , (char ) 0xA0 };
58+ final char [] inputChars = {(char ) 0xE0 , (char ) 0xB2 , (char ) 0xA0 };
5259 // Charset charset = Charset.forName("UTF-8"); // works
5360 final Charset charset = StandardCharsets .US_ASCII ; // infinite loop
5461 try (ReaderInputStream stream = new ReaderInputStream (new CharArrayReader (inputChars ), charset )) {
@@ -57,6 +64,20 @@ public void testCharsetMismatchInfiniteLoop() throws IOException {
5764 }
5865 }
5966
67+ /**
68+ * Tests IO-717 to avoid infinite loops.
69+ *
70+ * ReaderInputStream does not throw exception with {@link CodingErrorAction#REPORT}.
71+ */
72+ @ Test
73+ @ Timeout (value = 500 , unit = TimeUnit .MILLISECONDS )
74+ public void testCodingErrorAction () throws IOException {
75+ CharsetEncoder encoder = StandardCharsets .UTF_8 .newEncoder ().onMalformedInput (CodingErrorAction .REPORT );
76+ try (InputStream in = new ReaderInputStream (new StringReader ("\uD800 aa" ), encoder , 2 )) {
77+ assertThrows (CharacterCodingException .class , in ::read );
78+ }
79+ }
80+
6081 @ Test
6182 public void testLargeUTF8WithBufferedRead () throws IOException {
6283 testWithBufferedRead (LARGE_TEST_STRING , "UTF-8" );
0 commit comments