@Override public final int read(byte[] pBytes) throws IOException { return read(pBytes, 0, pBytes != null ? pBytes.length : 1); }
while (toSkip > 0 && read() >= 0) { toSkip--;
@Test public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final int size = 1024; // Fill bytes byte[] bytes = new byte[size * 2]; sRandom.nextBytes(bytes); // Create wrapper stream SeekableInputStream stream = makeInputStream(bytes); // Read to fill the buffer, then reset int val; val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; readFully(stream, result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); readFully(stream, result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
@Override public final int read(byte[] pBytes) throws IOException { return read(pBytes, 0, pBytes != null ? pBytes.length : 1); }
@Override public final int read(byte[] pBytes) throws IOException { return read(pBytes, 0, pBytes != null ? pBytes.length : 1); }
@Test public void testReadAllByteValuesRegression() throws IOException { final int size = 128; // Fill bytes byte[] bytes = new byte[256]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) i; } // Create wrapper stream SeekableInputStream stream = makeInputStream(bytes); // Fill buffer byte[] buffer = new byte[size]; while (stream.read(buffer) >= 0) { } stream.seek(0); for (int i = 0; i < bytes.length; i += 2) { assertEquals("Wrong stream position", i, stream.getStreamPosition()); int count = stream.read(buffer, 0, 2); assertEquals(2, count); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], buffer[0]); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i + 1], buffer[1]); } stream.seek(0); for (int i = 0; i < bytes.length; i++) { assertEquals("Wrong stream position", i, stream.getStreamPosition()); int actual = stream.read(); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i] & 0xff, actual); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], (byte) actual); } }
@Test public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }
while (toSkip > 0 && read() >= 0) { toSkip--;
public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final int size = 1024; // Fill bytes byte[] bytes = new byte[size * 2]; sRandom.nextBytes(bytes); // Create wrapper stream SeekableInputStream stream = makeInputStream(bytes); // Read to fill the buffer, then reset int val; val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; readFully(stream, result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); readFully(stream, result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
@Test public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final int size = 1024; // Fill bytes byte[] bytes = new byte[size * 2]; sRandom.nextBytes(bytes); // Create wrapper stream SeekableInputStream stream = makeInputStream(bytes); // Read to fill the buffer, then reset int val; val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); val = stream.read(); assertFalse("Unexepected EOF", val == -1); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; readFully(stream, result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); readFully(stream, result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
@Test public void testReadAllByteValuesRegression() throws IOException { final int size = 128; // Fill bytes byte[] bytes = new byte[256]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) i; } // Create wrapper stream SeekableInputStream stream = makeInputStream(bytes); // Fill buffer byte[] buffer = new byte[size]; while (stream.read(buffer) >= 0) { } stream.seek(0); for (int i = 0; i < bytes.length; i += 2) { assertEquals("Wrong stream position", i, stream.getStreamPosition()); int count = stream.read(buffer, 0, 2); assertEquals(2, count); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], buffer[0]); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i + 1], buffer[1]); } stream.seek(0); for (int i = 0; i < bytes.length; i++) { assertEquals("Wrong stream position", i, stream.getStreamPosition()); int actual = stream.read(); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i] & 0xff, actual); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], (byte) actual); } }
public void testReadAllByteValuesRegression() throws IOException { final int size = 128; // Fill bytes byte[] bytes = new byte[256]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) i; } // Create wrapper stream SeekableInputStream stream = makeInputStream(bytes); // Fill buffer byte[] buffer = new byte[size]; while (stream.read(buffer) >= 0) { } stream.seek(0); for (int i = 0; i < bytes.length; i += 2) { assertEquals("Wrong stream position", i, stream.getStreamPosition()); int count = stream.read(buffer, 0, 2); assertEquals(2, count); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], buffer[0]); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i + 1], buffer[1]); } stream.seek(0); for (int i = 0; i < bytes.length; i++) { assertEquals("Wrong stream position", i, stream.getStreamPosition()); int actual = stream.read(); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i] & 0xff, actual); assertEquals(String.format("Wrong value read at pos %d", stream.getStreamPosition()), bytes[i], (byte) actual); } }
public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }
@Test public void testCloseUnderlyingStream() throws IOException { final boolean[] closed = new boolean[1]; ByteArrayInputStream input = new ByteArrayInputStream(makeRandomArray(256)) { @Override public void close() throws IOException { closed[0] = true; super.close(); } }; SeekableInputStream stream = makeInputStream(input); try { FileUtil.read(stream); // Read until EOF assertEquals("EOF not reached (test case broken)", -1, stream.read()); assertFalse("Underlying stream closed before close", closed[0]); } finally { stream.close(); } assertTrue("Underlying stream not closed", closed[0]); }