summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-12 16:54:03 +0800
committerkeuin <[email protected]>2021-01-12 16:54:03 +0800
commitf4d926c1eb91749a4b0f89c7a35538821ff7e21b (patch)
tree32890a856dfe67f80762000793d6140d43dd1795 /src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier
parent4a52f5e6ce06cb6717510c6a975d5490be627c98 (diff)
Add serialization and deserialization for ObjectCollection
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java4
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java21
2 files changed, 14 insertions, 11 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java
index 6744616..aece07d 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java
@@ -1,11 +1,13 @@
package com.keuin.kbackupfabric.util.backup.incremental.identifier;
+import java.io.Serializable;
+
/**
* The identifier distinguishing files in the object collection.
* It should be based on cryptographic hash function in order to prevent possible attacks to the backup system.
* All identifiers should be immutable and implement their own equals method.
* Immutable.
*/
-public interface ObjectIdentifier {
+public interface ObjectIdentifier extends Serializable {
String getIdentification();
}
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 64716ed..9ecf2d3 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
@@ -41,27 +41,28 @@ public class Sha256Identifier extends SingleHashIdentifier {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
- FileInputStream inputStream = new FileInputStream(file);
-
- // This does not work. I don't know why
+ 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);
+ // 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
+ // 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();
+ return digest.digest();
+ }
+
} catch (NoSuchAlgorithmException ignored) {
// this shouldn't happen
return new byte[SHA256_LENGTH];