-
Notifications
You must be signed in to change notification settings - Fork 52
/
NIOFileUtil.java
113 lines (105 loc) · 3.64 KB
/
NIOFileUtil.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package org.seqdoop.hadoop_bam.util;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class NIOFileUtil {
private NIOFileUtil() {
}
static final String PARTS_GLOB = "glob:**/part-[mr]-[0-9][0-9][0-9][0-9][0-9]*";
/**
* Convert the given path {@link URI} to a {@link Path} object.
* @param uri the path to convert
* @return a {@link Path} object
*/
public static Path asPath(URI uri) {
try {
return Paths.get(uri);
} catch (FileSystemNotFoundException e) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
throw e;
}
try {
return FileSystems.newFileSystem(uri, new HashMap<>(), cl).provider().getPath(uri);
} catch (IOException ex) {
throw new RuntimeException("Cannot create filesystem for " + uri, ex);
}
}
}
/**
* Convert the given path string to a {@link Path} object.
* @param path the path to convert
* @return a {@link Path} object
*/
public static Path asPath(String path) {
URI uri = URI.create(path);
return uri.getScheme() == null ? Paths.get(path) : asPath(uri);
}
/**
* Delete the given directory and all of its contents if non-empty.
* @param directory the directory to delete
* @throws IOException
*/
static void deleteRecursive(Path directory) throws IOException {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.deleteIfExists(dir);
return FileVisitResult.CONTINUE;
}
});
}
/**
* Returns all the files in a directory that match the given pattern, and that don't
* have the given extension.
* @param directory the directory to look for files in, subdirectories are not
* considered
* @param syntaxAndPattern the syntax and pattern to use for matching (see
* {@link java.nio.file.FileSystem#getPathMatcher}
* @param excludesExt the extension to exclude, or null to exclude nothing
* @return a list of files, sorted by name
* @throws IOException
*/
static List<Path> getFilesMatching(Path directory,
String syntaxAndPattern, String excludesExt) throws IOException {
PathMatcher matcher = directory.getFileSystem().getPathMatcher(syntaxAndPattern);
List<Path> parts = Files.walk(directory)
.filter(matcher::matches)
.filter(path -> excludesExt == null || !path.toString().endsWith(excludesExt))
.collect(Collectors.toList());
Collections.sort(parts);
return parts;
}
/**
* Merge the given part files in order into an output stream.
* This deletes the parts.
* @param parts the part files to merge
* @param out the stream to write each file into, in order
* @throws IOException
*/
static void mergeInto(List<Path> parts, OutputStream out)
throws IOException {
for (final Path part : parts) {
Files.copy(part, out);
Files.delete(part);
}
}
}