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.*;
907N/Aimport java.nio.file.attribute.BasicFileAttributes;
893N/Aimport java.util.Iterator;
893N/Aimport java.util.NoSuchElementException;
893N/Aimport java.io.IOException;
893N/A
893N/Aimport static sun.nio.fs.WindowsNativeDispatcher.*;
893N/Aimport static sun.nio.fs.WindowsConstants.*;
893N/A
893N/A/**
893N/A * Windows implementation of DirectoryStream
893N/A */
893N/A
893N/Aclass WindowsDirectoryStream
893N/A implements DirectoryStream<Path>
893N/A{
893N/A private final WindowsPath dir;
893N/A private final DirectoryStream.Filter<? super Path> filter;
893N/A
893N/A // handle to directory
893N/A private final long handle;
893N/A // first entry in the directory
893N/A private final String firstName;
893N/A
907N/A // buffer for WIN32_FIND_DATA structure that receives information about file
907N/A private final NativeBuffer findDataBuffer;
907N/A
893N/A private final Object closeLock = new Object();
893N/A
893N/A // need closeLock to access these
893N/A private boolean isOpen = true;
893N/A private Iterator<Path> iterator;
893N/A
893N/A
893N/A WindowsDirectoryStream(WindowsPath dir, DirectoryStream.Filter<? super Path> filter)
893N/A throws IOException
893N/A {
893N/A this.dir = dir;
893N/A this.filter = filter;
893N/A
893N/A try {
893N/A // Need to append * or \* to match entries in directory.
893N/A String search = dir.getPathForWin32Calls();
893N/A char last = search.charAt(search.length() -1);
893N/A if (last == ':' || last == '\\') {
893N/A search += "*";
893N/A } else {
893N/A search += "\\*";
893N/A }
893N/A
893N/A FirstFile first = FindFirstFile(search);
893N/A this.handle = first.handle();
893N/A this.firstName = first.name();
907N/A this.findDataBuffer = WindowsFileAttributes.getBufferForFindData();
893N/A } catch (WindowsException x) {
893N/A if (x.lastError() == ERROR_DIRECTORY) {
893N/A throw new NotDirectoryException(dir.getPathForExceptionMessage());
893N/A }
893N/A x.rethrowAsIOException(dir);
893N/A
893N/A // keep compiler happy
893N/A throw new AssertionError();
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void close()
893N/A throws IOException
893N/A {
893N/A synchronized (closeLock) {
893N/A if (!isOpen)
893N/A return;
893N/A isOpen = false;
893N/A }
907N/A findDataBuffer.release();
893N/A try {
893N/A FindClose(handle);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(dir);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public Iterator<Path> iterator() {
893N/A if (!isOpen) {
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 WindowsDirectoryIterator(firstName);
893N/A return iterator;
893N/A }
893N/A }
893N/A
893N/A private class WindowsDirectoryIterator implements Iterator<Path> {
893N/A private boolean atEof;
893N/A private String first;
893N/A private Path nextEntry;
5022N/A private String prefix;
893N/A
893N/A WindowsDirectoryIterator(String first) {
893N/A atEof = false;
893N/A this.first = first;
5022N/A if (dir.needsSlashWhenResolving()) {
5022N/A prefix = dir.toString() + "\\";
5022N/A } else {
5022N/A prefix = dir.toString();
5022N/A }
5022N/A }
5022N/A
5022N/A // links to self and parent directories are ignored
5022N/A private boolean isSelfOrParent(String name) {
5022N/A return name.equals(".") || name.equals("..");
893N/A }
893N/A
893N/A // applies filter and also ignores "." and ".."
907N/A private Path acceptEntry(String s, BasicFileAttributes attrs) {
893N/A Path entry = WindowsPath
5022N/A .createFromNormalizedPath(dir.getFileSystem(), prefix + s, attrs);
1319N/A try {
1319N/A if (filter.accept(entry))
1319N/A return entry;
1319N/A } catch (IOException ioe) {
2748N/A throw new DirectoryIteratorException(ioe);
893N/A }
1319N/A return null;
893N/A }
893N/A
893N/A // reads next directory entry
893N/A private Path readNextEntry() {
893N/A // handle first element returned by search
893N/A if (first != null) {
5022N/A nextEntry = isSelfOrParent(first) ? null : acceptEntry(first, null);
893N/A first = null;
893N/A if (nextEntry != null)
893N/A return nextEntry;
893N/A }
893N/A
893N/A for (;;) {
907N/A String name = null;
907N/A WindowsFileAttributes attrs;
907N/A
893N/A // synchronize on closeLock to prevent close while reading
893N/A synchronized (closeLock) {
893N/A try {
2748N/A if (isOpen) {
2748N/A name = FindNextFile(handle, findDataBuffer.address());
907N/A }
893N/A } catch (WindowsException x) {
2748N/A IOException ioe = x.asIOException(dir);
2748N/A throw new DirectoryIteratorException(ioe);
2748N/A }
2748N/A
2748N/A // NO_MORE_FILES or stream closed
2748N/A if (name == null) {
2748N/A atEof = true;
2748N/A return null;
893N/A }
907N/A
5022N/A // ignore link to self and parent directories
5022N/A if (isSelfOrParent(name))
5022N/A continue;
5022N/A
907N/A // grab the attributes from the WIN32_FIND_DATA structure
907N/A // (needs to be done while holding closeLock because close
907N/A // will release the buffer)
907N/A attrs = WindowsFileAttributes
907N/A .fromFindData(findDataBuffer.address());
893N/A }
893N/A
907N/A // return entry if accepted by filter
907N/A Path entry = acceptEntry(name, attrs);
893N/A if (entry != null)
893N/A return entry;
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 = null;
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}