SyncableDataOutputStream extends DataOutputStream implements Syncable, StreamCapabilities { @Override public void close() throws IOException { IOException ioeFromFlush = null; try { flush(); } catch (IOException e) { ioeFromFlush = e; throw e; } finally { try { this.out.close(); } catch (IOException e) { if (ioeFromFlush == e) { LOG.debug("flush() and close() throwing back same Exception. Just swallowing the latter", e); } else { throw e; } } } } SyncableDataOutputStream(OutputStream out); @InterfaceAudience.LimitedPrivate({"HDFS"}) OutputStream getOutStream(); @Override boolean hasCapability(String capability); @Override void hflush(); @Override void hsync(); @Override void close();  }
@Test public void testCloseWhenFlushThrowingIOException() throws Exception { MockOutputStream out = new MockOutputStream(); SyncableDataOutputStream sdos = new SyncableDataOutputStream(out); out.flushThrowIOE = true; LambdaTestUtils.intercept(IOException.class, "An IOE from flush", () -> sdos.close()); MockOutputStream out2 = new MockOutputStream(); out2.flushThrowIOE = true; LambdaTestUtils.intercept(IOException.class, "An IOE from flush", () -> { try (SyncableDataOutputStream sdos2 = new SyncableDataOutputStream(out2)) { } }); }