From 43d48aa979a1e316af5ba0445412fe8926ce983b Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 25 Jan 2021 02:07:54 +0800 Subject: Perform clean-up after deleting a backup. --- .../incremental/ObjectCollectionSerializer.java | 76 +++++++++------------- 1 file changed, 32 insertions(+), 44 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java') 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 fa411a0..3be1bfe 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java @@ -1,11 +1,17 @@ package com.keuin.kbackupfabric.backup.incremental; -import org.jetbrains.annotations.NotNull; +import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerializer; +import com.keuin.kbackupfabric.backup.incremental.serializer.SavedIncrementalBackup; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Iterator; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Objects; /** @@ -19,17 +25,18 @@ public class ObjectCollectionSerializer { */ @Deprecated public static ObjectCollection2 fromFile(File file) throws IOException { - Objects.requireNonNull(file); - ObjectCollection2 collection; - try (FileInputStream fileInputStream = new FileInputStream(file)) { - try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) { - collection = (ObjectCollection2) objectInputStream.readObject(); - } catch (ClassNotFoundException ignored) { - // this should not happen - return null; - } - } - return collection; + throw new RuntimeException("This method has been depreciated."); +// Objects.requireNonNull(file); +// ObjectCollection2 collection; +// try (FileInputStream fileInputStream = new FileInputStream(file)) { +// try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) { +// collection = (ObjectCollection2) objectInputStream.readObject(); +// } catch (ClassNotFoundException ignored) { +// // this should not happen +// return null; +// } +// } +// return collection; } /** @@ -46,38 +53,19 @@ public class ObjectCollectionSerializer { } public static Iterable fromDirectory(File directory) throws IOException { - if (!directory.isDirectory()) { throw new IllegalArgumentException("Given directory is invalid."); } - return new Iterable() { - private final Iterator iter = new Iterator() { - private final Iterator 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 iterator() { - return iter; - } - }; - + List pathList = new ArrayList<>(); + Files.walk(directory.toPath(), 1).filter(p -> { + File f = p.toFile(); + return f.isFile() && f.getName().endsWith(".kbi"); + }).forEach(pathList::add); + List objectList = new ArrayList<>(); + for (Path path : pathList) { + SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(path.toFile()); + objectList.add(info.getObjectCollection()); + } + return Collections.unmodifiableCollection(objectList); } } -- cgit v1.2.3