Skip to content
Draft
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
18 changes: 13 additions & 5 deletions src/main/java/org/apache/commons/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,11 @@ private static void closeQ(final Closeable closeable) {
/**
* Closes a {@link Closeable} unconditionally.
* <p>
* Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in finally blocks.
* Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in cleanup code when reporting a close failure is not
* necessary or useful.
* <p>
* Do not use this method to close output streams or writers when close failures must be reported. Closing an output stream or writer may flush buffered data,
* and ignoring a close failure can hide that data was not fully written.
* <p>
* Example code:
* </p>
Expand All @@ -801,10 +805,12 @@ private static void closeQ(final Closeable closeable) {
*
* <pre>
* try {
* return IOUtils.copy(inputStream, outputStream);
* IOUtils.copy(inputStream, outputStream);
* outputStream.close(); // close errors are handled
* outputStream = null;
* } finally {
* IOUtils.closeQuietly(inputStream);
* IOUtils.closeQuietly(outputStream);
* IOUtils.closeQuietly(outputStream); // only if normal close was skipped

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Sarankumar18
This example makes no sense to me:

  • First, you close() the OS on line 809 with the contradictory comment "close errors are handled"; the close() call does not handle errors.
  • Second, in the finally block, the comment "only if normal close was skipped" is not true because the finally block is always executed.
    Please come up with an example and comments that make sense and are consistent with what actually happens.
    TY

* }
* </pre>
* <p>
Expand Down Expand Up @@ -853,9 +859,11 @@ public static void closeQuietly(final Closeable closeable) {
*
* <pre>
* try {
* return IOUtils.copy(inputStream, outputStream);
* IOUtils.copy(inputStream, outputStream);
* outputStream.close(); // close errors are handled

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same comment as above.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ping.

* outputStream = null;
* } finally {
* IOUtils.closeQuietly(inputStream, outputStream);
* IOUtils.closeQuietly(inputStream, outputStream); // only closes outputStream if normal close was skipped
* }
* </pre>
* <p>
Expand Down