diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental')
5 files changed, 31 insertions, 43 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java index 04ae96e..095108b 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionFactory.java @@ -13,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.function.Consumer; import java.util.function.Supplier; +import java.util.stream.Stream; /** * Incremental backup is implemented as git-like file collection. @@ -45,17 +46,19 @@ public class ObjectCollectionFactory<T extends ObjectIdentifier> { throw new IllegalArgumentException("given file is not a directory"); Set<File> files = new HashSet<>(); - for (Iterator<Path> iter = Files.walk(directory.toPath(), 1).iterator(); iter.hasNext(); ) { - Path path = iter.next(); - if (Files.isSameFile(path, directory.toPath())) - continue; - File file = path.toFile(); - if (file.isDirectory()) { - subCollections.put(file.getName(), fromDirectory(file, ignoredFiles)); - } else if (!ignoredFiles.contains(file.getName())) { - files.add(file); // add to the set to be processed - } else { - PrintUtil.info(String.format("Skipping file %s.", file.getName())); + try (Stream<Path> walk = Files.walk(directory.toPath(), 1)) { + for (Iterator<Path> iter = walk.iterator(); iter.hasNext(); ) { + Path path = iter.next(); + if (Files.isSameFile(path, directory.toPath())) + continue; + File file = path.toFile(); + if (file.isDirectory()) { + subCollections.put(file.getName(), fromDirectory(file, ignoredFiles)); + } else if (!ignoredFiles.contains(file.getName())) { + files.add(file); // add to the set to be processed + } else { + PrintUtil.info(String.format("Skipping file %s.", file.getName())); + } } } @@ -125,7 +128,7 @@ public class ObjectCollectionFactory<T extends ObjectIdentifier> { public void process() { workers.clear(); for (int i = 0; i < threads; i++) { - ParallelWorker<Res> worker = new ParallelWorker<Res>(taskList, consumer, i); + ParallelWorker<Res> worker = new ParallelWorker<>(taskList, consumer, i); workers.add(worker); worker.start(); } 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 42e7c0c..7a395d8 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java @@ -21,17 +21,6 @@ public class ObjectCollectionSerializer { @Deprecated public static ObjectCollection2 fromFile(File file) throws IOException { 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; } /** 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(); } diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index b18c119..7870620 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -47,7 +47,7 @@ public class IncrementalBackupStorageManager { * Check whether the storage contains all files in the given collection. * * @param collection the collection. - * @return whether all files exists. + * @return whether all files exist. */ public boolean contains(@NotNull ObjectCollection2 collection) { Objects.requireNonNull(collection); @@ -60,7 +60,7 @@ public class IncrementalBackupStorageManager { } /** - * Add a object collection to storage base and copy files to the storage. + * Add an object collection to storage base and copy files to the storage. * * @param collection the collection. * @return objects copied to the base. diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java index f17217a..c5d1115 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java @@ -13,6 +13,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.stream.Stream; public class IncBackupInfoSerializer { /** @@ -77,10 +78,12 @@ public class IncBackupInfoSerializer { throw new IllegalArgumentException("Given directory is invalid."); } List<Path> pathList = new ArrayList<>(); - Files.walk(directory.toPath(), 1).filter(p -> { - File f = p.toFile(); - return f.isFile() && f.getName().endsWith(".kbi"); - }).forEach(pathList::add); + try (Stream<Path> walk = Files.walk(directory.toPath(), 1)) { + walk.filter(p -> { + File f = p.toFile(); + return f.isFile() && f.getName().endsWith(".kbi"); + }).forEach(pathList::add); + } List<SavedIncrementalBackup> objectList = new ArrayList<>(); for (Path path : pathList) { SavedIncrementalBackup info = IncBackupInfoSerializer.fromFile(path.toFile()); |