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