|
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 |
| package edu.rice.cs.plt.io; |
|
36 |
| |
|
37 |
| import java.io.*; |
|
38 |
| import java.lang.reflect.Field; |
|
39 |
| import java.lang.reflect.Modifier; |
|
40 |
| import java.util.ArrayList; |
|
41 |
| import java.util.Date; |
|
42 |
| import java.util.HashSet; |
|
43 |
| import java.util.Iterator; |
|
44 |
| import java.util.List; |
|
45 |
| import java.util.LinkedList; |
|
46 |
| import java.util.NoSuchElementException; |
|
47 |
| import java.util.Set; |
|
48 |
| import java.util.regex.Pattern; |
|
49 |
| |
|
50 |
| import edu.rice.cs.plt.debug.ThreadSnapshot; |
|
51 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
52 |
| import edu.rice.cs.plt.iter.ReadOnlyIterator; |
|
53 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
54 |
| import edu.rice.cs.plt.lambda.*; |
|
55 |
| import edu.rice.cs.plt.tuple.*; |
|
56 |
| import edu.rice.cs.plt.recur.RecursionStack; |
|
57 |
| import edu.rice.cs.plt.reflect.ReflectUtil; |
|
58 |
| import edu.rice.cs.plt.text.TextUtil; |
|
59 |
| |
|
60 |
| import static edu.rice.cs.plt.debug.DebugUtil.error; |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| public final class IOUtil { |
|
67 |
| |
|
68 |
| |
|
69 |
0
| private IOUtil() {}
|
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| public static final File WORKING_DIRECTORY = IOUtil.attemptAbsoluteFile(new File(System.getProperty("user.dir", ""))); |
|
76 |
| |
|
77 |
| |
|
78 |
| public static final Lambda<String, File> FILE_FACTORY = new FileFactory(); |
|
79 |
| |
|
80 |
| private static class FileFactory implements Lambda<String, File>, Serializable { |
|
81 |
21
| private FileFactory() {}
|
|
82 |
110
| public File value(String name) { return new File(name); }
|
|
83 |
| }; |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
65
| public static File attemptAbsoluteFile(File f) {
|
|
91 |
65
| try { return f.getAbsoluteFile(); }
|
|
92 |
0
| catch (SecurityException e) { return f; }
|
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
0
| public static SizedIterable<File> getAbsoluteFiles(Iterable<? extends File> files) {
|
|
101 |
0
| return IterUtil.mapSnapshot(files, GET_ABSOLUTE_FILE);
|
|
102 |
| } |
|
103 |
| |
|
104 |
| private static final Lambda<File, File> GET_ABSOLUTE_FILE = new Lambda<File, File>() { |
|
105 |
0
| public File value(File arg) { return arg.getAbsoluteFile(); }
|
|
106 |
| }; |
|
107 |
| |
|
108 |
| |
|
109 |
4
| public static SizedIterable<File> attemptAbsoluteFiles(Iterable<? extends File> files) {
|
|
110 |
4
| return IterUtil.mapSnapshot(files, ATTEMPT_ABSOLUTE_FILE);
|
|
111 |
| } |
|
112 |
| |
|
113 |
| private static final Lambda<File, File> ATTEMPT_ABSOLUTE_FILE = new Lambda<File, File>() { |
|
114 |
37
| public File value(File arg) { return attemptAbsoluteFile(arg); }
|
|
115 |
| }; |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
0
| public static File attemptCanonicalFile(File f) {
|
|
124 |
0
| try { return f.getCanonicalFile(); }
|
|
125 |
0
| catch (IOException e) { return attemptAbsoluteFile(f); }
|
|
126 |
0
| catch (SecurityException e) { return attemptAbsoluteFile(f); }
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
0
| public static SizedIterable<File> getCanonicalFiles(Iterable<? extends File> files) throws IOException {
|
|
137 |
0
| try { return IterUtil.mapSnapshot(files, GET_CANONICAL_FILE); }
|
|
138 |
| catch (WrappedException e) { |
|
139 |
0
| if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); }
|
|
140 |
0
| else { throw e; }
|
|
141 |
| } |
|
142 |
| } |
|
143 |
| |
|
144 |
| private static final Lambda<File, File> GET_CANONICAL_FILE = new Lambda<File, File>() { |
|
145 |
0
| public File value(File arg) {
|
|
146 |
0
| try { return arg.getCanonicalFile(); }
|
|
147 |
0
| catch (IOException e) { throw new WrappedException(e); }
|
|
148 |
| } |
|
149 |
| }; |
|
150 |
| |
|
151 |
| |
|
152 |
0
| public static SizedIterable<File> attemptCanonicalFiles(Iterable<? extends File> files) {
|
|
153 |
0
| return IterUtil.mapSnapshot(files, ATTEMPT_CANONICAL_FILE);
|
|
154 |
| } |
|
155 |
| |
|
156 |
| private static final Lambda<File, File> ATTEMPT_CANONICAL_FILE = new Lambda<File, File>() { |
|
157 |
0
| public File value(File arg) { return attemptAbsoluteFile(arg); }
|
|
158 |
| }; |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
0
| public static boolean attemptCanRead(File f) {
|
|
165 |
0
| try { return f.canRead(); }
|
|
166 |
0
| catch (SecurityException e) { return false; }
|
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
0
| public static boolean attemptCanWrite(File f) {
|
|
174 |
0
| try { return f.canWrite(); }
|
|
175 |
0
| catch (SecurityException e) { return false; }
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
3
| public static boolean attemptExists(File f) {
|
|
183 |
3
| try { return f.exists(); }
|
|
184 |
0
| catch (SecurityException e) { return false; }
|
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
3
| public static boolean attemptIsDirectory(File f) {
|
|
192 |
3
| try { return f.isDirectory(); }
|
|
193 |
0
| catch (SecurityException e) { return false; }
|
|
194 |
| } |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
3
| public static boolean attemptIsFile(File f) {
|
|
201 |
3
| try { return f.isFile(); }
|
|
202 |
0
| catch (SecurityException e) { return false; }
|
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
0
| public static boolean attemptIsHidden(File f) {
|
|
210 |
0
| try { return f.isHidden(); }
|
|
211 |
0
| catch (SecurityException e) { return false; }
|
|
212 |
| } |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
0
| public static long attemptLastModified(File f) {
|
|
219 |
0
| try { return f.lastModified(); }
|
|
220 |
0
| catch (SecurityException e) { return 0l; }
|
|
221 |
| } |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
0
| public static long attemptLength(File f) {
|
|
228 |
0
| try { return f.length(); }
|
|
229 |
0
| catch (SecurityException e) { return 0l; }
|
|
230 |
| } |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
0
| public static boolean attemptCreateNewFile(File f) {
|
|
237 |
0
| try { return f.createNewFile(); }
|
|
238 |
0
| catch (IOException e) { return false; }
|
|
239 |
0
| catch (SecurityException e) { return false; }
|
|
240 |
| } |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
0
| public static boolean attemptDelete(File f) {
|
|
247 |
0
| try { return f.delete(); }
|
|
248 |
0
| catch (SecurityException e) { return false; }
|
|
249 |
| } |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
0
| public static void attemptDeleteOnExit(File f) {
|
|
256 |
0
| try { f.deleteOnExit(); }
|
|
257 |
| catch (SecurityException e) { } |
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
0
| public static File[] attemptListFiles(File f) {
|
|
265 |
0
| try { return f.listFiles(); }
|
|
266 |
0
| catch (SecurityException e) { return null; }
|
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
0
| public static File[] attemptListFiles(File f, FileFilter filter) {
|
|
274 |
0
| try { return f.listFiles(filter); }
|
|
275 |
0
| catch (SecurityException e) { return null; }
|
|
276 |
| } |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
0
| public static File[] attemptListFiles(File f, Predicate<? super File> filter) {
|
|
284 |
0
| return attemptListFiles(f, (FileFilter) asFilePredicate(filter));
|
|
285 |
| } |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
0
| public static File[] attemptListFiles(File f, FilePredicate filter) {
|
|
293 |
0
| return attemptListFiles(f, (FileFilter) filter);
|
|
294 |
| } |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
0
| public static SizedIterable<File> attemptListFilesAsIterable(File f) {
|
|
302 |
0
| File[] result = attemptListFiles(f);
|
|
303 |
0
| if (result == null) { return IterUtil.empty(); }
|
|
304 |
0
| else { return IterUtil.asIterable(result); }
|
|
305 |
| } |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
0
| public static SizedIterable<File> attemptListFilesAsIterable(File f, FileFilter filter) {
|
|
313 |
0
| File[] result = attemptListFiles(f, filter);
|
|
314 |
0
| if (result == null) { return IterUtil.empty(); }
|
|
315 |
0
| else { return IterUtil.asIterable(result); }
|
|
316 |
| } |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
0
| public static SizedIterable<File> attemptListFilesAsIterable(File f, Predicate<? super File> filter) {
|
|
324 |
0
| return attemptListFilesAsIterable(f, (FileFilter) asFilePredicate(filter));
|
|
325 |
| } |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
0
| public static SizedIterable<File> attemptListFilesAsIterable(File f, FilePredicate filter) {
|
|
334 |
0
| return attemptListFilesAsIterable(f, (FileFilter) filter);
|
|
335 |
| } |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
0
| public static boolean attemptMkdir(File f) {
|
|
342 |
0
| try { return f.mkdir(); }
|
|
343 |
0
| catch (SecurityException e) { return false; }
|
|
344 |
| } |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
0
| public static boolean attemptMkdirs(File f) {
|
|
351 |
0
| try { return f.mkdirs(); }
|
|
352 |
0
| catch (SecurityException e) { return false; }
|
|
353 |
| } |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
0
| public static boolean attemptRenameTo(File f, File dest) {
|
|
360 |
0
| try { return f.renameTo(dest); }
|
|
361 |
0
| catch (SecurityException e) { return false; }
|
|
362 |
| } |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
0
| public static boolean attemptMove(File f, File dest) {
|
|
370 |
0
| attemptDelete(dest);
|
|
371 |
0
| return attemptRenameTo(f, dest);
|
|
372 |
| } |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
0
| public static boolean attemptSetLastModified(File f, long time) {
|
|
379 |
0
| try { return f.setLastModified(time); }
|
|
380 |
0
| catch (SecurityException e) { return false; }
|
|
381 |
| } |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
0
| public static boolean attemptSetReadOnly(File f) {
|
|
388 |
0
| try { return f.setReadOnly(); }
|
|
389 |
0
| catch (SecurityException e) { return false; }
|
|
390 |
| } |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
15
| public static File canonicalCase(File f) {
|
|
401 |
15
| File lowered = new File(f.getPath().toLowerCase());
|
|
402 |
15
| if (f.equals(lowered)) { return lowered; }
|
|
403 |
0
| else { return f; }
|
|
404 |
| } |
|
405 |
| |
|
406 |
| |
|
407 |
0
| public static SizedIterable<File> canonicalCases(Iterable<? extends File> files) {
|
|
408 |
0
| return IterUtil.mapSnapshot(files, CANONICAL_CASE);
|
|
409 |
| } |
|
410 |
| |
|
411 |
| private static final Lambda<File, File> CANONICAL_CASE = new Lambda<File, File>() { |
|
412 |
0
| public File value(File arg) { return canonicalCase(arg); }
|
|
413 |
| }; |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
0
| public static boolean isMember(File f, File ancestor) {
|
|
420 |
0
| File parent = f;
|
|
421 |
0
| while (parent != null) {
|
|
422 |
0
| if (parent.equals(ancestor)) { return true; }
|
|
423 |
0
| parent = parent.getParentFile();
|
|
424 |
| } |
|
425 |
0
| return false;
|
|
426 |
| } |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
0
| public static SizedIterable<File> fullPath(File f) {
|
|
435 |
0
| SizedIterable<File> result = IterUtil.singleton(f);
|
|
436 |
0
| File parent = f.getParentFile();
|
|
437 |
0
| while (parent != null) {
|
|
438 |
0
| result = IterUtil.compose(parent, result);
|
|
439 |
0
| parent = parent.getParentFile();
|
|
440 |
| } |
|
441 |
0
| return result;
|
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
0
| public static boolean deleteRecursively(File f) {
|
|
450 |
0
| return deleteRecursively(f, new RecursionStack<File>(Wrapper.<File>factory()));
|
|
451 |
| } |
|
452 |
| |
|
453 |
| |
|
454 |
0
| private static boolean deleteRecursively(File f, final RecursionStack<File> stack) {
|
|
455 |
0
| if (f.isDirectory()) {
|
|
456 |
0
| try {
|
|
457 |
0
| final File canonicalF = f.getCanonicalFile();
|
|
458 |
0
| Runnable deleteMembers = new Runnable() {
|
|
459 |
0
| public void run() {
|
|
460 |
0
| for (File child : attemptListFilesAsIterable(canonicalF)) { deleteRecursively(child, stack); }
|
|
461 |
| } |
|
462 |
| }; |
|
463 |
0
| stack.run(deleteMembers, canonicalF);
|
|
464 |
| } |
|
465 |
| catch (IOException e) { } |
|
466 |
| catch (SecurityException e) { } |
|
467 |
| } |
|
468 |
0
| return attemptDelete(f);
|
|
469 |
| } |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
0
| public static void deleteOnExitRecursively(File f) {
|
|
479 |
0
| deleteOnExitRecursively(f, new RecursionStack<File>(Wrapper.<File>factory()));
|
|
480 |
| } |
|
481 |
| |
|
482 |
| |
|
483 |
0
| private static void deleteOnExitRecursively(File f, final RecursionStack<File> stack) {
|
|
484 |
0
| attemptDeleteOnExit(f);
|
|
485 |
0
| if (f.isDirectory()) {
|
|
486 |
| |
|
487 |
| |
|
488 |
0
| try {
|
|
489 |
0
| final File canonicalF = f.getCanonicalFile();
|
|
490 |
0
| Runnable markMembers = new Runnable() {
|
|
491 |
0
| public void run() {
|
|
492 |
0
| for (File child : attemptListFilesAsIterable(canonicalF)) { deleteOnExitRecursively(child, stack); }
|
|
493 |
| } |
|
494 |
| }; |
|
495 |
0
| stack.run(markMembers, canonicalF);
|
|
496 |
| } |
|
497 |
| catch (IOException e) { } |
|
498 |
| catch (SecurityException e) { } |
|
499 |
| } |
|
500 |
| } |
|
501 |
| |
|
502 |
| |
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
0
| public static SizedIterable<File> listFilesRecursively(File f) {
|
|
512 |
0
| return listFilesRecursively(f, ALWAYS_ACCEPT, ALWAYS_ACCEPT);
|
|
513 |
| } |
|
514 |
| |
|
515 |
| |
|
516 |
| |
|
517 |
| |
|
518 |
| |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
| |
|
526 |
0
| public static SizedIterable<File> listFilesRecursively(File f, FileFilter filter) {
|
|
527 |
0
| return listFilesRecursively(f, filter, ALWAYS_ACCEPT);
|
|
528 |
| } |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
0
| public static SizedIterable<File> listFilesRecursively(File f, Predicate<? super File> filter) {
|
|
542 |
0
| return listFilesRecursively(f, asFilePredicate(filter), ALWAYS_ACCEPT);
|
|
543 |
| } |
|
544 |
| |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
0
| public static SizedIterable<File> listFilesRecursively(File f, FilePredicate filter) {
|
|
558 |
0
| return listFilesRecursively(f, filter, ALWAYS_ACCEPT);
|
|
559 |
| } |
|
560 |
| |
|
561 |
| |
|
562 |
| |
|
563 |
| |
|
564 |
| |
|
565 |
| |
|
566 |
| |
|
567 |
| |
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
| |
|
572 |
| |
|
573 |
0
| public static SizedIterable<File> listFilesRecursively(File f, FileFilter filter, FileFilter recursionFilter) {
|
|
574 |
0
| return listFilesRecursively(f, filter, recursionFilter, new RecursionStack<File>(Wrapper.<File>factory()));
|
|
575 |
| } |
|
576 |
| |
|
577 |
| |
|
578 |
| |
|
579 |
| |
|
580 |
| |
|
581 |
| |
|
582 |
| |
|
583 |
| |
|
584 |
| |
|
585 |
| |
|
586 |
| |
|
587 |
| |
|
588 |
| |
|
589 |
0
| public static SizedIterable<File> listFilesRecursively(File f, Predicate<? super File> filter,
|
|
590 |
| Predicate<? super File> recursionFilter) { |
|
591 |
0
| return listFilesRecursively(f, asFilePredicate(filter), asFilePredicate(recursionFilter),
|
|
592 |
| new RecursionStack<File>(Wrapper.<File>factory())); |
|
593 |
| } |
|
594 |
| |
|
595 |
| |
|
596 |
| |
|
597 |
| |
|
598 |
| |
|
599 |
| |
|
600 |
| |
|
601 |
| |
|
602 |
| |
|
603 |
| |
|
604 |
| |
|
605 |
| |
|
606 |
| |
|
607 |
| |
|
608 |
0
| public static SizedIterable<File> listFilesRecursively(File f, FilePredicate filter, FilePredicate recursionFilter) {
|
|
609 |
0
| return listFilesRecursively(f, filter, recursionFilter, new RecursionStack<File>(Wrapper.<File>factory()));
|
|
610 |
| } |
|
611 |
| |
|
612 |
| |
|
613 |
0
| private static SizedIterable<File> listFilesRecursively(final File f, final FileFilter filter,
|
|
614 |
| final FileFilter recursionFilter, |
|
615 |
| final RecursionStack<File> stack) { |
|
616 |
0
| SizedIterable<File> result = (filter.accept(f)) ? IterUtil.singleton(f) : IterUtil.<File>empty();
|
|
617 |
0
| if (f.isDirectory() && recursionFilter.accept(f)) {
|
|
618 |
0
| Thunk<Iterable<File>> getMembers = new Thunk<Iterable<File>>() {
|
|
619 |
0
| public Iterable<File> value() {
|
|
620 |
0
| Iterable<File> dirFiles = IterUtil.empty();
|
|
621 |
0
| for (File child : attemptListFilesAsIterable(f)) {
|
|
622 |
0
| dirFiles = IterUtil.compose(dirFiles, listFilesRecursively(child, filter, recursionFilter, stack));
|
|
623 |
| } |
|
624 |
0
| return dirFiles;
|
|
625 |
| } |
|
626 |
| }; |
|
627 |
0
| try {
|
|
628 |
0
| result = IterUtil.compose(result, stack.apply(getMembers, IterUtil.<File>empty(), f.getCanonicalFile()));
|
|
629 |
| } |
|
630 |
| catch (IOException e) { } |
|
631 |
| catch (SecurityException e) { } |
|
632 |
| } |
|
633 |
0
| return result;
|
|
634 |
| } |
|
635 |
| |
|
636 |
| |
|
637 |
| |
|
638 |
| |
|
639 |
| |
|
640 |
| |
|
641 |
0
| public static byte[] toByteArray(File file) throws IOException {
|
|
642 |
0
| FileInputStream input = new FileInputStream(file);
|
|
643 |
0
| try { return toByteArray(input); }
|
|
644 |
0
| finally { input.close(); }
|
|
645 |
| } |
|
646 |
| |
|
647 |
| |
|
648 |
| |
|
649 |
| |
|
650 |
| |
|
651 |
| |
|
652 |
| |
|
653 |
| |
|
654 |
0
| public static StringBuffer toStringBuffer(File file) throws IOException {
|
|
655 |
0
| FileReader reader = new FileReader(file);
|
|
656 |
0
| try { return toStringBuffer(reader); }
|
|
657 |
0
| finally { reader.close(); }
|
|
658 |
| } |
|
659 |
| |
|
660 |
| |
|
661 |
| |
|
662 |
| |
|
663 |
| |
|
664 |
| |
|
665 |
0
| public static String toString(File file) throws IOException {
|
|
666 |
0
| FileReader reader = new FileReader(file);
|
|
667 |
0
| try { return toString(reader); }
|
|
668 |
0
| finally { reader.close(); }
|
|
669 |
| } |
|
670 |
| |
|
671 |
| |
|
672 |
| |
|
673 |
| |
|
674 |
| |
|
675 |
| |
|
676 |
| |
|
677 |
| |
|
678 |
0
| public static Iterator<String> readLines(File file) throws IOException {
|
|
679 |
0
| FileReader reader = new FileReader(file);
|
|
680 |
0
| return readLines(reader);
|
|
681 |
| } |
|
682 |
| |
|
683 |
| |
|
684 |
| |
|
685 |
| |
|
686 |
| |
|
687 |
| |
|
688 |
0
| public static int adler32Hash(File file) throws IOException {
|
|
689 |
0
| InputStream input = new FileInputStream(file);
|
|
690 |
0
| try { return adler32Hash(input); }
|
|
691 |
0
| finally { input.close(); }
|
|
692 |
| } |
|
693 |
| |
|
694 |
| |
|
695 |
| |
|
696 |
| |
|
697 |
| |
|
698 |
| |
|
699 |
0
| public static int crc32Hash(File file) throws IOException {
|
|
700 |
0
| InputStream input = new FileInputStream(file);
|
|
701 |
0
| try { return crc32Hash(input); }
|
|
702 |
0
| finally { input.close(); }
|
|
703 |
| } |
|
704 |
| |
|
705 |
| |
|
706 |
| |
|
707 |
| |
|
708 |
| |
|
709 |
| |
|
710 |
0
| public static byte[] md5Hash(File file) throws IOException {
|
|
711 |
0
| InputStream input = new FileInputStream(file);
|
|
712 |
0
| try { return md5Hash(input); }
|
|
713 |
0
| finally { input.close(); }
|
|
714 |
| } |
|
715 |
| |
|
716 |
| |
|
717 |
| |
|
718 |
| |
|
719 |
| |
|
720 |
| |
|
721 |
0
| public static byte[] sha1Hash(File file) throws IOException {
|
|
722 |
0
| InputStream input = new FileInputStream(file);
|
|
723 |
0
| try { return sha1Hash(input); }
|
|
724 |
0
| finally { input.close(); }
|
|
725 |
| } |
|
726 |
| |
|
727 |
| |
|
728 |
| |
|
729 |
| |
|
730 |
| |
|
731 |
| |
|
732 |
0
| public static byte[] sha256Hash(File file) throws IOException {
|
|
733 |
0
| InputStream input = new FileInputStream(file);
|
|
734 |
0
| try { return sha256Hash(input); }
|
|
735 |
0
| finally { input.close(); }
|
|
736 |
| } |
|
737 |
| |
|
738 |
| |
|
739 |
| |
|
740 |
| |
|
741 |
| |
|
742 |
| |
|
743 |
| |
|
744 |
| |
|
745 |
| |
|
746 |
0
| public static void copyFile(File source, File dest) throws IOException {
|
|
747 |
0
| FileInputStream in = new FileInputStream(source);
|
|
748 |
0
| try {
|
|
749 |
0
| FileOutputStream out = new FileOutputStream(dest);
|
|
750 |
0
| try { copyInputStream(in, out); }
|
|
751 |
0
| finally { out.close(); }
|
|
752 |
| } |
|
753 |
0
| finally { in.close(); }
|
|
754 |
| } |
|
755 |
| |
|
756 |
| |
|
757 |
| |
|
758 |
| |
|
759 |
| |
|
760 |
| |
|
761 |
| |
|
762 |
| |
|
763 |
| |
|
764 |
| |
|
765 |
0
| public static void copyFile(File source, File dest, byte[] buffer) throws IOException {
|
|
766 |
0
| FileInputStream in = new FileInputStream(source);
|
|
767 |
0
| try {
|
|
768 |
0
| FileOutputStream out = new FileOutputStream(dest);
|
|
769 |
0
| try { copyInputStream(in, out, buffer); }
|
|
770 |
0
| finally { out.close(); }
|
|
771 |
| } |
|
772 |
0
| finally { in.close(); }
|
|
773 |
| } |
|
774 |
| |
|
775 |
| |
|
776 |
| |
|
777 |
| |
|
778 |
| |
|
779 |
| |
|
780 |
| |
|
781 |
| |
|
782 |
0
| public static void writeStringToFile(File file, String text) throws IOException {
|
|
783 |
0
| writeStringToFile(file, text, false);
|
|
784 |
| } |
|
785 |
| |
|
786 |
| |
|
787 |
| |
|
788 |
| |
|
789 |
| |
|
790 |
| |
|
791 |
| |
|
792 |
0
| public static boolean attemptWriteStringToFile(File file, String text) {
|
|
793 |
0
| try { writeStringToFile(file, text); return true; }
|
|
794 |
0
| catch (IOException e) { return false; }
|
|
795 |
0
| catch (SecurityException e) { return false; }
|
|
796 |
| } |
|
797 |
| |
|
798 |
| |
|
799 |
| |
|
800 |
| |
|
801 |
| |
|
802 |
| |
|
803 |
| |
|
804 |
| |
|
805 |
| |
|
806 |
| |
|
807 |
0
| public static void writeStringToFile(File file, String text, boolean append) throws IOException {
|
|
808 |
0
| FileWriter writer = new FileWriter(file, append);
|
|
809 |
0
| try { writer.write(text); }
|
|
810 |
0
| finally { writer.close(); }
|
|
811 |
| } |
|
812 |
| |
|
813 |
| |
|
814 |
| |
|
815 |
| |
|
816 |
| |
|
817 |
| |
|
818 |
| |
|
819 |
| |
|
820 |
| |
|
821 |
0
| public static boolean attemptWriteStringToFile(File file, String text, boolean append) {
|
|
822 |
0
| try { writeStringToFile(file, text, append); return true; }
|
|
823 |
0
| catch (IOException e) { return false; }
|
|
824 |
0
| catch (SecurityException e) { return false; }
|
|
825 |
| } |
|
826 |
| |
|
827 |
| |
|
828 |
| |
|
829 |
| |
|
830 |
| |
|
831 |
| |
|
832 |
| |
|
833 |
0
| public static File createAndMarkTempFile(String prefix, String suffix) throws IOException {
|
|
834 |
0
| return createAndMarkTempFile(prefix, suffix, null);
|
|
835 |
| } |
|
836 |
| |
|
837 |
| |
|
838 |
| |
|
839 |
| |
|
840 |
| |
|
841 |
| |
|
842 |
| |
|
843 |
0
| public static File createAndMarkTempFile(String prefix, String suffix, File location) throws IOException {
|
|
844 |
0
| File result = File.createTempFile(prefix, suffix, location);
|
|
845 |
0
| attemptDeleteOnExit(result);
|
|
846 |
0
| return result;
|
|
847 |
| } |
|
848 |
| |
|
849 |
| |
|
850 |
| |
|
851 |
| |
|
852 |
| |
|
853 |
| |
|
854 |
| |
|
855 |
| |
|
856 |
| |
|
857 |
0
| public static File createAndMarkTempDirectory(String prefix, String suffix) throws IOException {
|
|
858 |
0
| return createAndMarkTempDirectory(prefix, suffix, null);
|
|
859 |
| } |
|
860 |
| |
|
861 |
| |
|
862 |
| |
|
863 |
| |
|
864 |
| |
|
865 |
| |
|
866 |
| |
|
867 |
| |
|
868 |
| |
|
869 |
0
| public static File createAndMarkTempDirectory(String prefix, String suffix, File location) throws IOException {
|
|
870 |
0
| File result = File.createTempFile(prefix, suffix, location);
|
|
871 |
0
| boolean success = result.delete();
|
|
872 |
0
| success = success && result.mkdir();
|
|
873 |
0
| if (!success) { throw new IOException("Attempt to create directory failed"); }
|
|
874 |
0
| attemptDeleteOnExit(result);
|
|
875 |
0
| return result;
|
|
876 |
| } |
|
877 |
| |
|
878 |
| |
|
879 |
| |
|
880 |
| |
|
881 |
| |
|
882 |
12
| public static SizedIterable<File> parsePath(String path) {
|
|
883 |
12
| String[] filenames = path.split(TextUtil.regexEscape(File.pathSeparator));
|
|
884 |
12
| return IterUtil.mapSnapshot(IterUtil.asIterable(filenames), FILE_FACTORY);
|
|
885 |
| } |
|
886 |
| |
|
887 |
| |
|
888 |
| |
|
889 |
| |
|
890 |
11
| public static String pathToString(Iterable<? extends File> path) {
|
|
891 |
11
| return IterUtil.toString(path, "", File.pathSeparator, "");
|
|
892 |
| } |
|
893 |
| |
|
894 |
| |
|
895 |
| |
|
896 |
| |
|
897 |
| |
|
898 |
| |
|
899 |
| |
|
900 |
| |
|
901 |
| |
|
902 |
| |
|
903 |
0
| public static int copyReader(Reader source, Writer dest) throws IOException {
|
|
904 |
0
| return WrappedDirectReader.makeDirect(source).readAll(dest);
|
|
905 |
| } |
|
906 |
| |
|
907 |
| |
|
908 |
| |
|
909 |
| |
|
910 |
| |
|
911 |
| |
|
912 |
| |
|
913 |
| |
|
914 |
0
| public static int copyReader(Reader source, Writer dest, char[] buffer) throws IOException {
|
|
915 |
0
| return WrappedDirectReader.makeDirect(source).readAll(dest, buffer);
|
|
916 |
| } |
|
917 |
| |
|
918 |
| |
|
919 |
| |
|
920 |
| |
|
921 |
| |
|
922 |
| |
|
923 |
| |
|
924 |
0
| public static int writeFromReader(Reader source, Writer dest, int chars) throws IOException {
|
|
925 |
0
| return WrappedDirectReader.makeDirect(source).read(dest, chars);
|
|
926 |
| } |
|
927 |
| |
|
928 |
| |
|
929 |
| |
|
930 |
| |
|
931 |
| |
|
932 |
| |
|
933 |
| |
|
934 |
0
| public static int writeFromReader(Reader source, Writer dest, int chars, char[] buffer) throws IOException {
|
|
935 |
0
| return WrappedDirectReader.makeDirect(source).read(dest, chars, buffer);
|
|
936 |
| } |
|
937 |
| |
|
938 |
| |
|
939 |
| |
|
940 |
| |
|
941 |
| |
|
942 |
| |
|
943 |
| |
|
944 |
| |
|
945 |
45
| public static int copyInputStream(InputStream source, OutputStream dest) throws IOException {
|
|
946 |
45
| return WrappedDirectInputStream.makeDirect(source).readAll(dest);
|
|
947 |
| } |
|
948 |
| |
|
949 |
| |
|
950 |
| |
|
951 |
| |
|
952 |
| |
|
953 |
| |
|
954 |
| |
|
955 |
| |
|
956 |
0
| public static int copyInputStream(InputStream source, OutputStream dest, byte[] buffer) throws IOException {
|
|
957 |
0
| return WrappedDirectInputStream.makeDirect(source).readAll(dest, buffer);
|
|
958 |
| } |
|
959 |
| |
|
960 |
| |
|
961 |
| |
|
962 |
| |
|
963 |
| |
|
964 |
| |
|
965 |
| |
|
966 |
0
| public static int writeFromInputStream(InputStream source, OutputStream dest, int bytes) throws IOException {
|
|
967 |
0
| return WrappedDirectInputStream.makeDirect(source).read(dest, bytes);
|
|
968 |
| } |
|
969 |
| |
|
970 |
| |
|
971 |
| |
|
972 |
| |
|
973 |
| |
|
974 |
| |
|
975 |
| |
|
976 |
0
| public static int writeFromInputStream(InputStream source, OutputStream dest, int bytes,
|
|
977 |
| byte[] buffer) throws IOException { |
|
978 |
0
| return WrappedDirectInputStream.makeDirect(source).read(dest, bytes, buffer);
|
|
979 |
| } |
|
980 |
| |
|
981 |
| |
|
982 |
| |
|
983 |
| |
|
984 |
| |
|
985 |
| |
|
986 |
| |
|
987 |
| |
|
988 |
| |
|
989 |
| |
|
990 |
0
| protected static int doCopyReader(Reader r, Writer w, char[] buffer) throws IOException {
|
|
991 |
0
| if (buffer.length == 0) { throw new IllegalArgumentException(); }
|
|
992 |
0
| int charsRead = r.read(buffer);
|
|
993 |
0
| if (charsRead == -1) { return -1; }
|
|
994 |
| else { |
|
995 |
0
| int totalCharsRead = 0;
|
|
996 |
0
| do {
|
|
997 |
0
| totalCharsRead += charsRead;
|
|
998 |
0
| if (totalCharsRead < 0) { totalCharsRead = Integer.MAX_VALUE; }
|
|
999 |
0
| w.write(buffer, 0, charsRead);
|
|
1000 |
0
| charsRead = r.read(buffer);
|
|
1001 |
0
| } while (charsRead != -1);
|
|
1002 |
0
| return totalCharsRead;
|
|
1003 |
| } |
|
1004 |
| } |
|
1005 |
| |
|
1006 |
| |
|
1007 |
| |
|
1008 |
| |
|
1009 |
| |
|
1010 |
| |
|
1011 |
| |
|
1012 |
| |
|
1013 |
| |
|
1014 |
| |
|
1015 |
50
| protected static int doCopyInputStream(InputStream in, OutputStream out, byte[] buffer) throws IOException {
|
|
1016 |
0
| if (buffer.length == 0) { throw new IllegalArgumentException(); }
|
|
1017 |
50
| int charsRead = in.read(buffer);
|
|
1018 |
0
| if (charsRead == -1) { return -1; }
|
|
1019 |
| else { |
|
1020 |
50
| int totalCharsRead = 0;
|
|
1021 |
50
| do {
|
|
1022 |
82
| totalCharsRead += charsRead;
|
|
1023 |
0
| if (totalCharsRead < 0) { totalCharsRead = Integer.MAX_VALUE; }
|
|
1024 |
82
| out.write(buffer, 0, charsRead);
|
|
1025 |
82
| charsRead = in.read(buffer);
|
|
1026 |
82
| } while (charsRead != -1);
|
|
1027 |
50
| return totalCharsRead;
|
|
1028 |
| } |
|
1029 |
| } |
|
1030 |
| |
|
1031 |
| |
|
1032 |
| |
|
1033 |
| |
|
1034 |
| |
|
1035 |
| |
|
1036 |
| |
|
1037 |
| |
|
1038 |
| |
|
1039 |
0
| protected static int doWriteFromReader(Reader r, Writer w, int chars, char[] buffer) throws IOException {
|
|
1040 |
0
| if (buffer.length == 0 && chars > 0) { throw new IllegalArgumentException(); }
|
|
1041 |
0
| int charsRead = r.read(buffer, 0, (chars < buffer.length) ? chars : buffer.length);
|
|
1042 |
0
| if (charsRead == -1) { return -1; }
|
|
1043 |
| else { |
|
1044 |
0
| int totalCharsRead = 0;
|
|
1045 |
0
| while (chars > 0 && charsRead > 0) {
|
|
1046 |
0
| totalCharsRead += charsRead;
|
|
1047 |
0
| chars -= charsRead;
|
|
1048 |
0
| w.write(buffer, 0, charsRead);
|
|
1049 |
0
| if (chars > 0) { charsRead = r.read(buffer, 0, (chars < buffer.length) ? chars : buffer.length); }
|
|
1050 |
| } |
|
1051 |
0
| return totalCharsRead;
|
|
1052 |
| } |
|
1053 |
| } |
|
1054 |
| |
|
1055 |
| |
|
1056 |
| |
|
1057 |
| |
|
1058 |
| |
|
1059 |
| |
|
1060 |
| |
|
1061 |
| |
|
1062 |
| |
|
1063 |
0
| protected static int doWriteFromInputStream(InputStream in, OutputStream out, int bytes,
|
|
1064 |
| byte[] buffer) throws IOException { |
|
1065 |
0
| if (buffer.length == 0 && bytes > 0) { throw new IllegalArgumentException(); }
|
|
1066 |
0
| int bytesRead = in.read(buffer, 0, (bytes < buffer.length) ? bytes : buffer.length);
|
|
1067 |
0
| if (bytesRead == -1) { return -1; }
|
|
1068 |
| else { |
|
1069 |
0
| int totalBytesRead = 0;
|
|
1070 |
0
| while (bytes > 0 && bytesRead > 0) {
|
|
1071 |
0
| totalBytesRead += bytesRead;
|
|
1072 |
0
| bytes -= bytesRead;
|
|
1073 |
0
| out.write(buffer, 0, bytesRead);
|
|
1074 |
0
| if (bytes > 0) { bytesRead = in.read(buffer, 0, (bytes < buffer.length) ? bytes : buffer.length); }
|
|
1075 |
| } |
|
1076 |
0
| return totalBytesRead;
|
|
1077 |
| } |
|
1078 |
| } |
|
1079 |
| |
|
1080 |
| |
|
1081 |
| |
|
1082 |
| |
|
1083 |
| |
|
1084 |
45
| public static byte[] toByteArray(InputStream stream) throws IOException {
|
|
1085 |
45
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1086 |
45
| try {
|
|
1087 |
45
| copyInputStream(stream, out);
|
|
1088 |
45
| return out.toByteArray();
|
|
1089 |
| } |
|
1090 |
45
| finally { out.close(); }
|
|
1091 |
| } |
|
1092 |
| |
|
1093 |
| |
|
1094 |
| |
|
1095 |
| |
|
1096 |
| |
|
1097 |
| |
|
1098 |
0
| public static StringBuffer toStringBuffer(Reader r) throws IOException {
|
|
1099 |
0
| StringWriter out = new StringWriter();
|
|
1100 |
0
| try {
|
|
1101 |
0
| copyReader(r, out);
|
|
1102 |
0
| return out.getBuffer();
|
|
1103 |
| } |
|
1104 |
0
| finally { out.close(); }
|
|
1105 |
| } |
|
1106 |
| |
|
1107 |
| |
|
1108 |
| |
|
1109 |
| |
|
1110 |
| |
|
1111 |
0
| public static String toString(Reader r) throws IOException {
|
|
1112 |
0
| return toStringBuffer(r).toString();
|
|
1113 |
| } |
|
1114 |
| |
|
1115 |
| |
|
1116 |
| |
|
1117 |
| |
|
1118 |
| |
|
1119 |
| |
|
1120 |
| |
|
1121 |
| |
|
1122 |
0
| public static Iterator<String> readLines(Reader r) throws IOException {
|
|
1123 |
0
| final BufferedReader br = asBuffered(r);
|
|
1124 |
0
| final String firstLine = br.readLine();
|
|
1125 |
0
| if (firstLine == null) { br.close(); }
|
|
1126 |
0
| return new ReadOnlyIterator<String>() {
|
|
1127 |
| String lookahead = firstLine; |
|
1128 |
0
| public boolean hasNext() { return lookahead != null; }
|
|
1129 |
0
| public String next() {
|
|
1130 |
0
| if (lookahead == null) { throw new NoSuchElementException(); }
|
|
1131 |
0
| try {
|
|
1132 |
0
| String result = lookahead;
|
|
1133 |
0
| lookahead = br.readLine();
|
|
1134 |
0
| if (lookahead == null) { br.close(); }
|
|
1135 |
0
| return result;
|
|
1136 |
| } |
|
1137 |
0
| catch (IOException e) { throw new WrappedException(e); }
|
|
1138 |
| } |
|
1139 |
| }; |
|
1140 |
| } |
|
1141 |
| |
|
1142 |
| |
|
1143 |
| |
|
1144 |
| |
|
1145 |
| |
|
1146 |
0
| public static int adler32Hash(InputStream stream) throws IOException {
|
|
1147 |
0
| ChecksumOutputStream out = ChecksumOutputStream.makeAdler32();
|
|
1148 |
0
| try {
|
|
1149 |
0
| copyInputStream(stream, out);
|
|
1150 |
0
| return (int) out.getValue();
|
|
1151 |
| } |
|
1152 |
0
| finally { out.close(); }
|
|
1153 |
| } |
|
1154 |
| |
|
1155 |
| |
|
1156 |
| |
|
1157 |
| |
|
1158 |
| |
|
1159 |
0
| public static int crc32Hash(InputStream stream) throws IOException {
|
|
1160 |
0
| ChecksumOutputStream out = ChecksumOutputStream.makeCRC32();
|
|
1161 |
0
| try {
|
|
1162 |
0
| copyInputStream(stream, out);
|
|
1163 |
0
| return (int) out.getValue();
|
|
1164 |
| } |
|
1165 |
0
| finally { out.close(); }
|
|
1166 |
| } |
|
1167 |
| |
|
1168 |
| |
|
1169 |
| |
|
1170 |
| |
|
1171 |
| |
|
1172 |
0
| public static byte[] md5Hash(InputStream stream) throws IOException {
|
|
1173 |
0
| MessageDigestOutputStream out = MessageDigestOutputStream.makeMD5();
|
|
1174 |
0
| try {
|
|
1175 |
0
| copyInputStream(stream, out);
|
|
1176 |
0
| return out.digest();
|
|
1177 |
| } |
|
1178 |
0
| finally { out.close(); }
|
|
1179 |
| } |
|
1180 |
| |
|
1181 |
| |
|
1182 |
| |
|
1183 |
| |
|
1184 |
| |
|
1185 |
0
| public static byte[] sha1Hash(InputStream stream) throws IOException {
|
|
1186 |
0
| MessageDigestOutputStream out = MessageDigestOutputStream.makeSHA1();
|
|
1187 |
0
| try {
|
|
1188 |
0
| copyInputStream(stream, out);
|
|
1189 |
0
| return out.digest();
|
|
1190 |
| } |
|
1191 |
0
| finally { out.close(); }
|
|
1192 |
| } |
|
1193 |
| |
|
1194 |
| |
|
1195 |
| |
|
1196 |
| |
|
1197 |
| |
|
1198 |
0
| public static byte[] sha256Hash(InputStream stream) throws IOException {
|
|
1199 |
0
| MessageDigestOutputStream out = MessageDigestOutputStream.makeSHA256();
|
|
1200 |
0
| try {
|
|
1201 |
0
| copyInputStream(stream, out);
|
|
1202 |
0
| return out.digest();
|
|
1203 |
| } |
|
1204 |
0
| finally { out.close(); }
|
|
1205 |
| } |
|
1206 |
| |
|
1207 |
| |
|
1208 |
0
| public static BufferedReader asBuffered(Reader r) {
|
|
1209 |
0
| if (r instanceof BufferedReader) { return (BufferedReader) r; }
|
|
1210 |
0
| else { return new BufferedReader(r); }
|
|
1211 |
| } |
|
1212 |
| |
|
1213 |
| |
|
1214 |
0
| public static BufferedWriter asBuffered(Writer w) {
|
|
1215 |
0
| if (w instanceof BufferedWriter) { return (BufferedWriter) w; }
|
|
1216 |
0
| else { return new BufferedWriter(w); }
|
|
1217 |
| } |
|
1218 |
| |
|
1219 |
| |
|
1220 |
| |
|
1221 |
| |
|
1222 |
| |
|
1223 |
6
| public static BufferedInputStream asBuffered(InputStream in) {
|
|
1224 |
6
| if (in instanceof BufferedInputStream) { return (BufferedInputStream) in; }
|
|
1225 |
0
| else { return new BufferedInputStream(in); }
|
|
1226 |
| } |
|
1227 |
| |
|
1228 |
| |
|
1229 |
| |
|
1230 |
| |
|
1231 |
| |
|
1232 |
0
| public static BufferedOutputStream asBuffered(OutputStream out) {
|
|
1233 |
0
| if (out instanceof BufferedOutputStream) { return (BufferedOutputStream) out; }
|
|
1234 |
0
| else { return new BufferedOutputStream(out); }
|
|
1235 |
| } |
|
1236 |
| |
|
1237 |
| |
|
1238 |
| private static final Thunk<List<Closeable>> TO_CLOSE = LazyThunk.make(new Thunk<List<Closeable>>() { |
|
1239 |
1
| public List<Closeable> value() {
|
|
1240 |
| |
|
1241 |
1
| Runtime.getRuntime().addShutdownHook(new Thread() {
|
|
1242 |
1
| public void run() {
|
|
1243 |
5
| for (Closeable c : TO_CLOSE.value()) { attemptClose(c); }
|
|
1244 |
| } |
|
1245 |
| }); |
|
1246 |
1
| return new LinkedList<Closeable>();
|
|
1247 |
| } |
|
1248 |
| }); |
|
1249 |
| |
|
1250 |
| |
|
1251 |
| |
|
1252 |
| |
|
1253 |
| |
|
1254 |
7
| public static void closeOnExit(Closeable c) {
|
|
1255 |
7
| TO_CLOSE.value().add(c);
|
|
1256 |
| } |
|
1257 |
| |
|
1258 |
| |
|
1259 |
13
| public static void attemptClose(Closeable c) {
|
|
1260 |
13
| try { c.close(); }
|
|
1261 |
| catch (IOException e) { } |
|
1262 |
| } |
|
1263 |
| |
|
1264 |
| |
|
1265 |
42
| public static FilePredicate asFilePredicate(Predicate<? super File> p) {
|
|
1266 |
42
| return new PredicateFilePredicate(p);
|
|
1267 |
| } |
|
1268 |
| |
|
1269 |
| private static final class PredicateFilePredicate implements FilePredicate, Serializable { |
|
1270 |
| private final Predicate<? super File> _p; |
|
1271 |
42
| public PredicateFilePredicate(Predicate<? super File> p) { _p = p; }
|
|
1272 |
0
| public boolean accept(File f) { return _p.contains(f); }
|
|
1273 |
0
| public boolean contains(File f) { return _p.contains(f); }
|
|
1274 |
| } |
|
1275 |
| |
|
1276 |
| |
|
1277 |
| |
|
1278 |
| |
|
1279 |
| |
|
1280 |
0
| public static FilePredicate asFilePredicate(FileFilter filter) {
|
|
1281 |
0
| return new FileFilterFilePredicate(filter);
|
|
1282 |
| } |
|
1283 |
| |
|
1284 |
| private static final class FileFilterFilePredicate implements FilePredicate, Serializable { |
|
1285 |
| private final FileFilter _filter; |
|
1286 |
0
| public FileFilterFilePredicate(FileFilter filter) { _filter = filter; }
|
|
1287 |
0
| public boolean accept(File f) { return _filter.accept(f); }
|
|
1288 |
0
| public boolean contains(File f) { return _filter.accept(f); }
|
|
1289 |
| } |
|
1290 |
| |
|
1291 |
| |
|
1292 |
| |
|
1293 |
| |
|
1294 |
| |
|
1295 |
0
| public static FilePredicate regexFilePredicate(String regex) {
|
|
1296 |
0
| return new RegexFilePredicate(regex);
|
|
1297 |
| } |
|
1298 |
| |
|
1299 |
| |
|
1300 |
| |
|
1301 |
| |
|
1302 |
| |
|
1303 |
0
| public static FilePredicate regexFilePredicate(Pattern regex) {
|
|
1304 |
0
| return new RegexFilePredicate(regex);
|
|
1305 |
| } |
|
1306 |
| |
|
1307 |
| private static final class RegexFilePredicate implements FilePredicate, Serializable { |
|
1308 |
| private final Pattern _regex; |
|
1309 |
0
| public RegexFilePredicate(String regex) { _regex = Pattern.compile(regex); }
|
|
1310 |
0
| public RegexFilePredicate(Pattern regex) { _regex = regex; }
|
|
1311 |
0
| public boolean accept(File f) { return _regex.matcher(f.getName()).matches(); }
|
|
1312 |
0
| public boolean contains(File f) { return _regex.matcher(f.getName()).matches(); }
|
|
1313 |
| } |
|
1314 |
| |
|
1315 |
| |
|
1316 |
| |
|
1317 |
| |
|
1318 |
| |
|
1319 |
0
| public static FilePredicate regexCanonicalCaseFilePredicate(String regex) {
|
|
1320 |
0
| return new RegexCanonicalCaseFilePredicate(regex);
|
|
1321 |
| } |
|
1322 |
| |
|
1323 |
| private static final class RegexCanonicalCaseFilePredicate implements FilePredicate, Serializable { |
|
1324 |
| private final Pattern _regex; |
|
1325 |
1
| public RegexCanonicalCaseFilePredicate(String regex) { _regex = Pattern.compile(regex); }
|
|
1326 |
0
| public RegexCanonicalCaseFilePredicate(Pattern regex) { _regex = regex; }
|
|
1327 |
7
| public boolean accept(File f) { return _regex.matcher(canonicalCase(f).getName()).matches(); }
|
|
1328 |
7
| public boolean contains(File f) { return _regex.matcher(canonicalCase(f).getName()).matches(); }
|
|
1329 |
| } |
|
1330 |
| |
|
1331 |
| |
|
1332 |
| |
|
1333 |
| |
|
1334 |
| |
|
1335 |
| |
|
1336 |
1
| public static FilePredicate extensionFilePredicate(String extension) {
|
|
1337 |
1
| return new RegexCanonicalCaseFilePredicate(".*\\." + canonicalCase(new File(extension)).getName());
|
|
1338 |
| } |
|
1339 |
| |
|
1340 |
| |
|
1341 |
| |
|
1342 |
| |
|
1343 |
| |
|
1344 |
0
| public static FilePredicate sameNameFilePredicate(String name) {
|
|
1345 |
0
| return new SamePathFilePredicate(new File(name));
|
|
1346 |
| } |
|
1347 |
| |
|
1348 |
| |
|
1349 |
| |
|
1350 |
| |
|
1351 |
| |
|
1352 |
| |
|
1353 |
| |
|
1354 |
0
| public static FilePredicate samePathFilePredicate(File path) {
|
|
1355 |
0
| return new SamePathFilePredicate(path);
|
|
1356 |
| } |
|
1357 |
| |
|
1358 |
| private static final class SamePathFilePredicate implements FilePredicate, Serializable { |
|
1359 |
| private final File _f; |
|
1360 |
0
| public SamePathFilePredicate(File f) { _f = canonicalCase(f); }
|
|
1361 |
0
| public boolean accept(File f) {
|
|
1362 |
0
| File candidate = canonicalCase(attemptAbsoluteFile(f));
|
|
1363 |
0
| for (File compareTo = _f; compareTo != null; compareTo = compareTo.getParentFile()) {
|
|
1364 |
0
| if (candidate == null || !compareTo.getName().equals(candidate.getName())) {
|
|
1365 |
0
| return false;
|
|
1366 |
| } |
|
1367 |
0
| candidate = candidate.getParentFile();
|
|
1368 |
| } |
|
1369 |
0
| return true;
|
|
1370 |
| } |
|
1371 |
0
| public boolean contains(File f) { return accept(f); }
|
|
1372 |
| } |
|
1373 |
| |
|
1374 |
| |
|
1375 |
| |
|
1376 |
| |
|
1377 |
| |
|
1378 |
| |
|
1379 |
| |
|
1380 |
| |
|
1381 |
0
| public static FilePredicate sameAttributesFilePredicate(File f) throws FileNotFoundException {
|
|
1382 |
0
| return new SameAttributesFilePredicate(f);
|
|
1383 |
| } |
|
1384 |
| |
|
1385 |
| private static final class SameAttributesFilePredicate implements FilePredicate, Serializable { |
|
1386 |
| private final long _lastModified; |
|
1387 |
| private final long _length; |
|
1388 |
| private final boolean _canRead; |
|
1389 |
| private final boolean _canWrite; |
|
1390 |
| |
|
1391 |
0
| public SameAttributesFilePredicate(File f) throws FileNotFoundException {
|
|
1392 |
0
| try {
|
|
1393 |
0
| if (!f.isFile()) { throw new FileNotFoundException(f + " is not a valid file"); }
|
|
1394 |
0
| _lastModified = f.lastModified();
|
|
1395 |
0
| if (_lastModified == 0l) {
|
|
1396 |
0
| throw new FileNotFoundException("Can't get valid modification date for " + f);
|
|
1397 |
| } |
|
1398 |
0
| _length = f.length();
|
|
1399 |
0
| _canRead = f.canRead();
|
|
1400 |
0
| _canWrite = f.canWrite();
|
|
1401 |
| } |
|
1402 |
0
| catch (SecurityException e) { throw new FileNotFoundException(e.getMessage()); }
|
|
1403 |
| } |
|
1404 |
| |
|
1405 |
0
| public boolean accept(File f) {
|
|
1406 |
0
| try {
|
|
1407 |
0
| return f.isFile() && f.lastModified() == _lastModified && f.length() == _length &&
|
|
1408 |
| f.canRead() == _canRead && f.canWrite() == _canWrite; |
|
1409 |
| } |
|
1410 |
0
| catch (SecurityException e) { return false; }
|
|
1411 |
| } |
|
1412 |
| |
|
1413 |
0
| public boolean contains(File f) { return accept(f); }
|
|
1414 |
| } |
|
1415 |
| |
|
1416 |
| |
|
1417 |
| |
|
1418 |
| |
|
1419 |
| |
|
1420 |
| |
|
1421 |
| |
|
1422 |
0
| public static FilePredicate sameContentsFilePredicate(File f) throws IOException {
|
|
1423 |
0
| return new SameContentsFilePredicate(f);
|
|
1424 |
| } |
|
1425 |
| |
|
1426 |
| private static final class SameContentsFilePredicate implements FilePredicate, Serializable { |
|
1427 |
| private final long _length; |
|
1428 |
| private final int _hash; |
|
1429 |
0
| public SameContentsFilePredicate(File f) throws IOException {
|
|
1430 |
| |
|
1431 |
| |
|
1432 |
0
| _length = attemptLength(f);
|
|
1433 |
0
| _hash = crc32Hash(f);
|
|
1434 |
| } |
|
1435 |
0
| public boolean accept(File f) {
|
|
1436 |
0
| long fLength = attemptLength(f);
|
|
1437 |
0
| if (fLength > 0l && _length > 0l && fLength != _length) { return false; }
|
|
1438 |
0
| try { return _hash == crc32Hash(f); }
|
|
1439 |
0
| catch (IOException e) { return false; }
|
|
1440 |
| } |
|
1441 |
0
| public boolean contains(File f) { return accept(f); }
|
|
1442 |
| } |
|
1443 |
| |
|
1444 |
| |
|
1445 |
| public static final FilePredicate IS_FILE = new IsFileFilePredicate(); |
|
1446 |
| |
|
1447 |
| private static final class IsFileFilePredicate implements FilePredicate, Serializable { |
|
1448 |
0
| public boolean accept(File f) { return attemptIsFile(f); }
|
|
1449 |
0
| public boolean contains(File f) { return attemptIsFile(f); }
|
|
1450 |
| } |
|
1451 |
| |
|
1452 |
| |
|
1453 |
| public static final FilePredicate IS_DIRECTORY = new IsDirectoryFilePredicate(); |
|
1454 |
| |
|
1455 |
| private static final class IsDirectoryFilePredicate implements FilePredicate, Serializable { |
|
1456 |
0
| public boolean accept(File f) { return attemptIsDirectory(f); }
|
|
1457 |
0
| public boolean contains(File f) { return attemptIsDirectory(f); }
|
|
1458 |
| } |
|
1459 |
| |
|
1460 |
| |
|
1461 |
| public static final FilePredicate ALWAYS_ACCEPT = asFilePredicate(LambdaUtil.TRUE); |
|
1462 |
| |
|
1463 |
| |
|
1464 |
| public static final FilePredicate ALWAYS_REJECT = asFilePredicate(LambdaUtil.FALSE); |
|
1465 |
| |
|
1466 |
| |
|
1467 |
0
| public static FilePredicate and(FileFilter... filters) {
|
|
1468 |
0
| return new AndFilePredicate(IterUtil.asIterable(filters));
|
|
1469 |
| } |
|
1470 |
| |
|
1471 |
| |
|
1472 |
0
| public static FilePredicate and(Iterable<? extends FileFilter> filters) {
|
|
1473 |
0
| return new AndFilePredicate(filters);
|
|
1474 |
| } |
|
1475 |
| |
|
1476 |
| private static final class AndFilePredicate implements FilePredicate, Serializable { |
|
1477 |
| private final Iterable<? extends FileFilter> _filters; |
|
1478 |
0
| public AndFilePredicate(Iterable<? extends FileFilter> filters) { _filters = filters; }
|
|
1479 |
0
| public boolean accept(File f) {
|
|
1480 |
0
| for (FileFilter filter : _filters) {
|
|
1481 |
0
| if (!filter.accept(f)) { return false; }
|
|
1482 |
| } |
|
1483 |
0
| return true;
|
|
1484 |
| } |
|
1485 |
0
| public boolean contains(File f) { return accept(f); }
|
|
1486 |
| } |
|
1487 |
| |
|
1488 |
| |
|
1489 |
0
| public static FilePredicate or(FileFilter... filters) {
|
|
1490 |
0
| return new OrFilePredicate(IterUtil.asIterable(filters));
|
|
1491 |
| } |
|
1492 |
| |
|
1493 |
| |
|
1494 |
0
| public static FilePredicate or(Iterable<? extends FileFilter> filters) {
|
|
1495 |
0
| return new OrFilePredicate(filters);
|
|
1496 |
| } |
|
1497 |
| |
|
1498 |
| private static final class OrFilePredicate implements FilePredicate, Serializable { |
|
1499 |
| private final Iterable<? extends FileFilter> _filters; |
|
1500 |
0
| public OrFilePredicate(Iterable<? extends FileFilter> filters) { _filters = filters; }
|
|
1501 |
0
| public boolean accept(File f) {
|
|
1502 |
0
| for (FileFilter filter : _filters) {
|
|
1503 |
0
| if (filter.accept(f)) { return true; }
|
|
1504 |
| } |
|
1505 |
0
| return false;
|
|
1506 |
| } |
|
1507 |
0
| public boolean contains(File f) { return accept(f); }
|
|
1508 |
| } |
|
1509 |
| |
|
1510 |
| |
|
1511 |
0
| public static FilePredicate negate(FileFilter filter) {
|
|
1512 |
0
| return new NegationFilePredicate(filter);
|
|
1513 |
| } |
|
1514 |
| |
|
1515 |
| private static final class NegationFilePredicate implements FilePredicate, Serializable { |
|
1516 |
| private final FileFilter _filter; |
|
1517 |
0
| public NegationFilePredicate(FileFilter filter) { _filter = filter; }
|
|
1518 |
0
| public boolean accept(File f) { return !_filter.accept(f); }
|
|
1519 |
0
| public boolean contains(File f) { return !_filter.accept(f); }
|
|
1520 |
| } |
|
1521 |
| |
|
1522 |
| |
|
1523 |
| |
|
1524 |
| |
|
1525 |
| |
|
1526 |
| |
|
1527 |
| |
|
1528 |
| |
|
1529 |
0
| public static FilePredicate fileKey(File f) throws IOException {
|
|
1530 |
0
| return and(samePathFilePredicate(attemptAbsoluteFile(f)),
|
|
1531 |
| sameAttributesFilePredicate(f), |
|
1532 |
| sameContentsFilePredicate(f)); |
|
1533 |
| } |
|
1534 |
| |
|
1535 |
| |
|
1536 |
| private static final LinkedList<PrintStream> SYSTEM_OUT_STACK = new LinkedList<PrintStream>(); |
|
1537 |
| private static final LinkedList<PrintStream> SYSTEM_ERR_STACK = new LinkedList<PrintStream>(); |
|
1538 |
| private static final LinkedList<InputStream> SYSTEM_IN_STACK = new LinkedList<InputStream>(); |
|
1539 |
| |
|
1540 |
| |
|
1541 |
| |
|
1542 |
| |
|
1543 |
| |
|
1544 |
8
| public static void replaceSystemOut(OutputStream substitute) {
|
|
1545 |
8
| SYSTEM_OUT_STACK.addLast(System.out);
|
|
1546 |
0
| if (substitute instanceof PrintStream) { System.setOut((PrintStream) substitute); }
|
|
1547 |
8
| else { System.setOut(new PrintStream(substitute)); }
|
|
1548 |
| } |
|
1549 |
| |
|
1550 |
| |
|
1551 |
| |
|
1552 |
| |
|
1553 |
| |
|
1554 |
8
| public static void ignoreSystemOut() {
|
|
1555 |
8
| replaceSystemOut(VoidOutputStream.INSTANCE);
|
|
1556 |
| } |
|
1557 |
| |
|
1558 |
| |
|
1559 |
| |
|
1560 |
| |
|
1561 |
| |
|
1562 |
| |
|
1563 |
| |
|
1564 |
0
| public static void revertSystemOut() {
|
|
1565 |
0
| if (SYSTEM_OUT_STACK.isEmpty()) { error.logStack("Unbalanced call to revertSystemOut"); }
|
|
1566 |
0
| else { System.setOut(SYSTEM_OUT_STACK.removeLast()); }
|
|
1567 |
| } |
|
1568 |
| |
|
1569 |
| |
|
1570 |
| |
|
1571 |
| |
|
1572 |
| |
|
1573 |
8
| public static void replaceSystemErr(OutputStream substitute) {
|
|
1574 |
8
| SYSTEM_ERR_STACK.addLast(System.err);
|
|
1575 |
0
| if (substitute instanceof PrintStream) { System.setErr((PrintStream) substitute); }
|
|
1576 |
8
| else { System.setErr(new PrintStream(substitute)); }
|
|
1577 |
| } |
|
1578 |
| |
|
1579 |
| |
|
1580 |
| |
|
1581 |
| |
|
1582 |
| |
|
1583 |
8
| public static void ignoreSystemErr() {
|
|
1584 |
8
| replaceSystemErr(VoidOutputStream.INSTANCE);
|
|
1585 |
| } |
|
1586 |
| |
|
1587 |
| |
|
1588 |
| |
|
1589 |
| |
|
1590 |
| |
|
1591 |
| |
|
1592 |
| |
|
1593 |
0
| public static void revertSystemErr() {
|
|
1594 |
0
| if (SYSTEM_ERR_STACK.isEmpty()) { error.logStack("Unbalanced call to revertSystemErr"); }
|
|
1595 |
0
| else { System.setErr(SYSTEM_ERR_STACK.removeLast()); }
|
|
1596 |
| } |
|
1597 |
| |
|
1598 |
| |
|
1599 |
| |
|
1600 |
| |
|
1601 |
| |
|
1602 |
0
| public static void replaceSystemIn(InputStream substitute) {
|
|
1603 |
0
| SYSTEM_IN_STACK.addLast(System.in);
|
|
1604 |
0
| System.setIn(substitute);
|
|
1605 |
| } |
|
1606 |
| |
|
1607 |
| |
|
1608 |
| |
|
1609 |
| |
|
1610 |
| |
|
1611 |
| |
|
1612 |
| |
|
1613 |
0
| public static void revertSystemIn() {
|
|
1614 |
0
| if (SYSTEM_IN_STACK.isEmpty()) { error.logStack("Unbalanced call to revertSystemIn"); }
|
|
1615 |
0
| else { System.setIn(SYSTEM_IN_STACK.removeLast()); }
|
|
1616 |
| } |
|
1617 |
| |
|
1618 |
| |
|
1619 |
| |
|
1620 |
| |
|
1621 |
| |
|
1622 |
| |
|
1623 |
| private static final Set<Class<?>> SERIALIZABLE_CLASSES = new HashSet<Class<?>>(); |
|
1624 |
| static { |
|
1625 |
| |
|
1626 |
21
| SERIALIZABLE_CLASSES.add(String.class);
|
|
1627 |
21
| SERIALIZABLE_CLASSES.add(Boolean.class);
|
|
1628 |
21
| SERIALIZABLE_CLASSES.add(Character.class);
|
|
1629 |
21
| SERIALIZABLE_CLASSES.add(Byte.class);
|
|
1630 |
21
| SERIALIZABLE_CLASSES.add(Short.class);
|
|
1631 |
21
| SERIALIZABLE_CLASSES.add(Integer.class);
|
|
1632 |
21
| SERIALIZABLE_CLASSES.add(Long.class);
|
|
1633 |
21
| SERIALIZABLE_CLASSES.add(Float.class);
|
|
1634 |
21
| SERIALIZABLE_CLASSES.add(Double.class);
|
|
1635 |
21
| SERIALIZABLE_CLASSES.add(Date.class);
|
|
1636 |
21
| SERIALIZABLE_CLASSES.add(File.class);
|
|
1637 |
21
| SERIALIZABLE_CLASSES.add(StackTraceElement.class);
|
|
1638 |
| |
|
1639 |
| |
|
1640 |
21
| SERIALIZABLE_CLASSES.add(ThreadSnapshot.class);
|
|
1641 |
21
| SERIALIZABLE_CLASSES.add(Null.class);
|
|
1642 |
| |
|
1643 |
| |
|
1644 |
21
| SERIALIZABLE_CLASSES.add(boolean[].class);
|
|
1645 |
21
| SERIALIZABLE_CLASSES.add(char[].class);
|
|
1646 |
21
| SERIALIZABLE_CLASSES.add(byte[].class);
|
|
1647 |
21
| SERIALIZABLE_CLASSES.add(short[].class);
|
|
1648 |
21
| SERIALIZABLE_CLASSES.add(int[].class);
|
|
1649 |
21
| SERIALIZABLE_CLASSES.add(long[].class);
|
|
1650 |
21
| SERIALIZABLE_CLASSES.add(float[].class);
|
|
1651 |
21
| SERIALIZABLE_CLASSES.add(double[].class);
|
|
1652 |
| } |
|
1653 |
| |
|
1654 |
| |
|
1655 |
| |
|
1656 |
| |
|
1657 |
| |
|
1658 |
| |
|
1659 |
| |
|
1660 |
| |
|
1661 |
12
| public static Object ensureSerializable(Object obj) {
|
|
1662 |
0
| if (obj == null) { return null; }
|
|
1663 |
12
| else if (SERIALIZABLE_CLASSES.contains(obj.getClass())) { return obj; }
|
|
1664 |
0
| else if (obj instanceof Object[]) { return ensureSerializable((Object[]) obj); }
|
|
1665 |
0
| else if (obj instanceof Iterable<?>) { return ensureSerializable((Iterable<?>) obj); }
|
|
1666 |
0
| else if (obj instanceof Throwable) { return ensureSerializable((Throwable) obj); }
|
|
1667 |
0
| else if (obj instanceof Tuple) { return ensureSerializable((Tuple) obj); }
|
|
1668 |
0
| else { return obj.toString(); }
|
|
1669 |
| } |
|
1670 |
| |
|
1671 |
| |
|
1672 |
| |
|
1673 |
| |
|
1674 |
| |
|
1675 |
| |
|
1676 |
| |
|
1677 |
0
| public static Object[] ensureSerializable(Object[] arr) {
|
|
1678 |
0
| Class<?> base = ReflectUtil.arrayBaseClass(arr.getClass());
|
|
1679 |
0
| if (SERIALIZABLE_CLASSES.contains(base) && Modifier.isFinal(base.getModifiers())) {
|
|
1680 |
| |
|
1681 |
| |
|
1682 |
0
| return arr;
|
|
1683 |
| } |
|
1684 |
| else { |
|
1685 |
0
| boolean keep = true;
|
|
1686 |
0
| Object[] result = new Object[arr.length];
|
|
1687 |
0
| for (int i = 0; i < arr.length; i++) {
|
|
1688 |
0
| result[i] = ensureSerializable(arr[i]);
|
|
1689 |
0
| keep &= (result[i] == arr[i]);
|
|
1690 |
| } |
|
1691 |
0
| return keep ? arr : result;
|
|
1692 |
| } |
|
1693 |
| } |
|
1694 |
| |
|
1695 |
| |
|
1696 |
| |
|
1697 |
| |
|
1698 |
| |
|
1699 |
| |
|
1700 |
| |
|
1701 |
0
| public static Iterable<?> ensureSerializable(Iterable<?> iter) {
|
|
1702 |
0
| if (IterUtil.isInfinite(iter)) { iter = IterUtil.compose(IterUtil.truncate(iter, 8), "..."); }
|
|
1703 |
| |
|
1704 |
| |
|
1705 |
0
| boolean keep = iter.getClass().equals(ArrayList.class);
|
|
1706 |
0
| List<Object> result = new ArrayList<Object>();
|
|
1707 |
0
| for (Object elt : iter) {
|
|
1708 |
0
| Object safe = ensureSerializable(elt);
|
|
1709 |
0
| keep &= (elt == safe);
|
|
1710 |
0
| result.add(safe);
|
|
1711 |
| } |
|
1712 |
0
| return keep ? iter : result;
|
|
1713 |
| } |
|
1714 |
| |
|
1715 |
| |
|
1716 |
| |
|
1717 |
| |
|
1718 |
| |
|
1719 |
| |
|
1720 |
3
| public static Throwable ensureSerializable(Throwable t) {
|
|
1721 |
3
| Throwable safeCause = (t.getCause() == null) ? null : ensureSerializable(t.getCause());
|
|
1722 |
3
| if (t.getCause() == safeCause && isSafeThrowableClass(t.getClass())) { return t; }
|
|
1723 |
0
| else { return new SerializableException(t, safeCause); }
|
|
1724 |
| } |
|
1725 |
| |
|
1726 |
| |
|
1727 |
| |
|
1728 |
| |
|
1729 |
| |
|
1730 |
| |
|
1731 |
0
| public static Exception ensureSerializable(Exception e) {
|
|
1732 |
0
| Throwable safeCause = (e.getCause() == null) ? null : ensureSerializable(e.getCause());
|
|
1733 |
0
| if (e.getCause() == safeCause && isSafeThrowableClass(e.getClass())) { return e; }
|
|
1734 |
0
| else { return new SerializableException(e, safeCause); }
|
|
1735 |
| } |
|
1736 |
| |
|
1737 |
| |
|
1738 |
| |
|
1739 |
| |
|
1740 |
| |
|
1741 |
| |
|
1742 |
0
| public static RuntimeException ensureSerializable(RuntimeException e) {
|
|
1743 |
0
| Throwable safeCause = (e.getCause() == null) ? null : ensureSerializable(e.getCause());
|
|
1744 |
0
| if (e.getCause() == safeCause && isSafeThrowableClass(e.getClass())) { return e; }
|
|
1745 |
0
| else { return new SerializableException(e, safeCause); }
|
|
1746 |
| } |
|
1747 |
| |
|
1748 |
| |
|
1749 |
3
| private static boolean isSafeThrowableClass(Class<?> c) {
|
|
1750 |
3
| try {
|
|
1751 |
3
| if (!c.getMethod("getCause").getDeclaringClass().equals(Throwable.class)) {
|
|
1752 |
| |
|
1753 |
| |
|
1754 |
| |
|
1755 |
| |
|
1756 |
| |
|
1757 |
| |
|
1758 |
0
| return false;
|
|
1759 |
| } |
|
1760 |
| } |
|
1761 |
0
| catch (NoSuchMethodException e) { return false; }
|
|
1762 |
0
| catch (SecurityException e) { return false; }
|
|
1763 |
| |
|
1764 |
3
| Class<?> parent = c;
|
|
1765 |
3
| while (!parent.equals(Throwable.class) && parent != null) {
|
|
1766 |
6
| for (Field f : parent.getDeclaredFields()) {
|
|
1767 |
6
| Class<?> fType = f.getType();
|
|
1768 |
0
| if (!fType.isPrimitive() && !SERIALIZABLE_CLASSES.contains(f.getType())) { return false; }
|
|
1769 |
| } |
|
1770 |
6
| parent = parent.getSuperclass();
|
|
1771 |
| } |
|
1772 |
3
| return true;
|
|
1773 |
| } |
|
1774 |
| |
|
1775 |
| |
|
1776 |
| |
|
1777 |
| |
|
1778 |
| |
|
1779 |
0
| public static Tuple ensureSerializable(Tuple t) {
|
|
1780 |
0
| if (t instanceof Null) { return t; }
|
|
1781 |
0
| else if (t instanceof Wrapper<?>) { return ensureSerializable((Wrapper<?>) t); }
|
|
1782 |
0
| else if (t instanceof Pair<?,?>) { return ensureSerializable((Pair<?,?>) t); }
|
|
1783 |
0
| else if (t instanceof Triple<?,?,?>) { return ensureSerializable((Triple<?,?,?>) t); }
|
|
1784 |
0
| else if (t instanceof Quad<?,?,?,?>) { return ensureSerializable((Quad<?,?,?,?>) t); }
|
|
1785 |
0
| else if (t instanceof Quint<?,?,?,?,?>) { return ensureSerializable((Quint<?,?,?,?,?>) t); }
|
|
1786 |
0
| else if (t instanceof Sextet<?,?,?,?,?,?>) { return ensureSerializable((Sextet<?,?,?,?,?,?>) t); }
|
|
1787 |
0
| else if (t instanceof Septet<?,?,?,?,?,?,?>) { return ensureSerializable((Septet<?,?,?,?,?,?,?>) t); }
|
|
1788 |
0
| else if (t instanceof Octet<?,?,?,?,?,?,?,?>) { return ensureSerializable((Octet<?,?,?,?,?,?,?,?>) t); }
|
|
1789 |
0
| else { throw new IllegalArgumentException("Unrecognized tuple type: " + t.getClass().getName()); }
|
|
1790 |
| } |
|
1791 |
| |
|
1792 |
| |
|
1793 |
| |
|
1794 |
| |
|
1795 |
| |
|
1796 |
0
| public static Option<?> ensureSerializable(Option<?> opt) {
|
|
1797 |
0
| if (opt instanceof Null) { return opt; }
|
|
1798 |
0
| else if (opt instanceof Wrapper<?>) { return ensureSerializable((Wrapper<?>) opt); }
|
|
1799 |
0
| else { throw new IllegalArgumentException("Unrecognized option type: " + opt.getClass().getName()); }
|
|
1800 |
| } |
|
1801 |
| |
|
1802 |
| |
|
1803 |
| |
|
1804 |
| |
|
1805 |
| |
|
1806 |
0
| public static Wrapper<?> ensureSerializable(Wrapper<?> w) {
|
|
1807 |
0
| Object safeVal = ensureSerializable(w.value());
|
|
1808 |
0
| if (w.getClass().equals(Wrapper.class) && w.value() == safeVal) { return w; }
|
|
1809 |
0
| else { return Wrapper.make(safeVal); }
|
|
1810 |
| } |
|
1811 |
| |
|
1812 |
| |
|
1813 |
| |
|
1814 |
| |
|
1815 |
| |
|
1816 |
0
| public static Pair<?,?> ensureSerializable(Pair<?,?> p) {
|
|
1817 |
0
| Object safeFirst = ensureSerializable(p.first());
|
|
1818 |
0
| Object safeSecond = ensureSerializable(p.second());
|
|
1819 |
0
| if (p.getClass().equals(Pair.class) && p.first() == safeFirst && p.second() == safeSecond) { return p; }
|
|
1820 |
0
| else { return Pair.make(safeFirst, safeSecond); }
|
|
1821 |
| } |
|
1822 |
| |
|
1823 |
| |
|
1824 |
| |
|
1825 |
| |
|
1826 |
| |
|
1827 |
0
| public static Triple<?,?,?> ensureSerializable(Triple<?,?,?> t) {
|
|
1828 |
0
| Object safeFirst = ensureSerializable(t.first());
|
|
1829 |
0
| Object safeSecond = ensureSerializable(t.second());
|
|
1830 |
0
| Object safeThird = ensureSerializable(t.third());
|
|
1831 |
0
| if (t.getClass().equals(Triple.class) &&
|
|
1832 |
| t.first() == safeFirst && |
|
1833 |
| t.second() == safeSecond && |
|
1834 |
| t.third() == safeThird) { |
|
1835 |
0
| return t;
|
|
1836 |
| } |
|
1837 |
0
| else { return Triple.make(safeFirst, safeSecond, safeThird); }
|
|
1838 |
| } |
|
1839 |
| |
|
1840 |
| |
|
1841 |
| |
|
1842 |
| |
|
1843 |
| |
|
1844 |
0
| public static Quad<?,?,?,?> ensureSerializable(Quad<?,?,?,?> q) {
|
|
1845 |
0
| Object safeFirst = ensureSerializable(q.first());
|
|
1846 |
0
| Object safeSecond = ensureSerializable(q.second());
|
|
1847 |
0
| Object safeThird = ensureSerializable(q.third());
|
|
1848 |
0
| Object safeFourth = ensureSerializable(q.fourth());
|
|
1849 |
0
| if (q.getClass().equals(Quad.class) &&
|
|
1850 |
| q.first() == safeFirst && |
|
1851 |
| q.second() == safeSecond && |
|
1852 |
| q.third() == safeThird && |
|
1853 |
| q.fourth() == safeFourth) { |
|
1854 |
0
| return q;
|
|
1855 |
| } |
|
1856 |
0
| else { return Quad.make(safeFirst, safeSecond, safeThird, safeFourth); }
|
|
1857 |
| } |
|
1858 |
| |
|
1859 |
| |
|
1860 |
| |
|
1861 |
| |
|
1862 |
| |
|
1863 |
0
| public static Quint<?,?,?,?,?> ensureSerializable(Quint<?,?,?,?,?> q) {
|
|
1864 |
0
| Object safeFirst = ensureSerializable(q.first());
|
|
1865 |
0
| Object safeSecond = ensureSerializable(q.second());
|
|
1866 |
0
| Object safeThird = ensureSerializable(q.third());
|
|
1867 |
0
| Object safeFourth = ensureSerializable(q.fourth());
|
|
1868 |
0
| Object safeFifth = ensureSerializable(q.fifth());
|
|
1869 |
0
| if (q.getClass().equals(Quint.class) &&
|
|
1870 |
| q.first() == safeFirst && |
|
1871 |
| q.second() == safeSecond && |
|
1872 |
| q.third() == safeThird && |
|
1873 |
| q.fourth() == safeFourth && |
|
1874 |
| q.fifth() == safeFifth) { |
|
1875 |
0
| return q;
|
|
1876 |
| } |
|
1877 |
0
| else { return Quint.make(safeFirst, safeSecond, safeThird, safeFourth, safeFifth); }
|
|
1878 |
| } |
|
1879 |
| |
|
1880 |
| |
|
1881 |
| |
|
1882 |
| |
|
1883 |
| |
|
1884 |
0
| public static Sextet<?,?,?,?,?,?> ensureSerializable(Sextet<?,?,?,?,?,?> s) {
|
|
1885 |
0
| Object safeFirst = ensureSerializable(s.first());
|
|
1886 |
0
| Object safeSecond = ensureSerializable(s.second());
|
|
1887 |
0
| Object safeThird = ensureSerializable(s.third());
|
|
1888 |
0
| Object safeFourth = ensureSerializable(s.fourth());
|
|
1889 |
0
| Object safeFifth = ensureSerializable(s.fifth());
|
|
1890 |
0
| Object safeSixth = ensureSerializable(s.sixth());
|
|
1891 |
0
| if (s.getClass().equals(Quint.class) &&
|
|
1892 |
| s.first() == safeFirst && |
|
1893 |
| s.second() == safeSecond && |
|
1894 |
| s.third() == safeThird && |
|
1895 |
| s.fourth() == safeFourth && |
|
1896 |
| s.fifth() == safeFifth && |
|
1897 |
| s.sixth() == safeSixth) { |
|
1898 |
0
| return s;
|
|
1899 |
| } |
|
1900 |
0
| else { return Sextet.make(safeFirst, safeSecond, safeThird, safeFourth, safeFifth, safeSixth); }
|
|
1901 |
| } |
|
1902 |
| |
|
1903 |
| |
|
1904 |
| |
|
1905 |
| |
|
1906 |
| |
|
1907 |
0
| public static Septet<?,?,?,?,?,?,?> ensureSerializable(Septet<?,?,?,?,?,?,?> s) {
|
|
1908 |
0
| Object safeFirst = ensureSerializable(s.first());
|
|
1909 |
0
| Object safeSecond = ensureSerializable(s.second());
|
|
1910 |
0
| Object safeThird = ensureSerializable(s.third());
|
|
1911 |
0
| Object safeFourth = ensureSerializable(s.fourth());
|
|
1912 |
0
| Object safeFifth = ensureSerializable(s.fifth());
|
|
1913 |
0
| Object safeSixth = ensureSerializable(s.sixth());
|
|
1914 |
0
| Object safeSeventh = ensureSerializable(s.seventh());
|
|
1915 |
0
| if (s.getClass().equals(Quint.class) &&
|
|
1916 |
| s.first() == safeFirst && |
|
1917 |
| s.second() == safeSecond && |
|
1918 |
| s.third() == safeThird && |
|
1919 |
| s.fourth() == safeFourth && |
|
1920 |
| s.fifth() == safeFifth && |
|
1921 |
| s.sixth() == safeSixth && |
|
1922 |
| s.seventh() == safeSeventh) { |
|
1923 |
0
| return s;
|
|
1924 |
| } |
|
1925 |
0
| else { return Septet.make(safeFirst, safeSecond, safeThird, safeFourth, safeFifth, safeSixth, safeSeventh); }
|
|
1926 |
| } |
|
1927 |
| |
|
1928 |
| |
|
1929 |
| |
|
1930 |
| |
|
1931 |
| |
|
1932 |
0
| public static Octet<?,?,?,?,?,?,?,?> ensureSerializable(Octet<?,?,?,?,?,?,?,?> o) {
|
|
1933 |
0
| Object safeFirst = ensureSerializable(o.first());
|
|
1934 |
0
| Object safeSecond = ensureSerializable(o.second());
|
|
1935 |
0
| Object safeThird = ensureSerializable(o.third());
|
|
1936 |
0
| Object safeFourth = ensureSerializable(o.fourth());
|
|
1937 |
0
| Object safeFifth = ensureSerializable(o.fifth());
|
|
1938 |
0
| Object safeSixth = ensureSerializable(o.sixth());
|
|
1939 |
0
| Object safeSeventh = ensureSerializable(o.seventh());
|
|
1940 |
0
| Object safeEighth = ensureSerializable(o.eighth());
|
|
1941 |
0
| if (o.getClass().equals(Quint.class) &&
|
|
1942 |
| o.first() == safeFirst && |
|
1943 |
| o.second() == safeSecond && |
|
1944 |
| o.third() == safeThird && |
|
1945 |
| o.fourth() == safeFourth && |
|
1946 |
| o.fifth() == safeFifth && |
|
1947 |
| o.sixth() == safeSixth && |
|
1948 |
| o.seventh() == safeSeventh && |
|
1949 |
| o.eighth() == safeEighth) { |
|
1950 |
0
| return o;
|
|
1951 |
| } |
|
1952 |
| else { |
|
1953 |
0
| return Octet.make(safeFirst, safeSecond, safeThird, safeFourth, safeFifth, safeSixth, safeSeventh, safeEighth);
|
|
1954 |
| } |
|
1955 |
| } |
|
1956 |
| |
|
1957 |
| } |