BinaryData extends InputStream { public void close() throws IOException { try { backingStream.close(); } finally { if (onCloseAction != null) { onCloseAction.run(); } } } BinaryData(byte[] inMemoryData, InputStream streamData); void setOnCloseAction(Runnable action); void close(); @Override void mark(int readlimit); @Override void reset(); @Override boolean markSupported(); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len); @Override long skip(long n); @Override int available();  }
@Test public void testClose() throws Exception { BinaryData binaryData = new BinaryData(buildByteArray("123"), buildInputStream("")); Assert.assertEquals(3, binaryData.available()); Assert.assertEquals('1', binaryData.read()); Assert.assertEquals(2, binaryData.available()); binaryData.close(); Assert.assertEquals(0, binaryData.available()); Assert.assertEquals(-1, binaryData.read()); }