public ImageInputStream createInputStreamInstance(Object pInput, boolean pUseCache, File pCacheDir) throws IOException { if (pInput instanceof byte[]) { return new ByteArrayImageInputStream((byte[]) pInput); } else { throw new IllegalArgumentException("Expected input of type byte[]: " + pInput); } }
@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)); } } }
@Test public void testReadOffsetLen() throws IOException { byte[] data = new byte[1024 * 1024]; random.nextBytes(data); int offset = random.nextInt(data.length / 10); int length = random.nextInt(data.length - offset); ByteArrayImageInputStream stream = new ByteArrayImageInputStream(data, offset, length); assertEquals("Data length should be same as stream length", length, stream.length()); for (int i = offset; i < offset + length; i++) { assertEquals("Wrong data read", data[i] & 0xff, stream.read()); } }
@Test public void testCreate() { ByteArrayImageInputStream stream = new ByteArrayImageInputStream(new byte[0]); assertEquals("Data length should be same as stream length", 0, stream.length()); }
@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 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 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()); }
@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)); } }
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 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 testCreate() { ByteArrayImageInputStream stream = new ByteArrayImageInputStream(new byte[0]); assertEquals("Data length should be same as stream length", 0, stream.length()); }
IIOMetadata readMetadata() throws IOException { ImageInputStream input = new ByteArrayImageInputStream(segment.thumbnail); try { reader.setInput(input); return reader.getImageMetadata(0); } finally { input.close(); } }
@Test public void testRead() 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()); for (byte b : data) { assertEquals("Wrong data read", b & 0xff, stream.read()); } }
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 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)); } } }
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 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 testCreate() { ByteArrayImageInputStream stream = new ByteArrayImageInputStream(new byte[0]); assertEquals("Data length should be same as stream length", 0, stream.length()); }
private BufferedImage readJPEGCached(boolean pixelsExposed) throws IOException { BufferedImage thumbnail = cachedThumbnail != null ? cachedThumbnail.get() : null; if (thumbnail == null) { ImageInputStream stream = new ByteArrayImageInputStream(segment.thumbnail); try { thumbnail = readJPEGThumbnail(reader, stream); } finally { stream.close(); } } cachedThumbnail = pixelsExposed ? null : new SoftReference<BufferedImage>(thumbnail); return thumbnail; }
public void testRead() 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()); for (byte b : data) { assertEquals("Wrong data read", b & 0xff, stream.read()); } }