-
Notifications
You must be signed in to change notification settings - Fork 443
/
FileUtils.java
executable file
·206 lines (182 loc) · 5.76 KB
/
FileUtils.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package in.srain.cube.diskcache;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileUtils {
/**
* Returns the remainder of 'reader' as a string, closing it when done.
*/
public static String readFully(Reader reader) throws IOException {
try {
StringWriter writer = new StringWriter();
char[] buffer = new char[1024];
int count;
while ((count = reader.read(buffer)) != -1) {
writer.write(buffer, 0, count);
}
return writer.toString();
} finally {
reader.close();
}
}
/**
* Returns the ASCII characters up to but not including the next "\r\n", or
* "\n".
*
* @throws java.io.EOFException if the stream is exhausted before the next newline
* character.
*/
public static String readAsciiLine(InputStream in) throws IOException {
StringBuilder result = new StringBuilder(80);
while (true) {
int c = in.read();
if (c == -1) {
throw new EOFException();
} else if (c == '\n') {
break;
}
result.append((char) c);
}
int length = result.length();
if (length > 0 && result.charAt(length - 1) == '\r') {
result.setLength(length - 1);
}
return result.toString();
}
/**
* Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
*/
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
/**
* Try to delete directory in a fast way.
*/
public static void deleteDirectoryQuickly(File dir) throws IOException {
if (!dir.exists()) {
return;
}
final File to = new File(dir.getAbsolutePath() + System.currentTimeMillis());
dir.renameTo(to);
if (!dir.exists()) {
// rebuild
dir.mkdirs();
}
// try to run "rm -r" to remove the whole directory
if (to.exists()) {
String deleteCmd = "rm -r " + to;
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(deleteCmd);
process.waitFor();
} catch (IOException e) {
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!to.exists()) {
return;
}
deleteDirectoryRecursively(to);
if (to.exists()) {
to.delete();
}
}
public static void chmod(String mode, String path) {
try {
String command = "chmod " + mode + " " + path;
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* recursively delete
*
* @param dir
* @throws java.io.IOException
*/
public static void deleteDirectoryRecursively(File dir) throws IOException {
File[] files = dir.listFiles();
if (files == null) {
throw new IllegalArgumentException("not a directory: " + dir);
}
for (File file : files) {
if (file.isDirectory()) {
deleteDirectoryRecursively(file);
}
if (!file.delete()) {
throw new IOException("failed to delete file: " + file);
}
}
}
public static void deleteIfExists(File file) throws IOException {
if (file.exists() && !file.delete()) {
throw new IOException();
}
}
public static boolean writeString(String filePath, String content) {
File file = new File(filePath);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
FileWriter writer = null;
try {
writer = new FileWriter(file);
writer.write(content);
} catch (IOException e) {
} finally {
try {
if (writer != null) {
writer.close();
return true;
}
} catch (IOException e) {
}
}
return false;
}
public static String readString(String filePath) {
File file = new File(filePath);
if (!file.exists())
return null;
FileInputStream fileInput = null;
FileChannel channel = null;
try {
fileInput = new FileInputStream(filePath);
channel = fileInput.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(buffer.array());
return byteArrayOutputStream.toString();
} catch (Exception e) {
} finally {
if (fileInput != null) {
try {
fileInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (channel != null) {
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}