diff options
author | Keuin <[email protected]> | 2023-01-13 02:49:57 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-01-13 02:49:57 +0800 |
commit | 5dc6e700cbcbc01833a02a193f660a286b934898 (patch) | |
tree | 72cf4cc3cfae2b3a7fb978bc0c465e329909db3e /src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier | |
parent | ff7d23b5b915f04c2b5f3614701cb56cfc605228 (diff) |
Code cleanup.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier')
2 files changed, 8 insertions, 38 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java index 31cde29..c640c0b 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java @@ -3,12 +3,11 @@ package com.keuin.kbackupfabric.util.backup.incremental.identifier; import com.keuin.kbackupfabric.util.BytesUtil; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.util.Objects; +import static com.keuin.kbackupfabric.backup.incremental.identifier.Sha256Identifier.sha256Hash; + /** * Identifier based on sha256. * Immutable. @@ -50,35 +49,7 @@ public class Sha256Identifier extends SingleHashIdentifier { @Override protected byte[] hash(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) - 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(); - } - - } catch (NoSuchAlgorithmException ignored) { - // this shouldn't happen - return new byte[SHA256_LENGTH]; - } + return sha256Hash(file); } }
\ No newline at end of file diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java index 6c67918..285c6ce 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java @@ -12,7 +12,6 @@ import java.util.Objects; * Immutable. */ public abstract class SingleHashIdentifier implements ObjectIdentifier { - private final byte[] hash; private final String type; @@ -37,11 +36,11 @@ public abstract class SingleHashIdentifier implements ObjectIdentifier { } @Override - public boolean equals(Object obj) { - if (!(obj instanceof com.keuin.kbackupfabric.backup.incremental.identifier.SingleHashIdentifier)) { - return false; - } - return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SingleHashIdentifier that = (SingleHashIdentifier) o; + return Arrays.equals(hash, that.hash) && type.equals(that.type); } @Override |