@Test public void testReadArrayOffLen() throws IOException { byte[] data = new byte[1024 * 1024]; random.nextBytes(data); int offset = random.nextInt(data.length - 10240); int length = 10240; ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data, offset, length); assertEquals("Data length should be same as stream length", length, stream.length()); byte[] result = new byte[1024]; for (int i = 0; i < length / result.length; i++) { stream.readFully(result); assertTrue("Wrong data read: " + i, rangeEquals(data, offset + i * result.length, result, 0, result.length)); } }
@Test public void testReadArray() throws IOException { byte[] data = new byte[1024 * 1024]; random.nextBytes(data); ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data); assertEquals("Data length should be same as stream length", data.length, stream.length()); byte[] result = new byte[1024]; for (int i = 0; i < data.length / result.length; i++) { stream.readFully(result); assertTrue("Wrong data read: " + i, rangeEquals(data, i * result.length, result, 0, result.length)); } }
@Test public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE; // Fill bytes byte[] bytes = new byte[size * 2]; random.nextBytes(bytes); // Create wrapper stream BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes)); // Read to fill the buffer, then reset stream.readLong(); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; stream.readFully(result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); stream.readFully(result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
@Test public void testReadSkip() throws IOException { byte[] data = new byte[1024 * 14]; random.nextBytes(data); ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data); assertEquals("Data length should be same as stream length", data.length, stream.length()); byte[] result = new byte[7]; for (int i = 0; i < data.length / result.length; i += 2) { stream.readFully(result); stream.skipBytes(result.length); assertTrue("Wrong data read: " + i, rangeEquals(data, i * result.length, result, 0, result.length)); } }
@Test public void testReadSeek() throws IOException { byte[] data = new byte[1024 * 18]; random.nextBytes(data); ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data); assertEquals("Data length should be same as stream length", data.length, stream.length()); byte[] result = new byte[9]; for (int i = 0; i < data.length / result.length; i++) { // Read backwards long newPos = stream.length() - result.length - i * result.length; stream.seek(newPos); assertEquals("Wrong stream position", newPos, stream.getStreamPosition()); stream.readFully(result); assertTrue("Wrong data read: " + i, rangeEquals(data, (int) newPos, result, 0, result.length)); } } }