-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathUtil.java
More file actions
84 lines (71 loc) · 2.73 KB
/
Util.java
File metadata and controls
84 lines (71 loc) · 2.73 KB
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
package net.hypixel.resourcepack;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
public final class Util {
private Util() {
throw new UnsupportedOperationException("This class cannot be instantiated");
}
public static void copyDir(Path src, Path dest) throws IOException {
Files.walk(src).forEach(path -> {
try {
Files.copy(path, dest.resolve(src.relativize(path)));
} catch (Throwable e) {
throw Util.propagate(e);
}
});
}
public static void deleteDirectoryAndContents(Path dirPath) throws IOException {
if (Files.exists(dirPath)) {
Files.walk(dirPath).sorted(Comparator.reverseOrder())
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException ignored) {
}
});
}
}
public static boolean fileExistsCorrectCasing(Path path) throws IOException {
if (Files.exists(path)) {
return path.toAbsolutePath().equals(path.toRealPath());
}
return false;
}
public static JsonObject readJsonResource(Gson gson, String path) {
try (InputStream stream = PackConverter.class.getResourceAsStream(path)) {
if (stream == null) return null;
try (InputStreamReader streamReader = new InputStreamReader(stream)) {
return gson.fromJson(streamReader, JsonObject.class);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static BufferedImage readImageResource(String path) {
try (InputStream stream = PackConverter.class.getResourceAsStream(path)) {
if (stream == null) return null;
return ImageIO.read(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static JsonObject readJson(Gson gson, Path path) throws IOException {
return Util.readJson(gson, path, JsonObject.class);
}
public static <T> T readJson(Gson gson, Path path, Class<T> clazz) throws IOException {
// TODO Improvement: this will fail if there is a BOM in the file
return gson.fromJson(new JsonReader(Files.newBufferedReader(path)), clazz);
}
public static RuntimeException propagate(Throwable t) {
throw new RuntimeException(t);
}
}