summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-24 21:15:24 +0800
committerkeuin <[email protected]>2021-01-25 02:12:49 +0800
commit1df50093bd76315905a9aae880470e81b5e1d8f0 (patch)
treef94b4d2847c2da2c820e708b6664c3246992e581 /src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
parent3648d0ca94c9954fa7c4797ce64a0f42a4f837b5 (diff)
If incremental backup failed, unfinished copy will be fully reverted.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
index f663f20..fa411a0 100644
--- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
+++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
@@ -1,6 +1,11 @@
package com.keuin.kbackupfabric.backup.incremental;
+import org.jetbrains.annotations.NotNull;
+
import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Iterator;
import java.util.Objects;
/**
@@ -39,4 +44,40 @@ public class ObjectCollectionSerializer {
}
}
}
+
+ public static Iterable<ObjectCollection2> fromDirectory(File directory) throws IOException {
+
+ if (!directory.isDirectory()) {
+ throw new IllegalArgumentException("Given directory is invalid.");
+ }
+ return new Iterable<ObjectCollection2>() {
+ private final Iterator<ObjectCollection2> iter = new Iterator<ObjectCollection2>() {
+ private final Iterator<Path> i = Files.walk(directory.toPath(), 1).filter(p -> {
+ File f = p.toFile();
+ return f.isFile() && f.getName().endsWith(".kbi");
+ }).iterator();
+
+ @Override
+ public boolean hasNext() {
+ return i.hasNext();
+ }
+
+ @Override
+ public ObjectCollection2 next() {
+ try {
+ return fromFile(i.next().toFile());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ @NotNull
+ @Override
+ public Iterator<ObjectCollection2> iterator() {
+ return iter;
+ }
+ };
+
+ }
}