moved fileWalker

This commit is contained in:
Frank 2021-08-16 10:28:37 +02:00
parent 03bef0ae88
commit 132623419d
2 changed files with 32 additions and 24 deletions

View file

@ -27,7 +27,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream; import java.util.stream.Stream;
abstract public class DataExchange { abstract public class DataExchange {
@ -133,7 +132,7 @@ abstract public class DataExchange {
public void loadCache() { public void loadCache() {
if (fileCache == null) { if (fileCache == null) {
fileCache = new ArrayList<>(8); fileCache = new ArrayList<>(8);
fileWalker(localFolder.toFile(), p -> fileCache.add(new SubFile(localFolder.relativize(p) PathUtil.fileWalker(localFolder.toFile(), p -> fileCache.add(new SubFile(localFolder.relativize(p)
.toString(), FileHash.create(p.toFile())))); .toString(), FileHash.create(p.toFile()))));
/*//this tests if we can trick the system to load files that are not beneath the base-folder /*//this tests if we can trick the system to load files that are not beneath the base-folder
@ -444,26 +443,4 @@ abstract public class DataExchange {
BCLib.LOGGER.error(localBaseFolder + " (from " + folderID + ") is outside the game directory " + GAME_FOLDER + ". Sync is not allowed."); BCLib.LOGGER.error(localBaseFolder + " (from " + folderID + ") is outside the game directory " + GAME_FOLDER + ". Sync is not allowed.");
} }
} }
/**
* A simple directory walker that ignores dot-files
*
* @param path The path where you want to start
* @param pathConsumer The consumer called for each valid file. The consumer will get an absolute {@link Path}-Object
* for each visited file
*/
public static void fileWalker(File path, Consumer<Path> pathConsumer) {
if (!path.exists()) return;
for (final File f : path.listFiles()) {
if (f.getName()
.startsWith(".")) continue;
if (f.isDirectory()) {
fileWalker(f, pathConsumer);
}
else if (f.isFile()) {
pathConsumer.accept(f.toPath());
}
}
}
} }

View file

@ -1,8 +1,18 @@
package ru.bclib.util; package ru.bclib.util;
import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.function.Consumer;
public class PathUtil { public class PathUtil {
/**
* Tests if a path is a child-path.
* <p>
* A path is a child of another if it is located in the parent or any of the parents subdirectories
* @param parent The folder we search for the {@code child}
* @param child The folder you want to test
* @return {@code true} if {@code child} is in {@code parent} or any of its sub directories
*/
public static boolean isChildOf(Path parent, Path child){ public static boolean isChildOf(Path parent, Path child){
if (child==null || parent==null) return false; if (child==null || parent==null) return false;
@ -14,4 +24,25 @@ public class PathUtil {
return child.equals(parent); return child.equals(parent);
} }
/**
* A simple directory walker that ignores dot-files
*
* @param path The path where you want to start
* @param pathConsumer The consumer called for each valid file. The consumer will get an absolute {@link Path}-Object
* for each visited file
*/
public static void fileWalker(File path, Consumer<Path> pathConsumer) {
if (!path.exists()) return;
for (final File f : path.listFiles()) {
if (f.getName()
.startsWith(".")) continue;
if (f.isDirectory()) {
fileWalker(f, pathConsumer);
}
else if (f.isFile()) {
pathConsumer.accept(f.toPath());
}
}
}
} }