1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.jrtfs;
  27 
  28 import java.nio.file.DirectoryStream;
  29 import java.nio.file.ClosedDirectoryStreamException;
  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 final class JrtDirectoryStream implements DirectoryStream<Path> {
  37     private final JrtFileSystem jrtfs;
  38     private final byte[] path;
  39     private final DirectoryStream.Filter<? super Path> filter;
  40     private volatile boolean isClosed;
  41     private volatile Iterator<Path> itr;
  42 
  43     JrtDirectoryStream(JrtPath jrtPath,
  44                        DirectoryStream.Filter<? super java.nio.file.Path> filter)
  45         throws IOException
  46     {
  47         this.jrtfs = jrtPath.getFileSystem();
  48         this.path = jrtPath.getResolvedPath();
  49         this.filter = filter;
  50         // sanity check
  51         if (!jrtfs.isDirectory(path))
  52             throw new NotDirectoryException(jrtPath.toString());
  53     }
  54 
  55     @Override
  56     public synchronized Iterator<Path> iterator() {
  57         if (isClosed)
  58             throw new ClosedDirectoryStreamException();
  59         if (itr != null)
  60             throw new IllegalStateException("Iterator has already been returned");
  61 
  62         try {
  63             itr = jrtfs.iteratorOf(path, filter);
  64         } catch (IOException e) {
  65             throw new IllegalStateException(e);
  66         }
  67         return new Iterator<Path>() {
  68             private Path next;
  69             @Override
  70             public boolean hasNext() {
  71                 if (isClosed)
  72                     return false;
  73                 return itr.hasNext();
  74             }
  75 
  76             @Override
  77             public synchronized Path next() {
  78                 if (isClosed)
  79                     throw new NoSuchElementException();
  80                 return itr.next();
  81             }
  82 
  83             @Override
  84             public void remove() {
  85                 throw new UnsupportedOperationException();
  86             }
  87         };
  88     }
  89 
  90     @Override
  91     public synchronized void close() throws IOException {
  92         isClosed = true;
  93     }
  94 }