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

Try with resources clean up #417

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jimender2
Copy link

There were several places in the code base where an object was opened and it had the possibility of not being closed if an error was thrown.

Since the objects that I saw already implemented the Closable interface, I went ahead and added a try with resources to automatically close the objects upon error/final usage of the object. This helps to prevent memory leaks and is a best practice.

dos.write(data);
offsets[i] = globalOffset; // always keep MCAFile information up to date
globalOffset += sectors;
try (DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(source.getFD()), sectors * 4096));
Copy link
Owner

Choose a reason for hiding this comment

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

This closes the RandomAccessFile's FileDescriptor as well, which is reused in this loop. Closing it prematurely will throw an IOException in the next iteration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants