ReleasableInputStream extends InputStream { @Override public int available() throws IOException { try { return delegate.available(); } catch (IOException e) { safeClose(); throw e; } } ReleasableInputStream(InputStream delegate, Runnable onClose); @Override int read(); @Override int read(byte[] bytes); @Override int read(byte[] bytes, int i, int i1); @Override long skip(long l); @Override int available(); @Override void close(); @Override void mark(int i); @Override void reset(); @Override boolean markSupported();  }
@Test public void available() throws Exception { releasableInputStream.available(); assertFalse(wasCalled.get()); }
@Test(expected = IOException.class) public void failedAvailable() throws Exception { failedReleasableInputStream.available(); assertTrue(wasCalled.get()); }