/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
@Test public void testCharsetMismatchInfiniteLoop() throws IOException { // Input is UTF-8 bytes: 0xE0 0xB2 0xA0 final char[] inputChars = new char[] { (char) 0xE0, (char) 0xB2, (char) 0xA0 }; // Charset charset = Charset.forName("UTF-8"); // works final Charset charset = Charset.forName("ASCII"); // infinite loop try (ReaderInputStream stream = new ReaderInputStream(new CharArrayReader(inputChars), charset)) { while (stream.read() != -1) { } } } }
@SuppressWarnings("deprecation") @Test public void testReadZeroEmptyString() throws Exception { final ReaderInputStream r = new ReaderInputStream(new StringReader("")); final byte[] bytes = new byte[30]; // Should always return 0 for length == 0 assertEquals(0, r.read(bytes, 0, 0)); assertEquals(-1, r.read(bytes, 0, 1)); assertEquals(0, r.read(bytes, 0, 0)); assertEquals(-1, r.read(bytes, 0, 1)); r.close(); }
@SuppressWarnings("deprecation") @Test public void testReadZero() throws Exception { final String inStr = "test"; final ReaderInputStream r = new ReaderInputStream(new StringReader(inStr)); final byte[] bytes = new byte[30]; assertEquals(0, r.read(bytes, 0, 0)); assertEquals(inStr.length(), r.read(bytes, 0, inStr.length()+1)); // Should always return 0 for length == 0 assertEquals(0, r.read(bytes, 0, 0)); r.close(); }
private void testWithSingleByteRead(final String testString, final String charsetName) throws IOException { final byte[] bytes = testString.getBytes(charsetName); final ReaderInputStream in = new ReaderInputStream(new StringReader(testString), charsetName); for (final byte b : bytes) { final int read = in.read(); assertTrue(read >= 0); assertTrue(read <= 255); assertEquals(b, (byte)read); } assertEquals(-1, in.read()); in.close(); }
private void testWithBufferedRead(final String testString, final String charsetName) throws IOException { final byte[] expected = testString.getBytes(charsetName); final ReaderInputStream in = new ReaderInputStream(new StringReader(testString), charsetName); final byte[] buffer = new byte[128]; int offset = 0; while (true) { int bufferOffset = random.nextInt(64); final int bufferLength = random.nextInt(64); int read = in.read(buffer, bufferOffset, bufferLength); if (read == -1) { assertEquals(offset, expected.length); break; } else { assertTrue(read <= bufferLength); while (read > 0) { assertTrue(offset < expected.length); assertEquals(expected[offset], buffer[bufferOffset]); offset++; bufferOffset++; read--; } } } in.close(); }
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }