private ImageInputStream createBufferedInput(final ImageInputStream input) throws IOException { return input instanceof BufferedImageInputStream ? input : new BufferedImageInputStream(input); }
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); } }
@Override public short readShort() throws IOException { readFully(integralCache.array(), 0, 2); return integralCache.getShort(0); }
@Test public void testCreate() throws IOException { new BufferedImageInputStream(new ByteArrayImageInputStream(new byte[0])); }
@Test(expected = IllegalArgumentException.class) public void testCreateNegativeLength() throws IOException { new SubImageInputStream(createStream(0), -1); fail("Expected IllegalArgumentException with negative 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 testWraphBeyondWrappedLength() throws IOException { SubImageInputStream stream = new SubImageInputStream(createStream(5), 6); assertEquals(5, stream.length()); }
@Override public int read() throws IOException { checkClosed(); if (!buffer.hasRemaining()) { fillBuffer(); } if (!buffer.hasRemaining()) { return -1; } bitOffset = 0; streamPos++; return buffer.get() & 0xff; }
@Override public void write(int b) throws IOException { flushBits(); stream.write(b); streamPos++; }
@Override public void flushBefore(long pos) throws IOException { checkClosed(); stream.flushBefore(pos); }
@Override Directory parseDirectory() throws IOException { // The data is in essence an embedded TIFF file. return new TIFFReader().read(new ByteArrayImageInputStream(data)); }
@Test public void testSeekAfterEOF() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(7), 5); stream.seek(6); assertEquals(-1, stream.read()); }
@Override public int readInt() throws IOException { readFully(integralCache.array(), 0, 4); return integralCache.getInt(0); }
@Override public void write(byte[] b, int off, int len) throws IOException { flushBits(); stream.write(b, off, len); streamPos += len; }
Directory parseDirectory() throws IOException { // Chop off potential trailing null-termination/padding that SAX parsers don't like... int len = data.length; for (; len > 0; len--) { if (data[len - 1] != 0) { break; } } return new XMPReader().read(new ByteArrayImageInputStream(data, 0, len)); } }
@Test public void testCreate() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(11), 7); assertEquals(0, stream.getStreamPosition()); assertEquals(7, stream.length()); }
@Override public long readLong() throws IOException { readFully(integralCache.array(), 0, 8); return integralCache.getLong(0); }
@Override Directory parseDirectory() throws IOException { return new IPTCReader().read(new ByteArrayImageInputStream(data)); }
@Test public void testSeek() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(7), 5); stream.seek(5); assertEquals(5, stream.getStreamPosition()); stream.seek(1); assertEquals(1, stream.getStreamPosition()); } }