Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java
32288 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.*;28import java.nio.file.attribute.BasicFileAttributes;29import java.util.Iterator;30import java.util.NoSuchElementException;31import java.io.IOException;3233import static sun.nio.fs.WindowsNativeDispatcher.*;34import static sun.nio.fs.WindowsConstants.*;3536/**37* Windows implementation of DirectoryStream38*/3940class WindowsDirectoryStream41implements DirectoryStream<Path>42{43private final WindowsPath dir;44private final DirectoryStream.Filter<? super Path> filter;4546// handle to directory47private final long handle;48// first entry in the directory49private final String firstName;5051// buffer for WIN32_FIND_DATA structure that receives information about file52private final NativeBuffer findDataBuffer;5354private final Object closeLock = new Object();5556// need closeLock to access these57private boolean isOpen = true;58private Iterator<Path> iterator;596061WindowsDirectoryStream(WindowsPath dir, DirectoryStream.Filter<? super Path> filter)62throws IOException63{64this.dir = dir;65this.filter = filter;6667try {68// Need to append * or \* to match entries in directory.69String search = dir.getPathForWin32Calls();70char last = search.charAt(search.length() -1);71if (last == ':' || last == '\\') {72search += "*";73} else {74search += "\\*";75}7677FirstFile first = FindFirstFile(search);78this.handle = first.handle();79this.firstName = first.name();80this.findDataBuffer = WindowsFileAttributes.getBufferForFindData();81} catch (WindowsException x) {82if (x.lastError() == ERROR_DIRECTORY) {83throw new NotDirectoryException(dir.getPathForExceptionMessage());84}85x.rethrowAsIOException(dir);8687// keep compiler happy88throw new AssertionError();89}90}9192@Override93public void close()94throws IOException95{96synchronized (closeLock) {97if (!isOpen)98return;99isOpen = false;100}101findDataBuffer.release();102try {103FindClose(handle);104} catch (WindowsException x) {105x.rethrowAsIOException(dir);106}107}108109@Override110public Iterator<Path> iterator() {111if (!isOpen) {112throw new IllegalStateException("Directory stream is closed");113}114synchronized (this) {115if (iterator != null)116throw new IllegalStateException("Iterator already obtained");117iterator = new WindowsDirectoryIterator(firstName);118return iterator;119}120}121122private class WindowsDirectoryIterator implements Iterator<Path> {123private boolean atEof;124private String first;125private Path nextEntry;126private String prefix;127128WindowsDirectoryIterator(String first) {129atEof = false;130this.first = first;131if (dir.needsSlashWhenResolving()) {132prefix = dir.toString() + "\\";133} else {134prefix = dir.toString();135}136}137138// links to self and parent directories are ignored139private boolean isSelfOrParent(String name) {140return name.equals(".") || name.equals("..");141}142143// applies filter and also ignores "." and ".."144private Path acceptEntry(String s, BasicFileAttributes attrs) {145Path entry = WindowsPath146.createFromNormalizedPath(dir.getFileSystem(), prefix + s, attrs);147try {148if (filter.accept(entry))149return entry;150} catch (IOException ioe) {151throw new DirectoryIteratorException(ioe);152}153return null;154}155156// reads next directory entry157private Path readNextEntry() {158// handle first element returned by search159if (first != null) {160nextEntry = isSelfOrParent(first) ? null : acceptEntry(first, null);161first = null;162if (nextEntry != null)163return nextEntry;164}165166for (;;) {167String name = null;168WindowsFileAttributes attrs;169170// synchronize on closeLock to prevent close while reading171synchronized (closeLock) {172try {173if (isOpen) {174name = FindNextFile(handle, findDataBuffer.address());175}176} catch (WindowsException x) {177IOException ioe = x.asIOException(dir);178throw new DirectoryIteratorException(ioe);179}180181// NO_MORE_FILES or stream closed182if (name == null) {183atEof = true;184return null;185}186187// ignore link to self and parent directories188if (isSelfOrParent(name))189continue;190191// grab the attributes from the WIN32_FIND_DATA structure192// (needs to be done while holding closeLock because close193// will release the buffer)194attrs = WindowsFileAttributes195.fromFindData(findDataBuffer.address());196}197198// return entry if accepted by filter199Path entry = acceptEntry(name, attrs);200if (entry != null)201return entry;202}203}204205@Override206public synchronized boolean hasNext() {207if (nextEntry == null && !atEof)208nextEntry = readNextEntry();209return nextEntry != null;210}211212@Override213public synchronized Path next() {214Path result = null;215if (nextEntry == null && !atEof) {216result = readNextEntry();217} else {218result = nextEntry;219nextEntry = null;220}221if (result == null)222throw new NoSuchElementException();223return result;224}225226@Override227public void remove() {228throw new UnsupportedOperationException();229}230}231}232233234