Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress resource leaks warnings in FileUtils #283

Merged
merged 2 commits into from
Jan 17, 2022

Conversation

dbwiddis
Copy link
Contributor

@dbwiddis dbwiddis commented Jan 17, 2022

FileChannel is not Closeable, (actually it is) so including it in a try-with-resources block does not close the underling streams. Refactored to ensure the closeable object is referenced in try.

Also implemented a try-with-resources block for the BufferedReader closure.

@@ -68,18 +68,16 @@ public static String read(URL location, String encoding) throws IOException {
public static String[] read(final URL[] locations, final String encoding) throws IOException {
final String[] results = new String[locations.length];
for (int i = 0; i < locations.length; i++) {
final Reader reader = new BufferedReader(new InputStreamReader(locations[i].openStream(), encoding));
try {
try (Reader reader = new BufferedReader(new InputStreamReader(locations[i].openStream(), encoding))) {
Copy link
Owner

Choose a reason for hiding this comment

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

👍

try (FileChannel channel = new FileOutputStream(file).getChannel()) {
channel.write(ByteBuffer.wrap(content.getBytes(encoding)));
try (FileOutputStream outStream = new FileOutputStream(file)) {
outStream.getChannel().write(ByteBuffer.wrap(content.getBytes(encoding)));
Copy link
Owner

Choose a reason for hiding this comment

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

Please revert: there was no leak here: the construction is right: we can convert a FileOutputStream to a FileChannel and only use and close the latter.

FileChannel implements Channel, which is Closeable. Besides, the javadoc clearly states that the channel will be closed, and as you can see in the FileChannel impl, its file descriptor is released and closed.

Regarding the usage of FileOutputStream and FileChannel, closing FileOutputStream will also close the channel, as stated in the javadoc of its close() method, and closing the FileChannel will also close the underlying file descriptor. Closing one or the other is fine.

This construction new FileOutputStream(file).getChannel() also prevents anyone from using the FileOutputStream and allows it to be GC-ed as soon as possible.

Side note: if there were such a leak in the plugin, that is running on very big projects, I think that it would have been discovered years go ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed you're right. I'd looked for close() and didn't see it but didn't look at the inheritance.

Side note: if there were such a leak in the plugin, that is running on very big projects, I think that it would have been discovered years go ;-)

Yeah, probably true! Got too eager trying to get rid of yellow triangles in my IDE. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted and added some suppressed warnings to help the future me.

@mathieucarbou mathieucarbou added the in:core MLP core module label Jan 17, 2022
@dbwiddis dbwiddis changed the title Fix resource leaks in FileUtils Suppress warnings for resource leaks in FileUtils Jan 17, 2022
@dbwiddis dbwiddis changed the title Suppress warnings for resource leaks in FileUtils Suppress resource leaks warnings in FileUtils Jan 17, 2022
@mathieucarbou mathieucarbou merged commit ebe3fe1 into mathieucarbou:master Jan 17, 2022
@mathieucarbou mathieucarbou added this to the 4.2 milestone Mar 25, 2022
@dbwiddis dbwiddis deleted the resourceleak branch March 29, 2022 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:core MLP core module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants