893N/A/*
3261N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.util.Iterator;
893N/Aimport java.util.NoSuchElementException;
893N/Aimport java.util.concurrent.locks.*;
893N/Aimport java.io.IOException;
893N/Aimport static sun.nio.fs.UnixNativeDispatcher.*;
893N/A
893N/A/**
893N/A * Unix implementation of java.nio.file.DirectoryStream
893N/A */
893N/A
893N/Aclass UnixDirectoryStream
893N/A implements DirectoryStream<Path>
893N/A{
893N/A // path to directory when originally opened
893N/A private final UnixPath dir;
893N/A
893N/A // directory pointer (returned by opendir)
893N/A private final long dp;
893N/A
893N/A // filter (may be null)
893N/A private final DirectoryStream.Filter<? super Path> filter;
893N/A
893N/A // used to coorindate closing of directory stream
893N/A private final ReentrantReadWriteLock streamLock =
893N/A new ReentrantReadWriteLock(true);
893N/A
893N/A // indicates if directory stream is open (synchronize on closeLock)
893N/A private volatile boolean isClosed;
893N/A
893N/A // directory iterator
893N/A private Iterator<Path> iterator;
893N/A
893N/A /**
893N/A * Initializes a new instance
893N/A */
893N/A UnixDirectoryStream(UnixPath dir, long dp, DirectoryStream.Filter<? super Path> filter) {
893N/A this.dir = dir;
893N/A this.dp = dp;
893N/A this.filter = filter;
893N/A }
893N/A
893N/A protected final UnixPath directory() {
893N/A return dir;
893N/A }
893N/A
893N/A protected final Lock readLock() {
893N/A return streamLock.readLock();
893N/A }
893N/A
893N/A protected final Lock writeLock() {
893N/A return streamLock.writeLock();
893N/A }
893N/A
893N/A protected final boolean isOpen() {
893N/A return !isClosed;
893N/A }
893N/A
893N/A protected final boolean closeImpl() throws IOException {
893N/A if (!isClosed) {
893N/A isClosed = true;
893N/A try {
893N/A closedir(dp);
893N/A } catch (UnixException x) {
893N/A throw new IOException(x.errorString());
893N/A }
893N/A return true;
893N/A } else {
893N/A return false;
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void close()
893N/A throws IOException
893N/A {
893N/A writeLock().lock();
893N/A try {
893N/A closeImpl();
893N/A } finally {
893N/A writeLock().unlock();
893N/A }
893N/A }
893N/A
893N/A protected final Iterator<Path> iterator(DirectoryStream<Path> ds) {
893N/A if (isClosed) {
893N/A throw new IllegalStateException("Directory stream is closed");
893N/A }
893N/A synchronized (this) {
893N/A if (iterator != null)
893N/A throw new IllegalStateException("Iterator already obtained");
893N/A iterator = new UnixDirectoryIterator(ds);
893N/A return iterator;
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public Iterator<Path> iterator() {
893N/A return iterator(this);
893N/A }
893N/A
893N/A /**
893N/A * Iterator implementation
893N/A */
893N/A private class UnixDirectoryIterator implements Iterator<Path> {
893N/A private final DirectoryStream<Path> stream;
893N/A
893N/A // true when at EOF
893N/A private boolean atEof;
893N/A
893N/A // next entry to return
893N/A private Path nextEntry;
893N/A
893N/A UnixDirectoryIterator(DirectoryStream<Path> stream) {
893N/A atEof = false;
893N/A this.stream = stream;
893N/A }
893N/A
893N/A // Return true if file name is "." or ".."
893N/A private boolean isSelfOrParent(byte[] nameAsBytes) {
893N/A if (nameAsBytes[0] == '.') {
893N/A if ((nameAsBytes.length == 1) ||
893N/A (nameAsBytes.length == 2 && nameAsBytes[1] == '.')) {
893N/A return true;
893N/A }
893N/A }
893N/A return false;
893N/A }
893N/A
893N/A // Returns next entry (or null)
893N/A private Path readNextEntry() {
893N/A assert Thread.holdsLock(this);
893N/A
893N/A for (;;) {
893N/A byte[] nameAsBytes = null;
893N/A
893N/A // prevent close while reading
893N/A readLock().lock();
893N/A try {
2748N/A if (isOpen()) {
893N/A nameAsBytes = readdir(dp);
893N/A }
2748N/A } catch (UnixException x) {
2748N/A IOException ioe = x.asIOException(dir);
2748N/A throw new DirectoryIteratorException(ioe);
893N/A } finally {
893N/A readLock().unlock();
893N/A }
893N/A
893N/A // EOF
893N/A if (nameAsBytes == null) {
2748N/A atEof = true;
893N/A return null;
893N/A }
893N/A
893N/A // ignore "." and ".."
893N/A if (!isSelfOrParent(nameAsBytes)) {
893N/A Path entry = dir.resolve(nameAsBytes);
893N/A
893N/A // return entry if no filter or filter accepts it
1319N/A try {
1319N/A if (filter == null || filter.accept(entry))
1319N/A return entry;
1319N/A } catch (IOException ioe) {
2748N/A throw new DirectoryIteratorException(ioe);
893N/A }
893N/A }
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public synchronized boolean hasNext() {
2748N/A if (nextEntry == null && !atEof)
893N/A nextEntry = readNextEntry();
893N/A return nextEntry != null;
893N/A }
893N/A
893N/A @Override
893N/A public synchronized Path next() {
2748N/A Path result;
2748N/A if (nextEntry == null && !atEof) {
2748N/A result = readNextEntry();
2748N/A } else {
2748N/A result = nextEntry;
2748N/A nextEntry = null;
893N/A }
2748N/A if (result == null)
2748N/A throw new NoSuchElementException();
2748N/A return result;
893N/A }
893N/A
893N/A @Override
893N/A public void remove() {
2748N/A throw new UnsupportedOperationException();
893N/A }
893N/A }
893N/A}