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