From 5dc6e700cbcbc01833a02a193f660a286b934898 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 13 Jan 2023 02:49:57 +0800 Subject: Code cleanup. --- .../incremental/identifier/Sha256Identifier.java | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/Sha256Identifier.java') diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/Sha256Identifier.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/Sha256Identifier.java index 50e6aa4..21111a0 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/Sha256Identifier.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/Sha256Identifier.java @@ -54,27 +54,20 @@ public class Sha256Identifier extends SingleHashIdentifier { @Override protected byte[] hash(File file) throws IOException { + return sha256Hash(file); + } + + public static byte[] sha256Hash(File file) throws IOException { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); try (FileInputStream inputStream = new FileInputStream(file)) { - // This does not work. I don't know why -// FileChannel channel = inputStream.getChannel(); -// ByteBuffer buffer = ByteBuffer.allocate(128); -// int readLength; -// while ((readLength = channel.read(buffer)) > 0) -// digest.update(buffer); - - // This also works, without warnings byte[] readBuffer = new byte[1024 * 1024]; + int readLength; - while ((readLength = inputStream.read(readBuffer)) > 0) + while ((readLength = inputStream.read(readBuffer)) > 0) { digest.update(readBuffer, 0, readLength); - - // The below lines also works, but the IDE will complain about the while loop -// DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); -// while(digestInputStream.read() > 0) -// ; + } return digest.digest(); } -- cgit v1.2.3