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/backup | |
parent | ff7d23b5b915f04c2b5f3614701cb56cfc605228 (diff) |
Code cleanup.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/backup')
7 files changed, 31 insertions, 77 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/BackupFilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/backup/BackupFilesystemUtil.java index eac7ce9..519951a 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/BackupFilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/BackupFilesystemUtil.java @@ -26,17 +26,6 @@ public final class BackupFilesystemUtil { return backupFileNamePrefix; } - -// @Deprecated -// public static String getBackupName(String backupFileName) { -// try { -// if (backupFileName.matches(backupFileNamePrefix + ".+\\.zip")) -// return backupFileName.substring(backupFileNamePrefix.length(), backupFileName.length() - 4); -// } catch (IndexOutOfBoundsException ignored) { -// } -// return backupFileName; -// } - public static boolean isBackupFileExists(String backupFileName, MinecraftServer server) { File backupFile = new File(getBackupSaveDirectory(server), backupFileName); return backupFile.isFile(); 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()); diff --git a/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java index 9c0c1c2..fa38979 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java @@ -47,11 +47,6 @@ public class BackupNameSuggestionProvider { } } -// private static void updateCandidateList(Collection<String> stringCollection) { -// candidateList.clear(); -// candidateList.addAll(stringCollection); -// } - public static SuggestionProvider<ServerCommandSource> getProvider() { // return (context, builder) -> getCompletableFuture(builder); return (context, builder) -> { @@ -61,24 +56,6 @@ public class BackupNameSuggestionProvider { }; } -// private static CompletableFuture<Suggestions> getCompletableFuture(SuggestionsBuilder builder) { -// if (isCacheExpired()) -// updateCandidateList(); -// String remaining = builder.getRemaining().toLowerCase(Locale.ROOT); -// synchronized (syncCache) { -// if (candidateCacheList.isEmpty()) { // If the list is empty then return no suggestions -// return Suggestions.empty(); // No suggestions -// } -// -// for (String string : candidateCacheList) { // Iterate through the supplied list -// if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) { -// builder.suggest(string); // Add every single entry to suggestions list. -// } -// } -// } -// return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions -// } - private static boolean isCacheExpired() { return ((int) System.currentTimeMillis()) - cacheUpdateTime > CACHE_TTL || cacheUpdateTime == 0; } |