-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileProcessor.java
74 lines (63 loc) · 3.21 KB
/
FileProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pl.geek.tewu.celeste_extractor;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class FileProcessor {
public static final String DATA_FILES_GLOB_PATTERN = "**/*.data";
private long filesCount = 1;
private AtomicLong processedFilesCount = new AtomicLong();
public boolean processDir(Path sourceDirPath, Path targetDirPath) throws IOException {
filesCount = countFiles(sourceDirPath, DATA_FILES_GLOB_PATTERN);
processedFilesCount.set(0);
walkDirParallel(sourceDirPath, DATA_FILES_GLOB_PATTERN, sourceFilePath -> {
Path targetFilePath = rerootPath(sourceDirPath, targetDirPath, sourceFilePath, sourceFilePath.getFileName().toString() + ".png");
if (processFile(sourceFilePath, targetFilePath))
processedFilesCount.incrementAndGet();
});
return processedFilesCount.get() == filesCount;
}
public boolean processFile(Path sourceFilePath, Path targetFilePath) {
if (targetFilePath.toFile().exists()) {
// If output file already exists, skip conversion
System.out.println("Skipping conversion of '" + sourceFilePath + "' file because output file '" + targetFilePath + "' already exists");
return true;
}
File targetFileParent = targetFilePath.getParent().toFile();
if (!targetFileParent.exists())
synchronized (this) { // We don't want to create the same dirs from multiple threads concurrently
if (!targetFileParent.exists() && !targetFileParent.mkdirs()) {
// If can't create output file's parent dirs, report failure
System.out.println("ERROR: Can't create '" + targetFilePath + "' file parent directories");
return false;
}
}
if (filesCount > 1) {
long fileNum = processedFilesCount.get() + 1;
System.out.print(fileNum + "/" + filesCount + " (" + fileNum * 100 / filesCount + "%) | ");
}
return DataFileConverter.convert(sourceFilePath, targetFilePath);
}
private long countFiles(Path dirPath, String globPattern) throws IOException {
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + globPattern);
return Files.walk(dirPath)
.filter(pathMatcher::matches)
.count();
}
private void walkDirParallel(Path dirPath, String globPattern, Consumer<Path> action) throws IOException {
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + globPattern);
Files.walk(dirPath)
.filter(pathMatcher::matches)
.collect(Collectors.toList())
.parallelStream()
.forEach(action);
}
private Path rerootPath(Path sourceRoot, Path targetRoot, Path sourceFile, String targetFileName) {
return targetRoot.resolve(sourceRoot.relativize(sourceFile)).resolveSibling(targetFileName);
}
}