2596 FileVisitor<? super Path> visitor) |
2596 FileVisitor<? super Path> visitor) |
2597 throws IOException |
2597 throws IOException |
2598 { |
2598 { |
2599 if (maxDepth < 0) |
2599 if (maxDepth < 0) |
2600 throw new IllegalArgumentException("'maxDepth' is negative"); |
2600 throw new IllegalArgumentException("'maxDepth' is negative"); |
2601 new FileTreeWalker(options, visitor, maxDepth).walk(start); |
2601 |
|
2602 /** |
|
2603 * Create a FileTreeWalker to walk the file tree, invoking the visitor |
|
2604 * for each event. |
|
2605 */ |
|
2606 try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) { |
|
2607 FileTreeWalker.Event ev = walker.walk(start); |
|
2608 do { |
|
2609 FileVisitResult result; |
|
2610 switch (ev.type()) { |
|
2611 case ENTRY : |
|
2612 IOException ioe = ev.ioeException(); |
|
2613 if (ioe == null) { |
|
2614 assert ev.attributes() != null; |
|
2615 result = visitor.visitFile(ev.file(), ev.attributes()); |
|
2616 } else { |
|
2617 result = visitor.visitFileFailed(ev.file(), ioe); |
|
2618 } |
|
2619 break; |
|
2620 |
|
2621 case START_DIRECTORY : |
|
2622 result = visitor.preVisitDirectory(ev.file(), ev.attributes()); |
|
2623 |
|
2624 // if SKIP_SIBLINGS and SKIP_SUBTREE is returned then |
|
2625 // there shouldn't be any more events for the current |
|
2626 // directory. |
|
2627 if (result == FileVisitResult.SKIP_SUBTREE || |
|
2628 result == FileVisitResult.SKIP_SIBLINGS) |
|
2629 walker.pop(); |
|
2630 break; |
|
2631 |
|
2632 case END_DIRECTORY : |
|
2633 result = visitor.postVisitDirectory(ev.file(), ev.ioeException()); |
|
2634 |
|
2635 // SKIP_SIBLINGS is a no-op for postVisitDirectory |
|
2636 if (result == FileVisitResult.SKIP_SIBLINGS) |
|
2637 result = FileVisitResult.CONTINUE; |
|
2638 break; |
|
2639 |
|
2640 default : |
|
2641 throw new AssertionError("Should not get here"); |
|
2642 } |
|
2643 |
|
2644 if (Objects.requireNonNull(result) != FileVisitResult.CONTINUE) { |
|
2645 if (result == FileVisitResult.TERMINATE) { |
|
2646 break; |
|
2647 } else if (result == FileVisitResult.SKIP_SIBLINGS) { |
|
2648 walker.skipRemainingSiblings(); |
|
2649 } |
|
2650 } |
|
2651 ev = walker.next(); |
|
2652 } while (ev != null); |
|
2653 } |
|
2654 |
2602 return start; |
2655 return start; |
2603 } |
2656 } |
2604 |
2657 |
2605 /** |
2658 /** |
2606 * Walks a file tree. |
2659 * Walks a file tree. |