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.BufferedReader; |
22 |
| import java.io.BufferedWriter; |
23 |
| import java.io.ByteArrayInputStream; |
24 |
| import java.io.File; |
25 |
| import java.io.FileInputStream; |
26 |
| import java.io.FileOutputStream; |
27 |
| import java.io.FileReader; |
28 |
| import java.io.FileWriter; |
29 |
| import java.io.IOException; |
30 |
| import java.io.InputStream; |
31 |
| import java.io.InputStreamReader; |
32 |
| import java.io.Reader; |
33 |
| import java.io.UnsupportedEncodingException; |
34 |
| import java.net.URL; |
35 |
| import java.util.Enumeration; |
36 |
| import java.util.Properties; |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| public final class IoUtility { |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
0
| private IoUtility() {
|
53 |
| |
54 |
| } |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
0
| public static String loadFile(final String filename)
|
64 |
| throws IOException { |
65 |
| |
66 |
0
| final StringBuffer buffer = new StringBuffer();
|
67 |
0
| loadFile(filename, buffer);
|
68 |
0
| return buffer.toString();
|
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
0
| public static void loadFile(final String filename,
|
79 |
| final StringBuffer buffer) |
80 |
| throws IOException { |
81 |
0
| loadFile(new File(filename), buffer);
|
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
0
| public static void loadStream(final InputStream in,
|
92 |
| final StringBuffer buffer) |
93 |
| throws IOException { |
94 |
| |
95 |
0
| buffer.setLength(0);
|
96 |
0
| int c;
|
97 |
0
| while ((c = in.read()) >= 0) {
|
98 |
0
| buffer.append((char) c);
|
99 |
| } |
100 |
| } |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
3161
| public static void loadFile(final File file,
|
110 |
| final StringBuffer buffer) |
111 |
| throws IOException { |
112 |
| |
113 |
3161
| final int size = (int) file.length();
|
114 |
3161
| buffer.setLength(0);
|
115 |
3161
| final FileReader in = new FileReader(file);
|
116 |
3161
| final char[] data = new char[size];
|
117 |
3161
| int charsread = 0;
|
118 |
3161
| while (charsread < size) {
|
119 |
3161
| charsread += in.read(data, charsread, size - charsread);
|
120 |
| } |
121 |
3161
| in.close();
|
122 |
3161
| buffer.insert(0, data);
|
123 |
| } |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
0
| public static final byte[] loadFileBinary(final File file) throws IOException {
|
133 |
0
| final int size = (int) file.length();
|
134 |
0
| final FileInputStream in = new FileInputStream(file);
|
135 |
0
| try {
|
136 |
0
| final byte[] data = new byte[size];
|
137 |
0
| int charsread = 0;
|
138 |
0
| while (charsread < size) {
|
139 |
0
| final int read = in.read(data, charsread, size - charsread);
|
140 |
0
| if (read == -1) {
|
141 |
0
| final byte[] result = new byte[charsread];
|
142 |
0
| System.arraycopy(data, 0, result, 0, charsread);
|
143 |
0
| return result;
|
144 |
| } |
145 |
0
| charsread += read;
|
146 |
| } |
147 |
0
| in.close();
|
148 |
0
| return data;
|
149 |
| } finally { |
150 |
0
| closeStream(in);
|
151 |
| } |
152 |
| } |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
0
| public static void loadFile(final URL url, final StringBuffer buffer) throws IOException {
|
162 |
0
| InputStream in = null;
|
163 |
0
| BufferedReader dis = null;
|
164 |
0
| try {
|
165 |
0
| in = url.openStream();
|
166 |
0
| dis = new BufferedReader(new InputStreamReader(in));
|
167 |
0
| in.read();
|
168 |
| |
169 |
0
| byte[] data = new byte[8 * 1024];
|
170 |
0
| int length;
|
171 |
| |
172 |
0
| while ((length = in.read(data)) != -1) {
|
173 |
0
| buffer.append(new String(data, 0, length));
|
174 |
| } |
175 |
| } finally { |
176 |
0
| closeStream(in);
|
177 |
0
| closeReader(dis);
|
178 |
| } |
179 |
| } |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
0
| public static final Reader stringToReader(final String data) {
|
191 |
0
| try {
|
192 |
0
| return new InputStreamReader(new ByteArrayInputStream(data.getBytes("ISO-8859-1")));
|
193 |
| } catch (UnsupportedEncodingException e) { |
194 |
0
| throw new RuntimeException(e);
|
195 |
| } |
196 |
| } |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
0
| public static void saveFile(final String filename, final String text)
|
206 |
| throws IOException { |
207 |
0
| saveFile(new File(filename), text);
|
208 |
| } |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
0
| public static void saveFile(final String filename, final StringBuffer text)
|
218 |
| throws IOException { |
219 |
0
| saveFile(new File(filename), text.toString());
|
220 |
| } |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
0
| public static void saveFile(final File file, final StringBuffer text)
|
231 |
| throws IOException { |
232 |
0
| saveFile(file, text.toString());
|
233 |
| } |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
1
| public static void saveFile(final File file, final String text)
|
244 |
| throws IOException { |
245 |
1
| BufferedWriter out = new BufferedWriter(
|
246 |
| new FileWriter(file)); |
247 |
1
| out.write(text);
|
248 |
1
| out.close();
|
249 |
| } |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
0
| public static void saveFileBinary(final File file, final byte[] data)
|
259 |
| throws IOException { |
260 |
0
| BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
|
261 |
0
| out.write(data);
|
262 |
0
| out.close();
|
263 |
| } |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
20
| public static void copyFile(final File from, final File to)
|
273 |
| throws IOException { |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
20
| if (from.getAbsoluteFile().equals(to.getAbsoluteFile())) {
|
280 |
0
| return;
|
281 |
| } |
282 |
20
| FileInputStream in = new FileInputStream(from);
|
283 |
20
| FileOutputStream out = new FileOutputStream(to);
|
284 |
| |
285 |
20
| byte[] data = new byte[8 * 1024];
|
286 |
20
| int length;
|
287 |
| |
288 |
?
| while ((length = in.read(data)) != -1) {
|
289 |
134
| out.write(data, 0, length);
|
290 |
| } |
291 |
20
| in.close();
|
292 |
20
| out.close();
|
293 |
| } |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
0
| public static String quote(final String unquoted) {
|
306 |
| |
307 |
0
| String result = "\"";
|
308 |
| |
309 |
0
| for (int i = 0; i < unquoted.length(); i++) {
|
310 |
0
| if (unquoted.charAt(i) == '\"') {
|
311 |
0
| result += "\"\"";
|
312 |
| } else { |
313 |
0
| result += unquoted.charAt(i);
|
314 |
| } |
315 |
| } |
316 |
0
| result += '\"';
|
317 |
0
| return result;
|
318 |
| } |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
0
| public static boolean isLetterDigitString(final String text) {
|
330 |
0
| if (text.length() <= 0) {
|
331 |
0
| return false;
|
332 |
| } |
333 |
0
| if (!Character.isLetter(text.charAt(0))) {
|
334 |
0
| return false;
|
335 |
| } |
336 |
0
| for (int i = 1; i < text.length(); i++) {
|
337 |
0
| if (!Character.isLetterOrDigit(text.charAt(i))) {
|
338 |
0
| return false;
|
339 |
| } |
340 |
| } |
341 |
0
| return true;
|
342 |
| } |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
0
| public static boolean deleteDir(final File directory) {
|
351 |
| |
352 |
| |
353 |
| |
354 |
0
| File candir;
|
355 |
0
| try {
|
356 |
0
| candir = directory.getCanonicalFile();
|
357 |
| } catch (IOException e) { |
358 |
0
| return false;
|
359 |
| } |
360 |
| |
361 |
| |
362 |
| |
363 |
0
| if (!candir.equals(directory.getAbsoluteFile())) {
|
364 |
| |
365 |
| |
366 |
| |
367 |
0
| return false;
|
368 |
| } |
369 |
| |
370 |
| |
371 |
| |
372 |
0
| File[] files = candir.listFiles();
|
373 |
0
| if (files != null) {
|
374 |
0
| for (int i = 0; i < files.length; i++) {
|
375 |
0
| File file = files[i];
|
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
0
| boolean deleted = file.delete();
|
381 |
0
| if (!deleted) {
|
382 |
| |
383 |
| |
384 |
0
| if (file.isDirectory()) {
|
385 |
0
| deleteDir(file);
|
386 |
| } |
387 |
| |
388 |
| |
389 |
| } |
390 |
| } |
391 |
| } |
392 |
| |
393 |
| |
394 |
| |
395 |
0
| return directory.delete();
|
396 |
| } |
397 |
| |
398 |
| |
399 |
| |
400 |
| |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
0
| public static StringBuffer getSpaces(final int length) {
|
406 |
0
| final StringBuffer buffer = new StringBuffer(length >= 0 ? length : 0);
|
407 |
0
| for (int i = 0; i < length; i++) {
|
408 |
0
| buffer.append(' ');
|
409 |
| } |
410 |
0
| return buffer;
|
411 |
| } |
412 |
| |
413 |
| |
414 |
| |
415 |
| |
416 |
| |
417 |
| |
418 |
| |
419 |
1201
| public static String getClassName(final Class clazz) {
|
420 |
1201
| return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
|
421 |
| } |
422 |
| |
423 |
| |
424 |
| |
425 |
| |
426 |
0
| public static void printAllSystemProperties() {
|
427 |
0
| Properties sysprops = System.getProperties();
|
428 |
0
| for (Enumeration e = sysprops.propertyNames(); e.hasMoreElements(); ) {
|
429 |
0
| String key = (String) e.nextElement();
|
430 |
0
| String value = sysprops.getProperty(key);
|
431 |
0
| System.out.println(key + "=" + value);
|
432 |
| } |
433 |
| } |
434 |
| |
435 |
| |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
0
| public static File getUserHomeDirectory() {
|
441 |
0
| return new File((String) System.getProperties().get("user.home"));
|
442 |
| } |
443 |
| |
444 |
| |
445 |
| |
446 |
| |
447 |
| |
448 |
| |
449 |
10
| public static void createNecessaryDirectories(final File file) {
|
450 |
10
| if (file.getParentFile() != null) {
|
451 |
10
| file.getParentFile().mkdirs();
|
452 |
| } |
453 |
| } |
454 |
| |
455 |
| |
456 |
| |
457 |
| |
458 |
| |
459 |
| |
460 |
| |
461 |
| |
462 |
0
| public static final String createRelativePath(final File orgin, final File next) {
|
463 |
0
| try {
|
464 |
0
| if (orgin.equals(next)) {
|
465 |
0
| return "";
|
466 |
| } |
467 |
0
| try {
|
468 |
0
| String org = orgin.getCanonicalPath().replace('\\', '/');
|
469 |
0
| if (orgin.isDirectory() && !org.endsWith("/")) {
|
470 |
0
| org += "/";
|
471 |
| } |
472 |
0
| String nex = next.getCanonicalPath().replace('\\', '/');
|
473 |
0
| if (next.isDirectory() && !nex.endsWith("/")) {
|
474 |
0
| nex += "/";
|
475 |
| } |
476 |
0
| int i = -1;
|
477 |
0
| int j = 0;
|
478 |
0
| while (0 <= (i = org.indexOf("/", j))) {
|
479 |
0
| if (i >= 0 && nex.length() > i
|
480 |
| && org.substring(j, i).equals( |
481 |
| nex.substring(j, i))) { |
482 |
0
| j = i + 1;
|
483 |
| } else { |
484 |
0
| break;
|
485 |
| } |
486 |
| } |
487 |
0
| if (j > 0) {
|
488 |
0
| i = j;
|
489 |
0
| StringBuffer result = new StringBuffer(nex.length());
|
490 |
0
| while (0 <= (i = org.indexOf("/", i))) {
|
491 |
0
| i++;
|
492 |
0
| result.append("../");
|
493 |
| } |
494 |
0
| result.append(nex.substring(j));
|
495 |
0
| return result.toString();
|
496 |
| } |
497 |
0
| return "/" + nex;
|
498 |
| } catch (RuntimeException e) { |
499 |
0
| return next.toString();
|
500 |
| } |
501 |
| } catch (IOException e) { |
502 |
0
| return next.toString();
|
503 |
| } |
504 |
| } |
505 |
| |
506 |
| |
507 |
| |
508 |
| |
509 |
0
| public static void waitln() {
|
510 |
0
| System.out.println("\n..press <return> to continue");
|
511 |
0
| try {
|
512 |
0
| (new java.io.BufferedReader(new java.io.InputStreamReader(
|
513 |
| System.in))).readLine(); |
514 |
| } catch (IOException e) { |
515 |
| |
516 |
| } |
517 |
| } |
518 |
| |
519 |
| |
520 |
| |
521 |
| |
522 |
| |
523 |
| |
524 |
0
| public static void closeStream(final InputStream in) {
|
525 |
0
| if (in != null) {
|
526 |
0
| try {
|
527 |
0
| in.close();
|
528 |
| } catch (Exception e) { |
529 |
| |
530 |
| } |
531 |
| } |
532 |
| } |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
0
| public static void closeReader(final Reader reader) {
|
540 |
0
| if (reader != null) {
|
541 |
0
| try {
|
542 |
0
| reader.close();
|
543 |
| } catch (Exception e) { |
544 |
| |
545 |
| } |
546 |
| } |
547 |
| } |
548 |
| |
549 |
| |
550 |
| |
551 |
| |
552 |
| |
553 |
| |
554 |
| |
555 |
| |
556 |
| |
557 |
| |
558 |
| |
559 |
| |
560 |
| |
561 |
| |
562 |
| |
563 |
| |
564 |
| |
565 |
| |
566 |
| |
567 |
| |
568 |
| |
569 |
| |
570 |
792
| public static void deleteLineLeadingWhitespace(final StringBuffer buffer) {
|
571 |
792
| int start = -1;
|
572 |
?
| while (0 <= (start = buffer.indexOf("\n", start + 1))) {
|
573 |
220
| if (start + 1 < buffer.length() && '\n' != buffer.charAt(start + 1)) {
|
574 |
195
| break;
|
575 |
| } |
576 |
| } |
577 |
792
| if (start >= 0) {
|
578 |
195
| int next = start + 1;
|
579 |
195
| while (next < buffer.length() && Character.isWhitespace(buffer.charAt(next))
|
580 |
| && '\n' != buffer.charAt(next)) { |
581 |
2522
| next++;
|
582 |
| } |
583 |
195
| final String empty = buffer.substring(start, next);
|
584 |
195
| if (empty.length() > 0) {
|
585 |
195
| ReplaceUtility.replace(buffer, empty, "\n");
|
586 |
| } |
587 |
| } |
588 |
| } |
589 |
| |
590 |
| } |