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.Iterator;
  32 import java.util.List;
  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         // Since options may be a mutable array, use List.of to copy the array 
  71         // and prevent TOCTOU.
  72         this.walker = new FileTreeWalker(List.of(options), maxDepth);
  73         this.next = walker.walk(start);
  74         assert next.type() == FileTreeWalker.EventType.ENTRY ||
  75                next.type() == FileTreeWalker.EventType.START_DIRECTORY;
  76 
  77         // IOException if there a problem accessing the starting file
  78         IOException ioe = next.ioeException();
  79         if (ioe != null)
  80             throw ioe;
  81     }
  82 
  83     private void fetchNextIfNeeded() {
  84         if (next == null) {
  85             FileTreeWalker.Event ev = walker.next();
  86             while (ev != null) {
  87                 IOException ioe = ev.ioeException();
  88                 if (ioe != null)
  89                     throw new UncheckedIOException(ioe);
  90 
  91                 // END_DIRECTORY events are ignored
  92                 if (ev.type() != FileTreeWalker.EventType.END_DIRECTORY) {
  93                     next = ev;
  94                     return;
  95                 }
  96                 ev = walker.next();
  97             }
  98         }
  99     }
 100 
 101     @Override
 102     public boolean hasNext() {
 103         if (!walker.isOpen())
 104             throw new IllegalStateException();
 105         fetchNextIfNeeded();
 106         return next != null;
 107     }
 108 
 109     @Override
 110     public Event next() {
 111         if (!walker.isOpen())
 112             throw new IllegalStateException();
 113         fetchNextIfNeeded();
 114         if (next == null)
 115             throw new NoSuchElementException();
 116         Event result = next;
 117         next = null;
 118         return result;
 119     }
 120 
 121     @Override
 122     public void close() {
 123         walker.close();
 124     }
 125 }