Path: blob/master/src/java.base/share/classes/java/nio/file/FileTreeIterator.java
41159 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.nio.file.FileTreeWalker.Event;3435/**36* An {@code Iterator} to iterate over the nodes of a file tree.37*38* <pre>{@code39* try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) {40* while (iterator.hasNext()) {41* Event ev = iterator.next();42* Path path = ev.file();43* BasicFileAttributes attrs = ev.attributes();44* }45* }46* }</pre>47*/4849class FileTreeIterator implements Iterator<Event>, Closeable {50private final FileTreeWalker walker;51private Event next;5253/**54* Creates a new iterator to walk the file tree starting at the given file.55*56* @throws IllegalArgumentException57* if {@code maxDepth} is negative58* @throws IOException59* if an I/O errors occurs opening the starting file60* @throws SecurityException61* if the security manager denies access to the starting file62* @throws NullPointerException63* if {@code start} or {@code options} is {@code null} or64* the options array contains a {@code null} element65*/66FileTreeIterator(Path start, int maxDepth, FileVisitOption... options)67throws IOException68{69this.walker = new FileTreeWalker(Arrays.asList(options), maxDepth);70this.next = walker.walk(start);71assert next.type() == FileTreeWalker.EventType.ENTRY ||72next.type() == FileTreeWalker.EventType.START_DIRECTORY;7374// IOException if there a problem accessing the starting file75IOException ioe = next.ioeException();76if (ioe != null)77throw ioe;78}7980private void fetchNextIfNeeded() {81if (next == null) {82FileTreeWalker.Event ev = walker.next();83while (ev != null) {84IOException ioe = ev.ioeException();85if (ioe != null)86throw new UncheckedIOException(ioe);8788// END_DIRECTORY events are ignored89if (ev.type() != FileTreeWalker.EventType.END_DIRECTORY) {90next = ev;91return;92}93ev = walker.next();94}95}96}9798@Override99public boolean hasNext() {100if (!walker.isOpen())101throw new IllegalStateException();102fetchNextIfNeeded();103return next != null;104}105106@Override107public Event next() {108if (!walker.isOpen())109throw new IllegalStateException();110fetchNextIfNeeded();111if (next == null)112throw new NoSuchElementException();113Event result = next;114next = null;115return result;116}117118@Override119public void close() {120walker.close();121}122}123124125