/** * Creates an {@code OutputStream} adapter that writes to an underlying {@code ImageOutputStream}. * <p/> * Note: The adapter is buffered, and <em>MUST</em> be properly flushed/closed after use, * otherwise data may be lost. * * @param pStream the stream to write to. * @return an {@code OutputSteam} writing to {@code pStream}. */ public static OutputStream createStreamAdapter(final ImageOutputStream pStream) { return new BufferedOutputStream(new IIOOutputStreamAdapter(pStream)); }
@Override public void flush() throws IOException { // NOTE: The contract of OutputStream.flush is very different from ImageOutputStream.flush. We can't delegate. // TODO: Fulfill the contract of OutputStream.flush? This seems to be good enough for now. assertOpen(); }
@Test public void testFlushOnAdapterDoesNotMoveFlushedPositionInBacking() throws IOException { MemoryCacheImageOutputStream backing = new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); IIOOutputStreamAdapter adapter = new IIOOutputStreamAdapter(backing); // Sanity check assertEquals(0, backing.getFlushedPosition()); // Write & flush adapter.write(0xCA); adapter.write(new byte[8]); adapter.write(0xFE); adapter.flush(); // Assertions assertEquals(10, backing.length()); assertEquals(10, backing.getStreamPosition()); assertEquals(0, backing.getFlushedPosition()); // Just make sure we can safely seek back to start and read data back backing.seek(0); assertEquals(0, backing.getStreamPosition()); // If this can be read, I think the contract of flush is also fulfilled (kind of) assertEquals(0xCA, backing.read()); assertEquals(8, backing.skipBytes(8)); assertEquals(0xFE, backing.read()); } }
@Test public void testFlushOnAdapterDoesNotMoveFlushedPositionInBacking() throws IOException { MemoryCacheImageOutputStream backing = new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); IIOOutputStreamAdapter adapter = new IIOOutputStreamAdapter(backing); // Sanity check assertEquals(0, backing.getFlushedPosition()); // Write & flush adapter.write(0xCA); adapter.write(new byte[8]); adapter.write(0xFE); adapter.flush(); // Assertions assertEquals(10, backing.length()); assertEquals(10, backing.getStreamPosition()); assertEquals(0, backing.getFlushedPosition()); // Just make sure we can safely seek back to start and read data back backing.seek(0); assertEquals(0, backing.getStreamPosition()); // If this can be read, I think the contract of flush is also fulfilled (kind of) assertEquals(0xCA, backing.read()); assertEquals(8, backing.skipBytes(8)); assertEquals(0xFE, backing.read()); } }
@Test(expected = IllegalArgumentException.class) public void testCreateNull() { new IIOOutputStreamAdapter(null); }
@Override public void write(final byte[] pBytes) throws IOException { assertOpen(); output.write(pBytes); }
@Override protected OutputStream makeObject() { return new IIOOutputStreamAdapter(new MemoryCacheImageOutputStream(new ByteArrayOutputStream())); }
@Override public void write(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { assertOpen(); output.write(pBytes, pOffset, pLength); }
/** * Creates an {@code OutputStream} adapter that writes to an underlying {@code ImageOutputStream}. * * @param pStream the stream to write to. * @return an {@code OutputSteam} writing to {@code pStream}. */ public static OutputStream createStreamAdapter(final ImageOutputStream pStream) { return new BufferedOutputStream(new IIOOutputStreamAdapter(pStream)); }
@Override public void write(final int pByte) throws IOException { assertOpen(); output.write(pByte); }
/** * Creates an {@code OutputStream} adapter that writes to an underlying {@code ImageOutputStream}. * <p/> * Note: The adapter is buffered, and <em>MUST</em> be properly flushed/closed after use, * otherwise data may be lost. * * @param pStream the stream to write to. * @return an {@code OutputSteam} writing to {@code pStream}. */ public static OutputStream createStreamAdapter(final ImageOutputStream pStream) { return new BufferedOutputStream(new IIOOutputStreamAdapter(pStream)); }
@Override public void flush() throws IOException { // NOTE: The contract of OutputStream.flush is very different from ImageOutputStream.flush. We can't delegate. // TODO: Fulfill the contract of OutputStream.flush? This seems to be good enough for now. assertOpen(); }
@Test(expected = IllegalArgumentException.class) public void testCreateNull() { new IIOOutputStreamAdapter(null); }
@Override public void write(final byte[] pBytes, final int pOffset, final int pLength) throws IOException { assertOpen(); output.write(pBytes, pOffset, pLength); }
@Override protected OutputStream makeObject() { return new IIOOutputStreamAdapter(new MemoryCacheImageOutputStream(new ByteArrayOutputStream())); }
@Override public void write(final byte[] pBytes) throws IOException { assertOpen(); output.write(pBytes); }
@Override public void write(final int pByte) throws IOException { assertOpen(); output.write(pByte); }