Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,7 @@ public static LineIterator lineIterator(final File file, final String charsetNam
inputStream = Files.newInputStream(file.toPath());
return IOUtils.lineIterator(inputStream, charsetName);
} catch (final IOException | RuntimeException ex) {
IOUtils.closeQuietly(inputStream, ex::addSuppressed);
IOUtils.closeQuietly(inputStream, ex);
throw ex;
}
}
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ public static void close(final URLConnection conn) {
* @param closeable the object to close, may be null.
*/
private static void closeQ(final Closeable closeable) {
closeQuietly(closeable, null);
closeQuietly(closeable, (Consumer<Exception>) null);
}

/**
Expand Down Expand Up @@ -824,7 +824,40 @@ private static void closeQ(final Closeable closeable) {
* @see Throwable#addSuppressed(Throwable)
*/
public static void closeQuietly(final Closeable closeable) {
closeQuietly(closeable, null);
closeQuietly(closeable, (Consumer<Exception>) null);
}

/**
* Closes a {@link Closeable} unconditionally and adds any exception thrown by the {@code close()} to the given Throwable.
*
* <p>
* For example:
* </p>
*
* <pre>
* Closeable closeable = ...;
* try {
* // process closeable
* closeable.close();
* } catch (Exception e) {
* // error handling
* throw IOUtils.closeQuietly(closeable, e);
* }
* </pre>
* <p>
* Also consider using a try-with-resources statement where appropriate.
* </p>
*
* @param <T> The Throwable type.
* @param closeable The object to close, may be null or already closed.
* @param throwable Add the exception throw by the closeable to the given Throwable.
* @return The given Throwable.
* @since 2.22.0
* @see Throwable#addSuppressed(Throwable)
*/
public static <T extends Throwable> T closeQuietly(final Closeable closeable, final T throwable) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This signature causes the following call to become ambiguous:

closeQuietly(closeable, e::addSuppressed);

Maybe we might consider inverting the order of the arguments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no type casting needed in the call sites in:

  • org.apache.commons.io.FileUtils.lineIterator(File, String)
  • org.apache.commons.io.LineIterator.hasNext()
  • org.apache.commons.io.output.FileWriterWithEncoding.initWriter(File, Object, boolean)
  • org.apache.commons.io.IOUtilsTest.testCloseQuietly_CloseableIOExceptionAddSuppressed()

I would only see a typecast needed for a null literal argument, and making the method weird compared to the others for this one use case doesn't seem worth it to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I don't understand why the compiler gets confused about e::addSuppressed (which is clearly not a Throwable), but experimentally it does.
Any idea why?

closeQuietly(closeable, (Consumer<Exception>) throwable::addSuppressed);
return throwable;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/apache/commons/io/LineIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public boolean hasNext() {
}
}
} catch (final IOException ioe) {
IOUtils.closeQuietly(this, ioe::addSuppressed);
throw new IllegalStateException(ioe);
throw new IllegalStateException(IOUtils.closeQuietly(this, ioe));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ private static OutputStreamWriter initWriter(final File file, final Object encod
}
return new OutputStreamWriter(outputStream, (String) encoding);
} catch (final IOException | RuntimeException ex) {
try {
IOUtils.close(outputStream);
} catch (final IOException e) {
ex.addSuppressed(e);
}
IOUtils.closeQuietly(outputStream, ex);
if (!fileExistedAlready) {
FileUtils.deleteQuietly(file);
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/apache/commons/io/IOUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -539,6 +540,18 @@ void testCloseQuietly_AllCloseableIOException() {
assertDoesNotThrow(() -> IOUtils.closeQuietly((Iterable<Closeable>) null));
}

@SuppressWarnings("resource")
@Test
void testCloseQuietly_CloseableIOExceptionAddSuppressed() {
final Throwable e = new Exception("test").fillInStackTrace();
assertEquals(0, e.getSuppressed().length);
assertSame(e, IOUtils.closeQuietly(new BrokenInputStream(new EOFException("Suppressed").fillInStackTrace()), e));
assertEquals(1, e.getSuppressed().length);
final Throwable suppressed0 = e.getSuppressed()[0];
assertInstanceOf(EOFException.class, suppressed0);
assertEquals("Suppressed", suppressed0.getMessage());
}

@Test
void testCloseQuietly_CloseableException() {
// IOException
Expand Down