jdk/src/share/classes/java/nio/file/FileTreeIterator.java
author henryjen
Thu, 09 May 2013 14:44:56 -0700
changeset 17696 23a863fbb6c3
permissions -rw-r--r--
8006884: (fs) Add Files.list, lines and find Reviewed-by: briangoetz, mduigou Contributed-by: alan.bateman@oracle.com, henry.jen@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     1
/*
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     4
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    10
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    15
 * accompanied this code).
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    16
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    20
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    23
 * questions.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    24
 */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    25
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    26
package java.nio.file;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    27
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    28
import java.io.Closeable;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    29
import java.io.IOException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    30
import java.io.UncheckedIOException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    31
import java.util.Arrays;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    32
import java.util.Iterator;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    33
import java.util.NoSuchElementException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    34
import java.util.Objects;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    35
import java.nio.file.FileTreeWalker.Event;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    36
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    37
/**
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    38
 * An {@code Iterator to iterate over the nodes of a file tree.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    39
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    40
 * <pre>{@code
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    41
 *     try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    42
 *         while (iterator.hasNext()) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    43
 *             Event ev = iterator.next();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    44
 *             Path path = ev.file();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    45
 *             BasicFileAttributes attrs = ev.attributes();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    46
 *         }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    47
 *     }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    48
 * }</pre>
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    49
 */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    50
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    51
class FileTreeIterator implements Iterator<Event>, Closeable {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    52
    private final FileTreeWalker walker;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    53
    private Event next;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    54
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    55
    /**
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    56
     * Creates a new iterator to walk the file tree starting at the given file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    57
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    58
     * @throws  IllegalArgumentException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    59
     *          if {@code maxDepth} is negative
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    60
     * @throws  IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    61
     *          if an I/O errors occurs opening the starting file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    62
     * @throws  SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    63
     *          if the security manager denies access to the starting file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    64
     * @throws  NullPointerException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    65
     *          if {@code start} or {@code options} is {@ocde null} or
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    66
     *          the options array contains a {@code null} element
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    67
     */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    68
    FileTreeIterator(Path start, int maxDepth, FileVisitOption... options)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    69
        throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    70
    {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    71
        this.walker = new FileTreeWalker(Arrays.asList(options), maxDepth);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    72
        this.next = walker.walk(start);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    73
        assert next.type() == FileTreeWalker.EventType.ENTRY ||
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    74
               next.type() == FileTreeWalker.EventType.START_DIRECTORY;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    75
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    76
        // IOException if there a problem accessing the starting file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    77
        IOException ioe = next.ioeException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    78
        if (ioe != null)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    79
            throw ioe;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    80
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    81
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    82
    private void fetchNextIfNeeded() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    83
        if (next == null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    84
            FileTreeWalker.Event ev = walker.next();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    85
            while (ev != null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    86
                IOException ioe = ev.ioeException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    87
                if (ioe != null)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    88
                    throw new UncheckedIOException(ioe);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    89
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    90
                // END_DIRECTORY events are ignored
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    91
                if (ev.type() != FileTreeWalker.EventType.END_DIRECTORY) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    92
                    next = ev;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    93
                    return;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    94
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    95
                ev = walker.next();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    96
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    97
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    98
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    99
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   100
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   101
    public boolean hasNext() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   102
        if (!walker.isOpen())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   103
            throw new IllegalStateException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   104
        fetchNextIfNeeded();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   105
        return next != null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   106
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   107
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   108
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   109
    public Event next() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   110
        if (!walker.isOpen())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   111
            throw new IllegalStateException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   112
        fetchNextIfNeeded();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   113
        if (next == null)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   114
            throw new NoSuchElementException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   115
        Event result = next;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   116
        next = null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   117
        return result;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   118
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   119
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   120
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   121
    public void close() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   122
        walker.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   123
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   124
}