commons-io icon indicating copy to clipboard operation
commons-io copied to clipboard

IOUtils chain together IOException for Closeables, add overload

Open wodencafe opened this issue 4 years ago • 7 comments

This pull request chains together IOExceptions thrown from closing multiple Closeable instances, which allows the method to attempt to close all of the Closeable instances rather than halting at the first IOException. An overload is added to allow closing multiple Closeable instances and passing the possible resulting IOException to an IOConsumer<IOException>.

wodencafe avatar Nov 05 '21 15:11 wodencafe

-1: This makes no sense to me as the exception are not causaly unrelated and you are forcing them to be. This might be a use case for IOExceptionList.

garydgregory avatar Nov 05 '21 22:11 garydgregory

Hi @garydgregory , good suggestion, IOExceptionList seems like the perfect fit for this. I have revised the code to use this class.

wodencafe avatar Nov 05 '21 23:11 wodencafe

I ended up reworking the underlying so that now the method looks like this

    public static void close(final Closeable... closeables) throws IOException {
        IOConsumer.forEach(closeables, IOUtils::close);
    }

See also forEachIndex which provides slightly different information for compatibility with other call sites.

garydgregory avatar Nov 06 '21 20:11 garydgregory

Hi @wodencafe Thank you for your update. Now I see one new method, without any tests, and not used from anywhere.

  • Should the [] be changed to a ... and moved to the last parameter position? I would think so.
  • Can the method be used within the code base?
  • There are no tests.

garydgregory avatar Nov 09 '21 16:11 garydgregory

Hello @garydgregory , my apologies, I got swamped this weekend and haven't updated this PR with tests. I also agree that the [] should be changed to ... varargs and swapped to be the last parameter, so I will implement this change, and see if there are other places in Commons IO that can take advantage of this new functionality. I will add tests for this today.

Thank you for reviewing the changes, and the contributions and feedback.

wodencafe avatar Nov 09 '21 17:11 wodencafe

Coverage Status

Coverage decreased (-0.5%) to 87.329% when pulling 1ccb075f79ec06e763e6b62417d0602c98524749 on wodencafe:IOUtils-closewitherrors into 5300267aa9d13ec0f800b221e3086e835be697be on apache:master.

coveralls avatar Nov 13 '21 08:11 coveralls

Hi @garydgregory , I have updated the pull request with the changes you suggested. Thank you!

wodencafe avatar Nov 13 '21 10:11 wodencafe