jdk/src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java
changeset 2057 3acf8e5e2ca0
child 2071 5e6af6d106cb
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.fs;
       
    27 
       
    28 import java.nio.file.*;
       
    29 import java.util.Iterator;
       
    30 import java.util.ConcurrentModificationException;
       
    31 import java.util.NoSuchElementException;
       
    32 import java.io.IOException;
       
    33 
       
    34 import static sun.nio.fs.WindowsNativeDispatcher.*;
       
    35 import static sun.nio.fs.WindowsConstants.*;
       
    36 
       
    37 /**
       
    38  * Windows implementation of DirectoryStream
       
    39  */
       
    40 
       
    41 class WindowsDirectoryStream
       
    42     implements DirectoryStream<Path>
       
    43 {
       
    44     private final WindowsPath dir;
       
    45     private final DirectoryStream.Filter<? super Path> filter;
       
    46 
       
    47     // handle to directory
       
    48     private final long handle;
       
    49     // first entry in the directory
       
    50     private final String firstName;
       
    51 
       
    52     private final Object closeLock = new Object();
       
    53 
       
    54     // need closeLock to access these
       
    55     private boolean isOpen = true;
       
    56     private Iterator<Path> iterator;
       
    57 
       
    58 
       
    59     WindowsDirectoryStream(WindowsPath dir, DirectoryStream.Filter<? super Path> filter)
       
    60         throws IOException
       
    61     {
       
    62         this.dir = dir;
       
    63         this.filter = filter;
       
    64 
       
    65         try {
       
    66             // Need to append * or \* to match entries in directory.
       
    67             String search = dir.getPathForWin32Calls();
       
    68             char last = search.charAt(search.length() -1);
       
    69             if (last == ':' || last == '\\') {
       
    70                 search += "*";
       
    71             } else {
       
    72                 search += "\\*";
       
    73             }
       
    74 
       
    75             FirstFile first = FindFirstFile(search);
       
    76             this.handle = first.handle();
       
    77             this.firstName = first.name();
       
    78         } catch (WindowsException x) {
       
    79             if (x.lastError() == ERROR_DIRECTORY) {
       
    80                 throw new NotDirectoryException(dir.getPathForExceptionMessage());
       
    81             }
       
    82             x.rethrowAsIOException(dir);
       
    83 
       
    84             // keep compiler happy
       
    85             throw new AssertionError();
       
    86         }
       
    87     }
       
    88 
       
    89     @Override
       
    90     public void close()
       
    91         throws IOException
       
    92     {
       
    93         synchronized (closeLock) {
       
    94             if (!isOpen)
       
    95                 return;
       
    96             isOpen = false;
       
    97         }
       
    98         try {
       
    99             FindClose(handle);
       
   100         } catch (WindowsException x) {
       
   101             x.rethrowAsIOException(dir);
       
   102         }
       
   103     }
       
   104 
       
   105     @Override
       
   106     public Iterator<Path> iterator() {
       
   107         if (!isOpen) {
       
   108             throw new IllegalStateException("Directory stream is closed");
       
   109         }
       
   110         synchronized (this) {
       
   111             if (iterator != null)
       
   112                 throw new IllegalStateException("Iterator already obtained");
       
   113             iterator = new WindowsDirectoryIterator(firstName);
       
   114             return iterator;
       
   115         }
       
   116     }
       
   117 
       
   118     private static void throwAsConcurrentModificationException(Throwable t) {
       
   119         ConcurrentModificationException cme = new ConcurrentModificationException();
       
   120         cme.initCause(t);
       
   121         throw cme;
       
   122     }
       
   123 
       
   124     private class WindowsDirectoryIterator implements Iterator<Path> {
       
   125         private boolean atEof;
       
   126         private String first;
       
   127         private Path nextEntry;
       
   128         private Path prevEntry;
       
   129 
       
   130         WindowsDirectoryIterator(String first) {
       
   131             atEof = false;
       
   132             this.first = first;
       
   133         }
       
   134 
       
   135         // applies filter and also ignores "." and ".."
       
   136         private Path acceptEntry(String s) {
       
   137             if (s.equals(".") || s.equals(".."))
       
   138                 return null;
       
   139             Path entry = WindowsPath
       
   140                 .createFromNormalizedPath(dir.getFileSystem(), dir + "\\" + s);
       
   141             if (filter.accept(entry)) {
       
   142                 return entry;
       
   143             } else {
       
   144                 return null;
       
   145             }
       
   146         }
       
   147 
       
   148         // reads next directory entry
       
   149         private Path readNextEntry() {
       
   150             // handle first element returned by search
       
   151             if (first != null) {
       
   152                 nextEntry = acceptEntry(first);
       
   153                 first = null;
       
   154                 if (nextEntry != null)
       
   155                     return nextEntry;
       
   156             }
       
   157 
       
   158             String name = null;
       
   159             for (;;) {
       
   160                 // synchronize on closeLock to prevent close while reading
       
   161                 synchronized (closeLock) {
       
   162                     if (!isOpen)
       
   163                         throwAsConcurrentModificationException(new
       
   164                             IllegalStateException("Directory stream is closed"));
       
   165                     try {
       
   166                         name = FindNextFile(handle);
       
   167                     } catch (WindowsException x) {
       
   168                         try {
       
   169                             x.rethrowAsIOException(dir);
       
   170                         } catch (IOException ioe) {
       
   171                             throwAsConcurrentModificationException(ioe);
       
   172                         }
       
   173                     }
       
   174                 }
       
   175 
       
   176                 // EOF
       
   177                 if (name == null)
       
   178                     return null;
       
   179 
       
   180                 Path entry = acceptEntry(name);
       
   181                 if (entry != null)
       
   182                     return entry;
       
   183             }
       
   184         }
       
   185 
       
   186         @Override
       
   187         public synchronized boolean hasNext() {
       
   188             if (nextEntry == null && !atEof) {
       
   189                 nextEntry = readNextEntry();
       
   190                 atEof = (nextEntry == null);
       
   191             }
       
   192             return nextEntry != null;
       
   193         }
       
   194 
       
   195         @Override
       
   196         public synchronized Path next() {
       
   197             if (nextEntry == null) {
       
   198                 if (!atEof) {
       
   199                     nextEntry = readNextEntry();
       
   200                 }
       
   201                 if (nextEntry == null) {
       
   202                     atEof = true;
       
   203                     throw new NoSuchElementException();
       
   204                 }
       
   205             }
       
   206             prevEntry = nextEntry;
       
   207             nextEntry = null;
       
   208             return prevEntry;
       
   209         }
       
   210 
       
   211         @Override
       
   212         public void remove() {
       
   213             if (!isOpen) {
       
   214                 throw new IllegalStateException("Directory stream is closed");
       
   215             }
       
   216             Path entry;
       
   217             synchronized (this) {
       
   218                 if (prevEntry == null)
       
   219                     throw new IllegalStateException("no last element");
       
   220                 entry = prevEntry;
       
   221                 prevEntry = null;
       
   222             }
       
   223             try {
       
   224                 entry.delete(true);
       
   225             } catch (IOException ioe) {
       
   226                 throwAsConcurrentModificationException(ioe);
       
   227             } catch (SecurityException se) {
       
   228                 throwAsConcurrentModificationException(se);
       
   229             }
       
   230         }
       
   231     }
       
   232 }