1 /*
   2  * Copyright (c) 2009, 2019, 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.nio.zipfs;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.ClosedDirectoryStreamException;
  30 import java.nio.file.DirectoryIteratorException;
  31 import java.nio.file.DirectoryStream;
  32 import java.nio.file.NotDirectoryException;
  33 import java.nio.file.Path;
  34 import java.util.Iterator;
  35 import java.util.NoSuchElementException;
  36 
  37 /**
  38  *
  39  * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
  40  */
  41 class ZipDirectoryStream implements DirectoryStream<Path> {
  42 
  43     private final ZipFileSystem zipfs;
  44     private final ZipPath dir;
  45     private final DirectoryStream.Filter<? super Path> filter;
  46     private volatile boolean isClosed;
  47     private volatile Iterator<Path> itr;
  48 
  49     ZipDirectoryStream(ZipPath dir,
  50                        DirectoryStream.Filter<? super java.nio.file.Path> filter)
  51         throws IOException
  52     {
  53         this.zipfs = dir.getFileSystem();
  54         this.dir = dir;
  55         this.filter = filter;
  56         // sanity check
  57         if (!zipfs.isDirectory(dir.getResolvedPath()))
  58             throw new NotDirectoryException(dir.toString());
  59     }
  60 
  61     @Override
  62     public synchronized Iterator<Path> iterator() {
  63         if (isClosed)
  64             throw new ClosedDirectoryStreamException();
  65         if (itr != null)
  66             throw new IllegalStateException("Iterator has already been returned");
  67 
  68         try {
  69             itr = zipfs.iteratorOf(dir, filter);
  70         } catch (IOException e) {
  71             throw new DirectoryIteratorException(e);
  72         }
  73 
  74         return new Iterator<Path>() {
  75             @Override
  76             public boolean hasNext() {
  77                 if (isClosed)
  78                     return false;
  79                 return itr.hasNext();
  80             }
  81 
  82             @Override
  83             public synchronized Path next() {
  84                 if (isClosed)
  85                     throw new NoSuchElementException();
  86                 return itr.next();
  87             }
  88 
  89             @Override
  90             public void remove() {
  91                 throw new UnsupportedOperationException();
  92             }
  93         };
  94     }
  95 
  96     @Override
  97     public synchronized void close() throws IOException {
  98         isClosed = true;
  99     }
 100 }