|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.utility; |
|
19 |
| |
|
20 |
| import java.io.BufferedOutputStream; |
|
21 |
| import java.io.BufferedWriter; |
|
22 |
| import java.io.File; |
|
23 |
| import java.io.FileInputStream; |
|
24 |
| import java.io.FileOutputStream; |
|
25 |
| import java.io.FileReader; |
|
26 |
| import java.io.FileWriter; |
|
27 |
| import java.io.IOException; |
|
28 |
| import java.io.InputStream; |
|
29 |
| import java.util.Enumeration; |
|
30 |
| import java.util.Properties; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public final class IoUtility { |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
0
| private IoUtility() {
|
|
45 |
| |
|
46 |
| } |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
0
| public static String loadFile(final String filename)
|
|
56 |
| throws IOException { |
|
57 |
| |
|
58 |
0
| final StringBuffer buffer = new StringBuffer();
|
|
59 |
0
| loadFile(filename, buffer);
|
|
60 |
0
| return buffer.toString();
|
|
61 |
| } |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
0
| public static void loadFile(final String filename,
|
|
71 |
| final StringBuffer buffer) |
|
72 |
| throws IOException { |
|
73 |
0
| loadFile(new File(filename), buffer);
|
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
0
| public static void loadStream(final InputStream in,
|
|
84 |
| final StringBuffer buffer) |
|
85 |
| throws IOException { |
|
86 |
| |
|
87 |
0
| buffer.setLength(0);
|
|
88 |
0
| int c;
|
|
89 |
0
| while ((c = in.read()) >= 0) {
|
|
90 |
0
| buffer.append((char) c);
|
|
91 |
| } |
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
410
| public static void loadFile(final File file,
|
|
102 |
| final StringBuffer buffer) |
|
103 |
| throws IOException { |
|
104 |
| |
|
105 |
410
| final int size = (int) file.length();
|
|
106 |
410
| buffer.setLength(0);
|
|
107 |
410
| final FileReader in = new FileReader(file);
|
|
108 |
410
| final char[] data = new char[size];
|
|
109 |
410
| int charsread = 0;
|
|
110 |
410
| while (charsread < size) {
|
|
111 |
410
| charsread += in.read(data, charsread, size - charsread);
|
|
112 |
| } |
|
113 |
410
| in.close();
|
|
114 |
410
| buffer.insert(0, data);
|
|
115 |
| } |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
0
| public static final byte[] loadFileBinary(final File file) throws IOException {
|
|
125 |
0
| final int size = (int) file.length();
|
|
126 |
0
| final FileInputStream in = new FileInputStream(file);
|
|
127 |
0
| final byte[] data = new byte[size];
|
|
128 |
0
| int charsread = 0;
|
|
129 |
0
| while (charsread < size) {
|
|
130 |
0
| charsread += in.read(data, charsread, size - charsread);
|
|
131 |
| } |
|
132 |
0
| in.close();
|
|
133 |
0
| return data;
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
0
| public static void saveFile(final String filename, final String text)
|
|
144 |
| throws IOException { |
|
145 |
0
| saveFile(new File(filename), text);
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
0
| public static void saveFile(final String filename, final StringBuffer text)
|
|
156 |
| throws IOException { |
|
157 |
0
| saveFile(new File(filename), text.toString());
|
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
0
| public static void saveFile(final File file, final StringBuffer text)
|
|
169 |
| throws IOException { |
|
170 |
0
| saveFile(file, text.toString());
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
1
| public static void saveFile(final File file, final String text)
|
|
182 |
| throws IOException { |
|
183 |
1
| BufferedWriter out = new BufferedWriter(
|
|
184 |
| new FileWriter(file)); |
|
185 |
1
| out.write(text);
|
|
186 |
1
| out.close();
|
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
0
| public static void saveFileBinary(final File file, final byte[] data)
|
|
197 |
| throws IOException { |
|
198 |
0
| BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
|
|
199 |
0
| out.write(data);
|
|
200 |
0
| out.close();
|
|
201 |
| } |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
12
| public static void copyFile(final File from, final File to)
|
|
211 |
| throws IOException { |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
12
| if (from.getAbsoluteFile().equals(to.getAbsoluteFile())) {
|
|
218 |
0
| return;
|
|
219 |
| } |
|
220 |
12
| FileInputStream in = new FileInputStream(from);
|
|
221 |
12
| FileOutputStream out = new FileOutputStream(to);
|
|
222 |
| |
|
223 |
12
| byte[] data = new byte[8 * 1024];
|
|
224 |
12
| int length;
|
|
225 |
| |
|
226 |
?
| while ((length = in.read(data)) != -1) {
|
|
227 |
43
| out.write(data, 0, length);
|
|
228 |
| } |
|
229 |
12
| in.close();
|
|
230 |
12
| out.close();
|
|
231 |
| } |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
0
| public static String quote(final String unquoted) {
|
|
244 |
| |
|
245 |
0
| String result = new String("\"");
|
|
246 |
| |
|
247 |
0
| for (int i = 0; i < unquoted.length(); i++) {
|
|
248 |
0
| if (unquoted.charAt(i) == '\"') {
|
|
249 |
0
| result += "\"\"";
|
|
250 |
| } else { |
|
251 |
0
| result += unquoted.charAt(i);
|
|
252 |
| } |
|
253 |
| } |
|
254 |
0
| result += '\"';
|
|
255 |
0
| return result;
|
|
256 |
| } |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
0
| public static boolean isLetterDigitString(final String text) {
|
|
268 |
0
| if (text.length() <= 0) {
|
|
269 |
0
| return false;
|
|
270 |
| } |
|
271 |
0
| if (!Character.isLetter(text.charAt(0))) {
|
|
272 |
0
| return false;
|
|
273 |
| } |
|
274 |
0
| for (int i = 1; i < text.length(); i++) {
|
|
275 |
0
| if (!Character.isLetterOrDigit(text.charAt(i))) {
|
|
276 |
0
| return false;
|
|
277 |
| } |
|
278 |
| } |
|
279 |
0
| return true;
|
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
0
| public static boolean deleteDir(final File directory) {
|
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
0
| File candir;
|
|
293 |
0
| try {
|
|
294 |
0
| candir = directory.getCanonicalFile();
|
|
295 |
| } catch (IOException e) { |
|
296 |
0
| return false;
|
|
297 |
| } |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
0
| if (!candir.equals(directory.getAbsoluteFile())) {
|
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
0
| return false;
|
|
306 |
| } |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
0
| File[] files = candir.listFiles();
|
|
311 |
0
| if (files != null) {
|
|
312 |
0
| for (int i = 0; i < files.length; i++) {
|
|
313 |
0
| File file = files[i];
|
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
0
| boolean deleted = file.delete();
|
|
319 |
0
| if (!deleted) {
|
|
320 |
| |
|
321 |
| |
|
322 |
0
| if (file.isDirectory()) {
|
|
323 |
0
| deleteDir(file);
|
|
324 |
| } |
|
325 |
| |
|
326 |
| |
|
327 |
| } |
|
328 |
| } |
|
329 |
| } |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
0
| return directory.delete();
|
|
334 |
| } |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
0
| public static StringBuffer getSpaces(final int length) {
|
|
344 |
0
| final StringBuffer buffer = new StringBuffer(length);
|
|
345 |
0
| for (int i = 0; i < length; i++) {
|
|
346 |
0
| buffer.append(' ');
|
|
347 |
| } |
|
348 |
0
| return buffer;
|
|
349 |
| } |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
18162
| public static String getClassName(final Class clazz) {
|
|
358 |
18162
| return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
|
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
0
| public static void printAllSystemProperties() {
|
|
365 |
0
| Properties sysprops = System.getProperties();
|
|
366 |
0
| for (Enumeration e = sysprops.propertyNames(); e.hasMoreElements(); ) {
|
|
367 |
0
| String key = (String) e.nextElement();
|
|
368 |
0
| String value = sysprops.getProperty(key);
|
|
369 |
0
| System.out.println(key + "=" + value);
|
|
370 |
| } |
|
371 |
| } |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
0
| public static File getUserHomeDirectory() {
|
|
379 |
0
| return new File((String) System.getProperties().get("user.home"));
|
|
380 |
| } |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
7
| public static void createNecessaryDirectories(final File file) {
|
|
388 |
7
| if (file.getParentFile() != null) {
|
|
389 |
7
| file.getParentFile().mkdirs();
|
|
390 |
| } |
|
391 |
| } |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
0
| public static final String createRelativePath(final File orgin, final File next) {
|
|
401 |
0
| try {
|
|
402 |
0
| if (orgin.equals(next)) {
|
|
403 |
0
| return "";
|
|
404 |
| } |
|
405 |
0
| try {
|
|
406 |
0
| String org = orgin.getCanonicalPath().replace('\\', '/');
|
|
407 |
0
| if (orgin.isDirectory() && !org.endsWith("/")) {
|
|
408 |
0
| org += "/";
|
|
409 |
| } |
|
410 |
0
| String nex = next.getCanonicalPath().replace('\\', '/');
|
|
411 |
0
| if (next.isDirectory() && !nex.endsWith("/")) {
|
|
412 |
0
| nex += "/";
|
|
413 |
| } |
|
414 |
0
| int i = -1;
|
|
415 |
0
| int j = 0;
|
|
416 |
0
| while (0 <= (i = org.indexOf("/", j))) {
|
|
417 |
0
| if (i >= 0 && nex.length() > i
|
|
418 |
| && org.substring(j, i).equals( |
|
419 |
| nex.substring(j, i))) { |
|
420 |
0
| j = i + 1;
|
|
421 |
| } else { |
|
422 |
0
| break;
|
|
423 |
| } |
|
424 |
| } |
|
425 |
0
| if (j > 0) {
|
|
426 |
0
| i = j;
|
|
427 |
0
| StringBuffer result = new StringBuffer(nex.length());
|
|
428 |
0
| while (0 <= (i = org.indexOf("/", i))) {
|
|
429 |
0
| i++;
|
|
430 |
0
| result.append("../");
|
|
431 |
| } |
|
432 |
0
| result.append(nex.substring(j));
|
|
433 |
0
| return result.toString();
|
|
434 |
| } |
|
435 |
0
| return "/" + nex;
|
|
436 |
| } catch (RuntimeException e) { |
|
437 |
0
| return next.toString();
|
|
438 |
| } |
|
439 |
| } catch (IOException e) { |
|
440 |
0
| return next.toString();
|
|
441 |
| } |
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
0
| public static void waitln() {
|
|
448 |
0
| System.out.println("\n..press <return> to continue");
|
|
449 |
0
| try {
|
|
450 |
0
| (new java.io.BufferedReader(new java.io.InputStreamReader(
|
|
451 |
| System.in))).readLine(); |
|
452 |
| } catch (IOException e) { |
|
453 |
| |
|
454 |
| } |
|
455 |
| } |
|
456 |
| |
|
457 |
| } |