ReleasableOutputStream extends OutputStream { @Override public void write(int b) throws IOException { try { delegate.write(b); } catch (IOException e) { safeClose(); throw e; } } ReleasableOutputStream(OutputStream delegate, Runnable onClose); @Override void write(int b); @Override void write(byte[] b); @Override void write(byte[] b, int off, int len); @Override void flush(); @Override void close();  }
@Test public void write() throws Exception { releasableOutputStream.write('a'); assertFalse(wasCalled.get()); }
@Test public void failedWrite() throws Exception { try { failedReleasableOutputStream.write('a'); } catch (IOException e) { } assertTrue(wasCalled.get()); }
@Test public void write1() throws Exception { releasableOutputStream.write(new byte[] { 'a', 'b' }); assertFalse(wasCalled.get()); }
@Test public void failedWrite1() throws Exception { try { failedReleasableOutputStream.write(new byte[] { 'a', 'b' }); } catch (IOException e) { } assertTrue(wasCalled.get()); }
@Test public void write2() throws Exception { releasableOutputStream.write(new byte[] { 'a', 'b' }, 0, 2); assertFalse(wasCalled.get()); }
@Test public void failedWrite2() throws Exception { try { failedReleasableOutputStream.write(new byte[] { 'a', 'b' }, 0, 2); } catch (IOException e) { } assertTrue(wasCalled.get()); }