-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.java
More file actions
121 lines (107 loc) · 3.65 KB
/
Copy pathUtils.java
File metadata and controls
121 lines (107 loc) · 3.65 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
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
114
115
116
117
118
119
120
121
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
class Utils {
static String sha1(Object... vals) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
for (Object val : vals) {
if (val instanceof byte[]) {
md.update((byte[]) val);
} else if (val instanceof String) {
md.update(((String) val).getBytes(StandardCharsets.UTF_8));
} else {
throw new IllegalArgumentException("improper type to sha1");
}
}
Formatter result = new Formatter();
for (byte b : md.digest()) {
result.format("%02x", b);
}
return result.toString();
} catch (NoSuchAlgorithmException excp) {
throw new IllegalArgumentException("System does not support SHA-1");
}
}
static String sha1(List<Object> vals) {
return sha1(vals.toArray(new Object[vals.size()]));
}
static boolean restrictedDelete(File file) {
if (!(new File(file.getParentFile(), ".agile")).isDirectory()) {
throw new IllegalArgumentException("not .agile working directory");
}
if (!file.isDirectory()) {
return file.delete();
} else {
return false;
}
}
static boolean restrictedDelete(String file) {
return restrictedDelete(new File(file));
}
static byte[] readContents(File file) {
if (!file.isFile()) {
throw new IllegalArgumentException("must be a normal file");
}
try {
return Files.readAllBytes(file.toPath());
} catch (IOException excp) {
throw new IllegalArgumentException(excp.getMessage());
}
}
static void writeContents(File file, byte[] bytes) {
try {
if (file.isDirectory()) {
throw
new IllegalArgumentException("cannot overwrite directory");
}
Files.write(file.toPath(), bytes);
} catch (IOException excp) {
throw new IllegalArgumentException(excp.getMessage());
}
}
/* OTHER FILE UTILITIES */
/**
* Return the concatentation of FIRST and OTHERS into a File designator,
* analogous to the {@link java.nio.file.Paths.#get(String, String[])}
* method.
*/
static File join(String first, String... others) {
return Paths.get(first, others).toFile();
}
/**
* Return the concatentation of FIRST and OTHERS into a File designator,
* analogous to the {@link java.nio.file.Paths.#get(String, String[])}
* method.
*/
static File join(File first, String... others) {
return Paths.get(first.getPath(), others).toFile();
}
private static final FilenameFilter PLAIN_FILES =
new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile();
}
};
static List<String> plainFilenamesIn(File dir) {
String[] files = dir.list(PLAIN_FILES);
if (files == null) {
return null;
} else {
Arrays.sort(files);
return Arrays.asList(files);
}
}
static List<String> plainFilenamesIn(String dir) {
return plainFilenamesIn(new File(dir));
}
}