public void testCreateNull() throws IOException { try { new BufferedImageInputStream(null); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertNotNull("Null exception message", expected.getMessage()); String message = expected.getMessage().toLowerCase(); assertTrue("Exception message does not contain parameter name", message.contains("stream")); assertTrue("Exception message does not contain null", message.contains("null")); } }
public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final 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)); }
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)); } }
public void testArrayIndexOutOfBoundsBufferedReadBug() throws IOException { // TODO: Create a more straight forward way to prove correctness, for now this is good enough to avoid regression ImageInputStream input = new BufferedImageInputStream(new MemoryCacheImageInputStream(getClass().getResourceAsStream("/Thumbs-camera.db"))); input.setByteOrder(ByteOrder.LITTLE_ENDIAN); Entry root = new CompoundDocument(input).getRootEntry(); Entry child = root.getChildEntry("Catalog"); assertNotNull("Input stream can never be null", child.getInputStream()); }
public void testBufferPositionCorrect() throws IOException { // Fill bytes byte[] bytes = new byte[1024]; mRandom.nextBytes(bytes); ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes); input.readByte(); input.readByte(); input.skipBytes(124); input.readByte(); input.readByte(); // Sanity check assertEquals(128, input.getStreamPosition()); BufferedImageInputStream stream = new BufferedImageInputStream(input); assertEquals(input.getStreamPosition(), stream.getStreamPosition()); stream.skipBytes(128); //assertTrue(256 <= input.getStreamPosition()); assertEquals(256, stream.getStreamPosition()); stream.seek(1020); assertEquals(1020, stream.getStreamPosition()); }
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)); } }
public void testArrayIndexOutOfBoundsBufferedReadBug() throws IOException { // TODO: Create a more straight forward way to prove correctness, for now this is good enough to avoid regression ImageInputStream input = new BufferedImageInputStream(new MemoryCacheImageInputStream(getClass().getResourceAsStream("/Thumbs-camera.db"))); input.setByteOrder(ByteOrder.LITTLE_ENDIAN); Entry root = new CompoundDocument(input).getRootEntry(); Entry child = root.getChildEntry("Catalog"); assertNotNull("Input stream can never be null", child.getInputStream()); }
public void testBufferPositionCorrect() throws IOException { // Fill bytes byte[] bytes = new byte[1024]; random.nextBytes(bytes); ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes); input.readByte(); input.readByte(); input.skipBytes(124); input.readByte(); input.readByte(); // Sanity check assertEquals(128, input.getStreamPosition()); BufferedImageInputStream stream = new BufferedImageInputStream(input); assertEquals(input.getStreamPosition(), stream.getStreamPosition()); stream.skipBytes(128); //assertTrue(256 <= input.getStreamPosition()); assertEquals(256, stream.getStreamPosition()); stream.seek(1020); assertEquals(1020, stream.getStreamPosition()); }
public void testCreateNull() throws IOException { try { new BufferedImageInputStream(null); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertNotNull("Null exception message", expected.getMessage()); String message = expected.getMessage().toLowerCase(); assertTrue("Exception message does not contain parameter name", message.contains("stream")); assertTrue("Exception message does not contain null", message.contains("null")); } }
public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE; // Fill bytes byte[] bytes = new byte[size * 2]; mRandom.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)); }
public void testReadArray() throws IOException { byte[] data = new byte[1024 * 1024]; mRandom.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)); } }
public void testReadSkip() throws IOException { byte[] data = new byte[1024 * 14]; mRandom.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)); } }
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)); } }
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)); } } }
public void testReadSeek() throws IOException { byte[] data = new byte[1024 * 18]; mRandom.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)); } } }