Skip to content

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.

@jimender2 jimender2 closed this Oct 11, 2025
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