< prev index next >

src/java.base/share/classes/jdk/internal/jrtfs/JrtDirectoryStream.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.jrtfs;
  26 
  27 import java.nio.file.DirectoryStream;
  28 import java.nio.file.ClosedDirectoryStreamException;
  29 import java.nio.file.DirectoryIteratorException;
  30 import java.nio.file.NotDirectoryException;
  31 import java.nio.file.Path;
  32 import java.util.Iterator;
  33 import java.util.NoSuchElementException;
  34 import java.io.IOException;
  35 
  36 /**
  37  * DirectoryStream implementation for jrt file system implementations.
  38  */
  39 final class JrtDirectoryStream implements DirectoryStream<Path> {
  40 
  41     private final AbstractJrtFileSystem jrtfs;
  42     private final byte[] path;
  43     // prefix to be used for children of this directory
  44     // so that child path are reported relatively (if needed)
  45     private final String childPrefix;
  46     private final DirectoryStream.Filter<? super Path> filter;
  47     private volatile boolean isClosed;
  48     private volatile Iterator<Path> itr;
  49 
  50     JrtDirectoryStream(AbstractJrtPath jrtPath,
  51             DirectoryStream.Filter<? super java.nio.file.Path> filter)
  52             throws IOException {
  53         this.jrtfs = jrtPath.getFileSystem();
  54         this.path = jrtPath.getResolvedPath();
  55         // sanity check
  56         if (!jrtfs.isDirectory(path, true)) {
  57             throw new NotDirectoryException(jrtPath.toString());
  58         }
  59 
  60         // absolute path and does not have funky chars in front like /./java.base
  61         if (jrtPath.isAbsolute() && (path.length == jrtPath.getPathLength())) {
  62             childPrefix = null;
  63         } else {
  64             // cases where directory content needs to modified with prefix
  65             // like ./java.base, /./java.base, java.base and so on.
  66             String dirName = jrtPath.toString();
  67             int idx = dirName.indexOf(JrtFileSystem.getString(path).substring(1));
  68             childPrefix = dirName.substring(0, idx);
  69         }
  70         this.filter = filter;
  71     }
  72 
  73     @Override
  74     public synchronized Iterator<Path> iterator() {
  75         if (isClosed) {
  76             throw new ClosedDirectoryStreamException();
  77         }
  78         if (itr != null) {
  79             throw new IllegalStateException("Iterator has already been returned");
  80         }
  81 
  82         try {
  83             itr = jrtfs.iteratorOf(path, childPrefix);
  84         } catch (IOException e) {
  85             throw new IllegalStateException(e);
  86         }
  87         return new Iterator<Path>() {
  88             /*
  89              * next Path value to return from this iterator.
  90              * null value means hasNext() not called yet
  91              * or last hasNext() returned false or resulted
  92              * in exception. If last hasNext() returned true,
  93              * then this field has non-null value.
  94              */
  95             private Path next;
  96 
  97             // get-and-clear and set-next by these methods
  98             private Path getAndClearNext() {
  99                 assert next != null;
 100                 Path result = this.next;
 101                 this.next = null;
 102                 return result;
 103             }




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.jrtfs;
  26 
  27 import java.nio.file.DirectoryStream;
  28 import java.nio.file.ClosedDirectoryStreamException;
  29 import java.nio.file.DirectoryIteratorException;
  30 import java.nio.file.NotDirectoryException;
  31 import java.nio.file.Path;
  32 import java.util.Iterator;
  33 import java.util.NoSuchElementException;
  34 import java.io.IOException;
  35 
  36 /**
  37  * DirectoryStream implementation for jrt file system implementations.
  38  */
  39 final class JrtDirectoryStream implements DirectoryStream<Path> {
  40 
  41     private final AbstractJrtFileSystem jrtfs;
  42     private final AbstractJrtPath dir;



  43     private final DirectoryStream.Filter<? super Path> filter;
  44     private volatile boolean isClosed;
  45     private volatile Iterator<Path> itr;
  46 
  47     JrtDirectoryStream(AbstractJrtPath jrtPath,
  48             DirectoryStream.Filter<? super java.nio.file.Path> filter)
  49             throws IOException {
  50         this.jrtfs = jrtPath.getFileSystem();
  51         this.dir = jrtPath;
  52         // sanity check
  53         if (!jrtfs.isDirectory(dir, true)) {
  54             throw new NotDirectoryException(jrtPath.toString());
  55         }
  56 










  57         this.filter = filter;
  58     }
  59 
  60     @Override
  61     public synchronized Iterator<Path> iterator() {
  62         if (isClosed) {
  63             throw new ClosedDirectoryStreamException();
  64         }
  65         if (itr != null) {
  66             throw new IllegalStateException("Iterator has already been returned");
  67         }
  68 
  69         try {
  70             itr = jrtfs.iteratorOf(dir);
  71         } catch (IOException e) {
  72             throw new IllegalStateException(e);
  73         }
  74         return new Iterator<Path>() {
  75             /*
  76              * next Path value to return from this iterator.
  77              * null value means hasNext() not called yet
  78              * or last hasNext() returned false or resulted
  79              * in exception. If last hasNext() returned true,
  80              * then this field has non-null value.
  81              */
  82             private Path next;
  83 
  84             // get-and-clear and set-next by these methods
  85             private Path getAndClearNext() {
  86                 assert next != null;
  87                 Path result = this.next;
  88                 this.next = null;
  89                 return result;
  90             }


< prev index next >