Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/file/FileTreeIterator.java
38918 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.io.Closeable;28import java.io.IOException;29import java.io.UncheckedIOException;30import java.util.Arrays;31import java.util.Iterator;32import java.util.NoSuchElementException;33import java.util.Objects;34import java.nio.file.FileTreeWalker.Event;3536/**37* An {@code Iterator to iterate over the nodes of a file tree.38*39* <pre>{@code40* try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) {41* while (iterator.hasNext()) {42* Event ev = iterator.next();43* Path path = ev.file();44* BasicFileAttributes attrs = ev.attributes();45* }46* }47* }</pre>48*/4950class FileTreeIterator implements Iterator<Event>, Closeable {51private final FileTreeWalker walker;52private Event next;5354/**55* Creates a new iterator to walk the file tree starting at the given file.56*57* @throws IllegalArgumentException58* if {@code maxDepth} is negative59* @throws IOException60* if an I/O errors occurs opening the starting file61* @throws SecurityException62* if the security manager denies access to the starting file63* @throws NullPointerException64* if {@code start} or {@code options} is {@ocde null} or65* the options array contains a {@code null} element66*/67FileTreeIterator(Path start, int maxDepth, FileVisitOption... options)68throws IOException69{70this.walker = new FileTreeWalker(Arrays.asList(options), maxDepth);71this.next = walker.walk(start);72assert next.type() == FileTreeWalker.EventType.ENTRY ||73next.type() == FileTreeWalker.EventType.START_DIRECTORY;7475// IOException if there a problem accessing the starting file76IOException ioe = next.ioeException();77if (ioe != null)78throw ioe;79}8081private void fetchNextIfNeeded() {82if (next == null) {83FileTreeWalker.Event ev = walker.next();84while (ev != null) {85IOException ioe = ev.ioeException();86if (ioe != null)87throw new UncheckedIOException(ioe);8889// END_DIRECTORY events are ignored90if (ev.type() != FileTreeWalker.EventType.END_DIRECTORY) {91next = ev;92return;93}94ev = walker.next();95}96}97}9899@Override100public boolean hasNext() {101if (!walker.isOpen())102throw new IllegalStateException();103fetchNextIfNeeded();104return next != null;105}106107@Override108public Event next() {109if (!walker.isOpen())110throw new IllegalStateException();111fetchNextIfNeeded();112if (next == null)113throw new NoSuchElementException();114Event result = next;115next = null;116return result;117}118119@Override120public void close() {121walker.close();122}123}124125126