diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6958cfe15..85b21afaa 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -83,6 +83,7 @@
+ * If flushing fails, an attempt is still made to close the stream. A distinct failure from closing is suppressed on the flush failure. + *
* * @param flush whether to flush before the actual close. * @throws IOException @@ -137,12 +141,22 @@ public void close() throws IOException { * @see CSVFormat#getAutoFlush() */ public void close(final boolean flush) throws IOException { - if (flush || format.getAutoFlush()) { - flush(); - } - if (appendable instanceof Closeable) { - ((Closeable) appendable).close(); + final Closeable closeable = appendable instanceof Closeable ? (Closeable) appendable : null; + try { + if (flush || format.getAutoFlush()) { + flush(); + } + } catch (final IOException | RuntimeException | Error flushFailure) { + try { + IOUtils.close(closeable); + } catch (final IOException | RuntimeException | Error closeFailure) { + if (closeFailure != flushFailure) { + flushFailure.addSuppressed(closeFailure); + } + } + throw flushFailure; } + IOUtils.close(closeable); } /** diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 66064f448..5a6e7a920 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -79,6 +80,31 @@ */ class CSVPrinterTest { + private static final class FlushFailingWriter extends Writer { + + private final IOException flushFailure = new IOException("flush failed"); + private IOException closeFailure; + private int closeCount; + + @Override + public void close() throws IOException { + closeCount++; + if (closeFailure != null) { + throw closeFailure; + } + } + + @Override + public void flush() throws IOException { + throw flushFailure; + } + + @Override + public void write(final char[] buffer, final int offset, final int length) { + // These tests only exercise the writer lifecycle. + } + } + private static final int TABLE_RECORD_COUNT = 2; private static final int TABLE_AND_HEADER_RECORD_COUNT = TABLE_RECORD_COUNT + 1; private static final char DQUOTE_CHAR = '"'; @@ -349,6 +375,39 @@ void testCloseWithFlushOn() throws IOException { } } + @Test + void testCloseWithFlushIOExceptionStillCloses() throws IOException { + final FlushFailingWriter writer = new FlushFailingWriter(); + @SuppressWarnings("resource") + final CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT); + assertSame(writer.flushFailure, assertThrows(IOException.class, () -> printer.close(true))); + assertEquals(1, writer.closeCount); + } + + @Test + void testCloseWithAutoFlushIOExceptionStillCloses() throws IOException { + final FlushFailingWriter writer = new FlushFailingWriter(); + final CSVFormat format = CSVFormat.DEFAULT.builder().setAutoFlush(true).get(); + @SuppressWarnings("resource") + final CSVPrinter printer = new CSVPrinter(writer, format); + assertSame(writer.flushFailure, assertThrows(IOException.class, printer::close)); + assertEquals(1, writer.closeCount); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void testCloseWithBothFlushAndCloseIOException(final boolean sameFailure) throws IOException { + final FlushFailingWriter writer = new FlushFailingWriter(); + writer.closeFailure = sameFailure ? writer.flushFailure : new IOException("close failed"); + @SuppressWarnings("resource") + final CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT); + final IOException actual = assertThrows(IOException.class, () -> printer.close(true)); + assertSame(writer.flushFailure, actual); + assertEquals(1, writer.closeCount); + final Throwable[] expected = sameFailure ? new Throwable[0] : new Throwable[] {writer.closeFailure}; + assertArrayEquals(expected, actual.getSuppressed()); + } + @Test void testCRComment() throws IOException { final StringWriter sw = new StringWriter();