Skip to content

Refactor hashCompare to use try-with-resources#860

Open
kayerajava wants to merge 1 commit into
LeavesMC:masterfrom
kayerajava:master
Open

Refactor hashCompare to use try-with-resources#860
kayerajava wants to merge 1 commit into
LeavesMC:masterfrom
kayerajava:master

Conversation

@kayerajava

Copy link
Copy Markdown

Wrapped "FileInputStream" in a try-with-resources block to ensure the stream
is always closed after use, even if an exception occurs.
Prevents file descriptor leaks under heavy load and fixes potential file lock issues on Windows.

@Lumine1909 Lumine1909 left a comment

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.

Thank you for that pr!

Seems there is an indentation issue, the added lines contain an extra indent.
In addition, the close of the stream parameter in createChecksum can also be deleted:

    @NotNull
    public static UUID createChecksum(final @NotNull InputStream fis) throws NoSuchAlgorithmException, IOException {
        final byte[] buffer = new byte[4096];
        final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        int numRead;

        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                messageDigest.update(buffer, 0, numRead);
            }
        } while (numRead != -1);

-       fis.close();
        return UUID.nameUUIDFromBytes(messageDigest.digest());
    }

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