1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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 import java.util.function.Consumer;
  37 
  38 /**
  39  * An {@code Iterator to iterate over the nodes of a file tree.
  40  *
  41  * <pre>{@code
  42  *     try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) {
  43  *         while (iterator.hasNext()) {
  44  *             Event ev = iterator.next();
  45  *             Path path = ev.file();
  46  *             BasicFileAttributes attrs = ev.attributes();
  47  *         }
  48  *     }
  49  * }</pre>
  50  */
  51 
  52 class FileTreeIterator implements Iterator<Event>, Closeable {
  53     private final Consumer<IOException> ioExceptionHandler;
  54     private final FileTreeWalker walker;
  55     private Event next;
  56 
  57     /**
  58      * Creates a new iterator to walk the file tree starting at the given file.
  59      *
  60      * @throws  IllegalArgumentException
  61      *          if {@code maxDepth} is negative
  62      * @throws  SecurityException
  63      *          if the security manager denies access to the starting file
  64      * @throws  NullPointerException
  65      *          if {@code start} or {@code options} or {@code ioExceptionHandler}
  66      *          is {@code null} or the options array contains a {@code null} element
  67      */
  68     FileTreeIterator(Path start, int maxDepth,
  69                      Consumer<IOException> ioExceptionHandler,
  70                      FileVisitOption... options)
  71     {
  72         this.ioExceptionHandler = Objects.requireNonNull(ioExceptionHandler);
  73         this.walker = new FileTreeWalker(Arrays.asList(options), maxDepth);
  74         this.next = walker.walk(start);
  75         assert next.type() == FileTreeWalker.EventType.ENTRY ||
  76                next.type() == FileTreeWalker.EventType.START_DIRECTORY;
  77 
  78         // handle IOException if there a problem accessing the starting file
  79         IOException ioe = next.ioeException();
  80         if (ioe != null) {
  81             next = null;
  82             try {
  83                 ioExceptionHandler.accept(ioe);
  84             } catch (Throwable t) {
  85                 // clean-up when constructors throws
  86                 try {
  87                     close();
  88                 } catch (Throwable ct) {
  89                     t.addSuppressed(ct);
  90                 }
  91                 throw t;
  92             }
  93         }
  94     }
  95 
  96     private void fetchNextIfNeeded() {
  97         if (next == null) {
  98             FileTreeWalker.Event ev = walker.next();
  99             while (ev != null) {
 100                 IOException ioe = ev.ioeException();
 101                 if (ioe != null) {
 102                     ioExceptionHandler.accept(ioe);
 103                 }
 104                 // END_DIRECTORY events are ignored
 105                 else if (ev.type() != FileTreeWalker.EventType.END_DIRECTORY) {
 106                     next = ev;
 107                     return;
 108                 }
 109                 ev = walker.next();
 110             }
 111         }
 112     }
 113 
 114     @Override
 115     public boolean hasNext() {
 116         if (!walker.isOpen())
 117             throw new IllegalStateException();
 118         fetchNextIfNeeded();
 119         return next != null;
 120     }
 121 
 122     @Override
 123     public Event next() {
 124         if (!walker.isOpen())
 125             throw new IllegalStateException();
 126         fetchNextIfNeeded();
 127         if (next == null)
 128             throw new NoSuchElementException();
 129         Event result = next;
 130         next = null;
 131         return result;
 132     }
 133 
 134     @Override
 135     public void close() {
 136         walker.close();
 137     }
 138 }