Path: blob/master/src/java.base/share/classes/java/nio/file/DirectoryStream.java
41159 views
/*1* Copyright (c) 2007, 2020, 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.util.Iterator;28import java.io.Closeable;29import java.io.IOException;3031/**32* An object to iterate over the entries in a directory. A directory stream33* allows for the convenient use of the for-each construct to iterate over a34* directory.35*36* <p> <b> While {@code DirectoryStream} extends {@code Iterable}, it is not a37* general-purpose {@code Iterable} as it supports only a single {@code38* Iterator}; invoking the {@link #iterator iterator} method to obtain a second39* or subsequent iterator throws {@code IllegalStateException}. </b>40*41* <p> An important property of the directory stream's {@code Iterator} is that42* its {@link Iterator#hasNext() hasNext} method is guaranteed to read-ahead by43* at least one element. If {@code hasNext} method returns {@code true}, and is44* followed by a call to the {@code next} method, it is guaranteed that the45* {@code next} method will not throw an exception due to an I/O error, or46* because the stream has been {@link #close closed}. The {@code Iterator} does47* not support the {@link Iterator#remove remove} operation.48*49* <p> A {@code DirectoryStream} is opened upon creation and is closed by50* invoking the {@code close} method. Closing a directory stream releases any51* resources associated with the stream. Failure to close the stream may result52* in a resource leak. The try-with-resources statement provides a useful53* construct to ensure that the stream is closed:54* <pre>55* Path dir = ...56* try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {57* for (Path entry: stream) {58* ...59* }60* }61* </pre>62*63* <p> Once a directory stream is closed, then further access to the directory,64* using the {@code Iterator}, behaves as if the end of stream has been reached.65* Due to read-ahead, the {@code Iterator} may return one or more elements66* after the directory stream has been closed. Once these buffered elements67* have been read, then subsequent calls to the {@code hasNext} method returns68* {@code false}, and subsequent calls to the {@code next} method will throw69* {@code NoSuchElementException}.70*71* <p> A directory stream is not required to be <i>asynchronously closeable</i>.72* If a thread is blocked on the directory stream's iterator reading from the73* directory, and another thread invokes the {@code close} method, then the74* second thread may block until the read operation is complete.75*76* <p> If an I/O error is encountered when accessing the directory then it77* causes the {@code Iterator}'s {@code hasNext} or {@code next} methods to78* throw {@link DirectoryIteratorException} with the {@link IOException} as the79* cause. As stated above, the {@code hasNext} method is guaranteed to80* read-ahead by at least one element. This means that if {@code hasNext} method81* returns {@code true}, and is followed by a call to the {@code next} method,82* then it is guaranteed that the {@code next} method will not fail with a83* {@code DirectoryIteratorException}.84*85* <p> The elements returned by the iterator are in no specific order. Some file86* systems maintain special links to the directory itself and the directory's87* parent directory. Entries representing these links are not returned by the88* iterator.89*90* <p> The iterator is <i>weakly consistent</i>. It is thread safe but does not91* freeze the directory while iterating, so it may (or may not) reflect updates92* to the directory that occur after the {@code DirectoryStream} is created.93*94* <p> <b>Usage Examples:</b>95* Suppose we want a list of the source files in a directory. This example uses96* both the for-each and try-with-resources constructs.97* <pre>98* List<Path> listSourceFiles(Path dir) throws IOException {99* List<Path> result = new ArrayList<>();100* try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {101* for (Path entry: stream) {102* result.add(entry);103* }104* } catch (DirectoryIteratorException ex) {105* // I/O error encountered during the iteration, the cause is an IOException106* throw ex.getCause();107* }108* return result;109* }110* </pre>111* @param <T> The type of element returned by the iterator112*113* @since 1.7114*115* @see Files#newDirectoryStream(Path)116*/117118public interface DirectoryStream<T>119extends Closeable, Iterable<T> {120/**121* An interface that is implemented by objects that decide if a directory122* entry should be accepted or filtered. A {@code Filter} is passed as the123* parameter to the {@link Files#newDirectoryStream(Path,DirectoryStream.Filter)}124* method when opening a directory to iterate over the entries in the125* directory.126*127* @param <T> the type of the directory entry128*129* @since 1.7130*/131@FunctionalInterface132public static interface Filter<T> {133/**134* Decides if the given directory entry should be accepted or filtered.135*136* @param entry137* the directory entry to be tested138*139* @return {@code true} if the directory entry should be accepted140*141* @throws IOException142* If an I/O error occurs143*/144boolean accept(T entry) throws IOException;145}146147/**148* Returns the iterator associated with this {@code DirectoryStream}.149*150* @return the iterator associated with this {@code DirectoryStream}151*152* @throws IllegalStateException153* if this directory stream is closed or the iterator has already154* been returned155*/156@Override157Iterator<T> iterator();158}159160161