@Test public void testReadSubstream() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 9); for (int i = 0; i < 9; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } assertEquals("Read value after end of stream", -1, stream.read()); assertEquals("Read value after end of stream", -1, stream.read()); // Make sure we don't read outside stream boundaries assertTrue(input.getStreamPosition() <= 9); input.close(); }
public int read(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { if (hasLength && left <= 0) { return -1; } int read = input.read(pBytes, pOffset, (int) findMaxLen(pLength)); if (hasLength) { left = read < 0 ? 0 : left - read; } return read; }
@Test public void testReadSubstreamOpenEnd() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); input.seek(10); assertEquals(10, input.getStreamPosition()); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input); for (int i = 0; i < 10; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } assertEquals("Read value after end of stream", -1, stream.read()); assertEquals("Read value after end of stream", -1, stream.read()); // Make sure underlying stream is positioned at end of substream after close stream.close(); assertEquals(20, input.getStreamPosition()); input.close(); }
@Test public void testSeekBeforeStream() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); input.seek(10); assertEquals(10, input.getStreamPosition()); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 9); assertEquals("Should not skip backwards", 0, stream.skip(-5)); assertEquals(10, input.getStreamPosition()); } }
/** * Creates an {@code InputStream} adapter that reads from an underlying {@code ImageInputStream}. * The input stream will read until the end of {@code pStream}. * * @param pStream the stream to read from. * @return an {@code InputStream} reading from {@code pStream}. */ public static InputStream createStreamAdapter(final ImageInputStream pStream) { // TODO: Include stream start pos? // TODO: Skip buffering for known in-memory implementations? return new BufferedInputStream(new IIOInputStreamAdapter(pStream)); }
public final int read(byte[] pBytes) throws IOException { return read(pBytes, 0, pBytes.length); }
@Test public void testReadSubstreamRepositionOnClose() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 10); for (int i = 0; i < 7; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } // Make sure we don't read outside stream boundaries assertTrue(input.getStreamPosition() <= 7); // Make sure underlying stream is positioned at end of substream after close stream.close(); assertEquals(10, input.getStreamPosition()); input.close(); }
@Test public void testSeekBeforeStreamNoEnd() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); input.seek(10); assertEquals(10, input.getStreamPosition()); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input); assertEquals("Should not skip backwards", 0, stream.skip(-5)); assertEquals(10, input.getStreamPosition()); }
/** * Creates an {@code InputStream} adapter that reads from an underlying {@code ImageInputStream}. * The input stream will read until the end of {@code pStream}, or at most {@code pLength} bytes has been read. * * @param pStream the stream to read from. * @param pLength the maximum number of bytes that can be read from {@code pStream}. * @return an {@code InputStream} reading from {@code pStream}. */ public static InputStream createStreamAdapter(final ImageInputStream pStream, final long pLength) { // TODO: Include stream start pos? // TODO: Skip buffering for known in-memory implementations? return new BufferedInputStream(new IIOInputStreamAdapter(pStream, pLength)); }
public final int read(byte[] pBytes) throws IOException { return read(pBytes, 0, pBytes.length); }
public void testReadSubstreamOpenEnd() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); input.seek(10); assertEquals(10, input.getStreamPosition()); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input); for (int i = 0; i < 10; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } assertEquals("Read value after end of stream", -1, stream.read()); assertEquals("Read value after end of stream", -1, stream.read()); // Make sure underlying stream is positioned at end of substream after close stream.close(); assertEquals(20, input.getStreamPosition()); input.close(); }
public void testSeekBeforeStreamNoEnd() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); input.seek(10); assertEquals(10, input.getStreamPosition()); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input); assertEquals("Should not skip backwards", 0, stream.skip(-5)); assertEquals(10, input.getStreamPosition()); }
public void testReadSubstream() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 9); for (int i = 0; i < 9; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } assertEquals("Read value after end of stream", -1, stream.read()); assertEquals("Read value after end of stream", -1, stream.read()); // Make sure we don't read outside stream boundaries assertTrue(input.getStreamPosition() <= 9); input.close(); }
@Test(expected = IllegalArgumentException.class) public void testCreateNull() { new IIOInputStreamAdapter(null); }
public final int read(byte[] pBytes) throws IOException { return read(pBytes, 0, pBytes.length); }
public long skip(long pLength) throws IOException { long skipped = input.skipBytes(findMaxLen(pLength)); // Skips 0 or more, never -1 left -= skipped; return skipped; } }
public void testReadSubstreamRepositionOnClose() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 10); for (int i = 0; i < 7; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } // Make sure we don't read outside stream boundaries assertTrue(input.getStreamPosition() <= 7); // Make sure underlying stream is positioned at end of substream after close stream.close(); assertEquals(10, input.getStreamPosition()); input.close(); }
public void testSeekBeforeStream() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); input.seek(10); assertEquals(10, input.getStreamPosition()); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 9); assertEquals("Should not skip backwards", 0, stream.skip(-5)); assertEquals(10, input.getStreamPosition()); } }
@Test public void testReadSubstream() throws IOException { byte[] bytes = new byte[20]; MemoryCacheImageInputStream input = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes)); IIOInputStreamAdapter stream = new IIOInputStreamAdapter(input, 9); for (int i = 0; i < 9; i++) { assertTrue("Unexpected end of stream", -1 != stream.read()); } assertEquals("Read value after end of stream", -1, stream.read()); assertEquals("Read value after end of stream", -1, stream.read()); // Make sure we don't read outside stream boundaries assertTrue(input.getStreamPosition() <= 9); input.close(); }
protected InputStream makeInputStream(byte[] pBytes) { return new IIOInputStreamAdapter(new MemoryCacheImageInputStream(new ByteArrayInputStream(pBytes)), pBytes.length); }