Path: blob/master/src/java.base/share/classes/jdk/internal/jrtfs/JrtDirectoryStream.java
41159 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package jdk.internal.jrtfs;2526import java.nio.file.DirectoryStream;27import java.nio.file.ClosedDirectoryStreamException;28import java.nio.file.DirectoryIteratorException;29import java.nio.file.NotDirectoryException;30import java.nio.file.Path;31import java.util.Iterator;32import java.util.Objects;33import java.util.NoSuchElementException;34import java.io.IOException;3536/**37* DirectoryStream implementation for jrt file system implementations.38*39* @implNote This class needs to maintain JDK 8 source compatibility.40*41* It is used internally in the JDK to implement jimage/jrtfs access,42* but also compiled and delivered as part of the jrtfs.jar to support access43* to the jimage file provided by the shipped JDK by tools running on JDK 8.44*/45final class JrtDirectoryStream implements DirectoryStream<Path> {4647private final JrtPath dir;48private final DirectoryStream.Filter<? super Path> filter;49private boolean isClosed;50private Iterator<Path> itr;5152JrtDirectoryStream(JrtPath dir,53DirectoryStream.Filter<? super java.nio.file.Path> filter)54throws IOException55{56this.dir = dir;57if (!dir.jrtfs.isDirectory(dir, true)) { // sanity check58throw new NotDirectoryException(dir.toString());59}60this.filter = filter;61}6263@Override64public synchronized Iterator<Path> iterator() {65if (isClosed)66throw new ClosedDirectoryStreamException();67if (itr != null)68throw new IllegalStateException("Iterator has already been returned");69try {70itr = dir.jrtfs.iteratorOf(dir, filter);71} catch (IOException e) {72throw new IllegalStateException(e);73}74return new Iterator<Path>() {75@Override76public boolean hasNext() {77synchronized (JrtDirectoryStream.this) {78if (isClosed)79return false;80return itr.hasNext();81}82}8384@Override85public Path next() {86synchronized (JrtDirectoryStream.this) {87if (isClosed)88throw new NoSuchElementException();89return itr.next();90}91}92};93}9495@Override96public synchronized void close() throws IOException {97isClosed = true;98}99}100101102