1/*
2 * Copyright (c) 2009, 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
26package jdk.nio.zipfs;
27
28import java.nio.file.DirectoryStream;
29import java.nio.file.ClosedDirectoryStreamException;
30import java.nio.file.NotDirectoryException;
31import java.nio.file.Path;
32import java.util.Iterator;
33import java.util.NoSuchElementException;
34import java.io.IOException;
35
36/**
37 *
38 * @author  Xueming Shen, Rajendra Gutupalli, Jaya Hangal
39 */
40
41class ZipDirectoryStream implements DirectoryStream<Path> {
42
43    private final ZipFileSystem zipfs;
44    private final byte[] path;
45    private final DirectoryStream.Filter<? super Path> filter;
46    private volatile boolean isClosed;
47    private volatile Iterator<Path> itr;
48
49    ZipDirectoryStream(ZipPath zipPath,
50                       DirectoryStream.Filter<? super java.nio.file.Path> filter)
51        throws IOException
52    {
53        this.zipfs = zipPath.getFileSystem();
54        this.path = zipPath.getResolvedPath();
55        this.filter = filter;
56        // sanity check
57        if (!zipfs.isDirectory(path))
58            throw new NotDirectoryException(zipPath.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(path, filter);
70        } catch (IOException e) {
71            throw new IllegalStateException(e);
72        }
73        return new Iterator<Path>() {
74            private Path next;
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
101
102}
103