Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<action type="fix" dev="ggregory" due-to="Sharang Gupta, Gary Gregory">Add missing @Test to CSVFormatTest.testEqualsSkipHeaderRecord#testEqualsSkipHeaderRecord() (#639).</action>
<action type="fix" dev="ggregory" due-to="Mohd Irshad, Gary Gregory">Fix Predefined @see references in CSVFormat (#641).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Document CSVFormat validation exceptions.</action>
<action type="fix" issue="CSV-333" due-to="Qiuye Wang">Close the CSVPrinter output even if the optional flush fails.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory, Indy, Sylvia van Os" issue="CSV-307">Add an "Android Compatibility" section to the web site.</action>
<action type="add" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="CSV-325">Add CSVParser.Builder.setByteOffset(long) (#604).</action>
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/org/apache/commons/csv/CSVPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.function.IOStream;

/**
Expand Down Expand Up @@ -129,6 +130,9 @@ public void close() throws IOException {

/**
* Closes the underlying stream with an optional flush first.
* <p>
* If flushing fails, an attempt is still made to close the stream. A distinct failure from closing is suppressed on the flush failure.
* </p>
*
* @param flush whether to flush before the actual close.
* @throws IOException
Expand All @@ -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);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '"';
Expand Down Expand Up @@ -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();
Expand Down