-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
118 lines (100 loc) · 4.25 KB
/
Utils.java
File metadata and controls
118 lines (100 loc) · 4.25 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
package russianSquare;
import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
class Utils {
/** Return the entire contents of FILE as a byte array. FILE must
* be a normal file. Throws IllegalArgumentException
* in case of problems. */
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());
}
}
/** Return the entire contents of FILE as a String. FILE must
* be a normal file. Throws IllegalArgumentException
* in case of problems. */
static String readContentsAsString(File file) {
return new String(readContents(file), StandardCharsets.UTF_8);
}
/** Write the result of concatenating the bytes in CONTENTS to FILE,
* creating or overwriting it as needed. Each object in CONTENTS may be
* either a String or a byte array. Throws IllegalArgumentException
* in case of problems. */
static void writeContents(File file, Object... contents) {
try {
if (file.isDirectory()) {
throw
new IllegalArgumentException("cannot overwrite directory");
}
BufferedOutputStream str =
new BufferedOutputStream(Files.newOutputStream(file.toPath()));
for (Object obj : contents) {
if (obj instanceof byte[]) {
str.write((byte[]) obj);
} else {
str.write(((String) obj).getBytes(StandardCharsets.UTF_8));
}
}
str.close();
} catch (IOException | ClassCastException excp) {
throw new IllegalArgumentException(excp.getMessage());
}
}
/** Return an object of type T read from FILE, casting it to EXPECTEDCLASS.
* Throws IllegalArgumentException in case of problems. */
static <T extends Serializable> T readObject(File file,
Class<T> expectedClass) {
try {
ObjectInputStream in =
new ObjectInputStream(new FileInputStream(file));
T result = expectedClass.cast(in.readObject());
in.close();
return result;
} catch (IOException | ClassCastException
| ClassNotFoundException excp) {
throw new IllegalArgumentException(excp.getMessage());
}
}
/** Write OBJ to FILE. */
static void writeObject(File file, Serializable obj) {
writeContents(file, serialize(obj));
}
/* OTHER FILE UTILITIES */
/** Return the concatentation of FIRST and OTHERS into a File designator,
* analogous to the 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 java.nio.file.Paths.get(String, String[])
* method. */
static File join(File first, String... others) {
return Paths.get(first.getPath(), others).toFile();
}
/* SERIALIZATION UTILITIES */
/** Returns a byte array containing the serialized contents of OBJ. */
static byte[] serialize(Serializable obj) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(stream);
objectStream.writeObject(obj);
objectStream.close();
return stream.toByteArray();
} catch (IOException excp) {
throw error("Internal error serializing commit.");
}
}
/* MESSAGES AND ERROR REPORTING */
/** Return a RuntimeException whose message is composed from MSG and ARGS as
* for the String.format method. */
static RuntimeException error(String msg, Object... args) {
return new RuntimeException(String.format(msg, args));
}
}