#153987 - don't hang with invalid input data but throw IOException.
1.1 --- a/openide.util/src/org/openide/util/io/ReaderInputStream.java Thu Nov 20 15:50:28 2008 -0500
1.2 +++ b/openide.util/src/org/openide/util/io/ReaderInputStream.java Fri Nov 28 07:46:43 2008 +0100
1.3 @@ -94,9 +94,14 @@
1.4 osw.flush();
1.5 pos.flush();
1.6
1.7 - return pis.read();
1.8 + if (pis.available() > 0) {
1.9 + return pis.read();
1.10 + } else {
1.11 + throw new IOException("Cannot encode input data using " + osw.getEncoding() + " encoding."); // NOI18N
1.12 + }
1.13 }
1.14
1.15 + @Override
1.16 public int read(byte[] b, int off, int len) throws IOException {
1.17 if (len == 0) {
1.18 return 0;
1.19 @@ -126,6 +131,7 @@
1.20 return i;
1.21 }
1.22
1.23 + @Override
1.24 public int available() throws IOException {
1.25 int i = pis.available();
1.26
1.27 @@ -141,6 +147,7 @@
1.28 }
1.29 }
1.30
1.31 + @Override
1.32 public void close() throws IOException {
1.33 reader.close();
1.34 osw.close();
2.1 --- a/openide.util/test/unit/src/org/openide/util/io/ReaderInputStreamTest.java Thu Nov 20 15:50:28 2008 -0500
2.2 +++ b/openide.util/test/unit/src/org/openide/util/io/ReaderInputStreamTest.java Fri Nov 28 07:46:43 2008 +0100
2.3 @@ -39,6 +39,7 @@
2.4
2.5 package org.openide.util.io;
2.6
2.7 +import java.io.IOException;
2.8 import java.io.StringReader;
2.9 import org.netbeans.junit.NbTestCase;
2.10
2.11 @@ -52,4 +53,19 @@
2.12 assertEquals(0, new ReaderInputStream(new StringReader(("abc"))).read(new byte[256], 0, 0));
2.13 }
2.14
2.15 + public void testTextDataRead() throws IOException {
2.16 + ReaderInputStream ris = new ReaderInputStream(new StringReader("0123456789"), "UTF-8");
2.17 + assertEquals("Wrong number of bytes read.", 10, ris.read(new byte[10], 0, 10));
2.18 + }
2.19 +
2.20 + /** Tests stream doesn't hang with invalid input data (see #153987). */
2.21 + public void testBinaryDataRead() throws IOException {
2.22 + ReaderInputStream ris = new ReaderInputStream(new StringReader(""+((char)0xD8FF)), "UTF-8");
2.23 + try {
2.24 + ris.read(new byte[10], 0, 10);
2.25 + fail("ReaderInputStream should refuse input characteres between \\uD800 and \\uDBFF (see OutputStreamWriter javadoc).");
2.26 + } catch (IOException e) {
2.27 + // OK
2.28 + }
2.29 + }
2.30 }