@Override public int read() throws IOException { for (;;) { if (this.bbuf.hasRemaining()) { return this.bbuf.get() & 0xFF; } fillBuffer(); if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { return EOF; } } }
private void testIO_356(final int bufferSize, final int dataSize, final int readFirst, final String csName) throws Exception { final CharSequenceInputStream is = new CharSequenceInputStream(ALPHABET, csName, bufferSize); for (int i = 0; i < readFirst; i++) { final int ch = is.read(); assertFalse(ch == -1); } is.mark(dataSize); final byte[] data1 = new byte[dataSize]; final int readCount1 = is.read(data1); assertEquals(dataSize, readCount1); is.reset(); // should allow data to be re-read final byte[] data2 = new byte[dataSize]; final int readCount2 = is.read(data2); assertEquals(dataSize, readCount2); is.close(); // data buffers should be identical assertArrayEquals("bufferSize=" + bufferSize + " dataSize=" + dataSize, data1, data2); }
@Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
private void testCharsetMismatchInfiniteLoop(final String csName) throws IOException { // Input is UTF-8 bytes: 0xE0 0xB2 0xA0 final char[] inputChars = new char[] { (char) 0xE0, (char) 0xB2, (char) 0xA0 }; final Charset charset = Charset.forName(csName); // infinite loop for US-ASCII, UTF-8 OK try (InputStream stream = new CharSequenceInputStream(new String(inputChars), charset, 512)) { while (stream.read() != -1) { } } }
@Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
private void testReadZero(final String csName) throws Exception { try (InputStream r = new CharSequenceInputStream("test", csName)) { final byte[] bytes = new byte[30]; assertEquals(0, r.read(bytes, 0, 0)); } }
bytesRead += chunk; } else { fillBuffer(); if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { break;
@Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
@Test public void testReadZero_EmptyString() throws Exception { try (InputStream r = new CharSequenceInputStream("", "UTF-8")) { final byte[] bytes = new byte[30]; assertEquals(0, r.read(bytes, 0, 0)); } }
this.bbuf.rewind(); // empty the buffer (we only refill when empty during normal processing) this.bbuf.limit(0); fillBuffer();
@Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
@Test public void testMarkSupported() throws Exception { try (InputStream r = new CharSequenceInputStream("test", "UTF-8")) { assertTrue(r.markSupported()); } }
@Override public int read() throws IOException { for (;;) { if (this.bbuf.hasRemaining()) { return this.bbuf.get() & 0xFF; } fillBuffer(); if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { return EOF; } } }
@Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
private void testSkip(final String csName) throws Exception { try (InputStream r = new CharSequenceInputStream("test", csName)) { assertEquals(1, r.skip(1)); assertEquals(2, r.skip(2)); assertEquals(csName, 't', r.read()); r.skip(100); assertEquals(csName, -1, r.read()); } }
@Override public int read() throws IOException { for (;;) { if (this.bbuf.hasRemaining()) { return this.bbuf.get() & 0xFF; } fillBuffer(); if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { return EOF; } } }
private void testSingleByteRead(final String testString, final String charsetName) throws IOException { final byte[] bytes = testString.getBytes(charsetName); try (InputStream in = new CharSequenceInputStream(testString, charsetName, 512)) { for (final byte b : bytes) { final int read = in.read(); assertTrue("read " + read + " >=0 ", read >= 0); assertTrue("read " + read + " <= 255", read <= 255); assertEquals("Should agree with input", b, (byte) read); } assertEquals(-1, in.read()); } }