14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.nio.file; 27 28 import java.io.Closeable; 29 import java.io.IOException; 30 import java.io.UncheckedIOException; 31 import java.util.Arrays; 32 import java.util.Iterator; 33 import java.util.NoSuchElementException; 34 import java.util.Objects; 35 import java.nio.file.FileTreeWalker.Event; 36 37 /** 38 * An {@code Iterator to iterate over the nodes of a file tree. 39 * 40 * <pre>{@code 41 * try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) { 42 * while (iterator.hasNext()) { 43 * Event ev = iterator.next(); 44 * Path path = ev.file(); 45 * BasicFileAttributes attrs = ev.attributes(); 46 * } 47 * } 48 * }</pre> 49 */ 50 51 class FileTreeIterator implements Iterator<Event>, Closeable { 52 private final FileTreeWalker walker; 53 private Event next; 54 55 /** 56 * Creates a new iterator to walk the file tree starting at the given file. 57 * 58 * @throws IllegalArgumentException 59 * if {@code maxDepth} is negative 60 * @throws IOException 61 * if an I/O errors occurs opening the starting file 62 * @throws SecurityException 63 * if the security manager denies access to the starting file 64 * @throws NullPointerException 65 * if {@code start} or {@code options} is {@ocde null} or 66 * the options array contains a {@code null} element 67 */ 68 FileTreeIterator(Path start, int maxDepth, FileVisitOption... options) 69 throws IOException 70 { 71 this.walker = new FileTreeWalker(Arrays.asList(options), maxDepth); 72 this.next = walker.walk(start); 73 assert next.type() == FileTreeWalker.EventType.ENTRY || 74 next.type() == FileTreeWalker.EventType.START_DIRECTORY; 75 76 // IOException if there a problem accessing the starting file 77 IOException ioe = next.ioeException(); 78 if (ioe != null) 79 throw ioe; 80 } 81 82 private void fetchNextIfNeeded() { 83 if (next == null) { 84 FileTreeWalker.Event ev = walker.next(); 85 while (ev != null) { | 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.nio.file; 27 28 import java.io.Closeable; 29 import java.io.IOException; 30 import java.io.UncheckedIOException; 31 import java.util.Arrays; 32 import java.util.Iterator; 33 import java.util.NoSuchElementException; 34 import java.nio.file.FileTreeWalker.Event; 35 36 /** 37 * An {@code Iterator} to iterate over the nodes of a file tree. 38 * 39 * <pre>{@code 40 * 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 */ 49 50 class FileTreeIterator implements Iterator<Event>, Closeable { 51 private final FileTreeWalker walker; 52 private Event next; 53 54 /** 55 * Creates a new iterator to walk the file tree starting at the given file. 56 * 57 * @throws IllegalArgumentException 58 * if {@code maxDepth} is negative 59 * @throws IOException 60 * if an I/O errors occurs opening the starting file 61 * @throws SecurityException 62 * if the security manager denies access to the starting file 63 * @throws NullPointerException 64 * if {@code start} or {@code options} is {@code null} or 65 * the options array contains a {@code null} element 66 */ 67 FileTreeIterator(Path start, int maxDepth, FileVisitOption... options) 68 throws IOException 69 { 70 this.walker = new FileTreeWalker(Arrays.asList(options), maxDepth); 71 this.next = walker.walk(start); 72 assert next.type() == FileTreeWalker.EventType.ENTRY || 73 next.type() == FileTreeWalker.EventType.START_DIRECTORY; 74 75 // IOException if there a problem accessing the starting file 76 IOException ioe = next.ioeException(); 77 if (ioe != null) 78 throw ioe; 79 } 80 81 private void fetchNextIfNeeded() { 82 if (next == null) { 83 FileTreeWalker.Event ev = walker.next(); 84 while (ev != null) { |