src/share/classes/java/nio/file/FileTreeWalker.java

Print this page
rev 7023 : 8006884: (fs) Add Files.list, lines and find
Reviewed-by:
Contributed-by: alan.bateman@oracle.com, henry.jen@oracle.com


  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.nio.file.attribute.BasicFileAttributes;
  29 import java.io.Closeable;
  30 import java.io.IOException;
  31 import java.util.ArrayDeque;

  32 import java.util.Iterator;
  33 import java.util.Set;
  34 import sun.nio.fs.BasicFileAttributesHolder;
  35 
  36 /**
  37  * Walks a file tree, generating a sequence of events corresponding to the files
  38  * in the tree.
  39  *
  40  * <pre>{@code
  41  *     Path top = ...
  42  *     Set<FileVisitOption> options = ...
  43  *     int maxDepth = ...
  44  *
  45  *     try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
  46  *         FileTreeWalker.Event ev = walker.walk(top);
  47  *         do {
  48  *             process(ev);
  49  *             ev = walker.next();
  50  *         } while (ev != null);
  51  *     }
  52  * }</pre>
  53  *


 147 
 148         EventType type() {
 149             return type;
 150         }
 151 
 152         Path file() {
 153             return file;
 154         }
 155 
 156         BasicFileAttributes attributes() {
 157             return attrs;
 158         }
 159 
 160         IOException ioeException() {
 161             return ioe;
 162         }
 163     }
 164 
 165     /**
 166      * Creates a {@code FileTreeWalker}.









 167      */
 168     FileTreeWalker(Set<FileVisitOption> options, int maxDepth) {
 169         boolean fl = false;
 170         for (FileVisitOption option: options) {
 171             // will throw NPE if options contains null
 172             switch (option) {
 173                 case FOLLOW_LINKS : fl = true; break;
 174                 default:
 175                     throw new AssertionError("Should not get here");
 176             }
 177         }



 178         this.followLinks = fl;
 179         this.linkOptions = (fl) ? new LinkOption[0] :
 180             new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
 181         this.maxDepth = maxDepth;
 182     }
 183 
 184     /**
 185      * Returns the attributes of the given file, taking into account whether
 186      * the walk is following sym links is not. The {@code canUseCached}
 187      * argument determines whether this method can use cached attributes.
 188      */
 189     private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
 190         throws IOException
 191     {
 192         // if attributes are cached then use them if possible
 193         if (canUseCached &&
 194             (file instanceof BasicFileAttributesHolder) &&
 195             (System.getSecurityManager() == null))
 196         {
 197             BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();




  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.nio.file.attribute.BasicFileAttributes;
  29 import java.io.Closeable;
  30 import java.io.IOException;
  31 import java.util.ArrayDeque;
  32 import java.util.Collection;
  33 import java.util.Iterator;

  34 import sun.nio.fs.BasicFileAttributesHolder;
  35 
  36 /**
  37  * Walks a file tree, generating a sequence of events corresponding to the files
  38  * in the tree.
  39  *
  40  * <pre>{@code
  41  *     Path top = ...
  42  *     Set<FileVisitOption> options = ...
  43  *     int maxDepth = ...
  44  *
  45  *     try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
  46  *         FileTreeWalker.Event ev = walker.walk(top);
  47  *         do {
  48  *             process(ev);
  49  *             ev = walker.next();
  50  *         } while (ev != null);
  51  *     }
  52  * }</pre>
  53  *


 147 
 148         EventType type() {
 149             return type;
 150         }
 151 
 152         Path file() {
 153             return file;
 154         }
 155 
 156         BasicFileAttributes attributes() {
 157             return attrs;
 158         }
 159 
 160         IOException ioeException() {
 161             return ioe;
 162         }
 163     }
 164 
 165     /**
 166      * Creates a {@code FileTreeWalker}.
 167      *
 168      * @throws  IllegalArgumentException
 169      *          if {@code maxDepth} is negative
 170      * @throws  ClassCastException
 171      *          if (@code options} contains an element that is not a
 172      *          {@code FileVisitOption}
 173      * @throws  NullPointerException
 174      *          if {@code options} is {@ocde null} or the options
 175      *          array contains a {@code null} element
 176      */
 177     FileTreeWalker(Collection<FileVisitOption> options, int maxDepth) {
 178         boolean fl = false;
 179         for (FileVisitOption option: options) {
 180             // will throw NPE if options contains null
 181             switch (option) {
 182                 case FOLLOW_LINKS : fl = true; break;
 183                 default:
 184                     throw new AssertionError("Should not get here");
 185             }
 186         }
 187         if (maxDepth < 0)
 188             throw new IllegalArgumentException("'maxDepth' is negative");
 189 
 190         this.followLinks = fl;
 191         this.linkOptions = (fl) ? new LinkOption[0] :
 192             new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
 193         this.maxDepth = maxDepth;
 194     }
 195 
 196     /**
 197      * Returns the attributes of the given file, taking into account whether
 198      * the walk is following sym links is not. The {@code canUseCached}
 199      * argument determines whether this method can use cached attributes.
 200      */
 201     private BasicFileAttributes getAttributes(Path file, boolean canUseCached)
 202         throws IOException
 203     {
 204         // if attributes are cached then use them if possible
 205         if (canUseCached &&
 206             (file instanceof BasicFileAttributesHolder) &&
 207             (System.getSecurityManager() == null))
 208         {
 209             BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();