java.nio.file.DirectoryStream#iterator ( )源码实例Demo

下面列出了java.nio.file.DirectoryStream#iterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: helidon-build-tools   文件: SourcePath.java
private static List<SourcePath> doScan(File root, File dir) {
    List<SourcePath> sourcePaths = new ArrayList<>();
    DirectoryStream<Path> dirStream = null;
    try {
        dirStream = Files.newDirectoryStream(dir.toPath());
        Iterator<Path> it = dirStream.iterator();
        while (it.hasNext()) {
            Path next = it.next();
            if (Files.isDirectory(next)) {
                sourcePaths.addAll(doScan(root, next.toFile()));
            } else {
                sourcePaths.add(new SourcePath(root, next.toFile()));
            }
        }
    } catch (IOException ex) {
        if (dirStream != null) {
            try {
                dirStream.close();
            } catch (IOException e) { }
        }
    }
    return sort(sourcePaths);
}
 
源代码2 项目: db   文件: Version1to2.java
private void upgradeData() {
    logger.info("Migrating data to new storage format");
    long startTime = System.currentTimeMillis();
    try {
        DirectoryStream<Path> ds = Files.newDirectoryStream(FileSystems.getDefault().getPath(BSql.OLD_BSQL_BASE_FOLDER));
        Iterator<Path> iterator = ds.iterator();
        while (iterator.hasNext()) {
            Path path = iterator.next();
            if (Files.isDirectory(path)) {
                portApplication(path.getFileName().toString());
            }
        }
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(Version1to2.class.getName()).log(Level.SEVERE, null, ex);
    }
    long elapsedTime = System.currentTimeMillis() - startTime;
    logger.info("Data migration to new storage format successfully completed in " + elapsedTime + "ms");
}
 
源代码3 项目: jsr203-hadoop   文件: TestDirectoryStream.java
/**
 * According to Java documentation of NIO API, if we try to call
 * <code>next</code> on iterator of closed directory stream, there should be
 * an {@see NoSuchElementException} exception.
 * 
 * @throws IOException
 *           if we can't get the DirectoryStream
 */
@Test(expected = NoSuchElementException.class)
public void testNextOnClosedDirectoryStreamIterator() throws IOException {
  Path dir = Paths.get(clusterUri);
  // create file
  Path file = dir.resolve("foo");
  if (Files.exists(file)) {
    Files.delete(file);
  }
  Files.createFile(file);

  DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
  Iterator<Path> it = stream.iterator();
  stream.close();
  Assert.assertFalse(it.hasNext());
  // This one throws the exception
  it.next();
}
 
源代码4 项目: Singularity   文件: SingularityExecutorCleanup.java
private Iterator<Path> getUncompressedLogrotatedFileIterator(
  SingularityExecutorTaskDefinition taskDefinition
) {
  final Path serviceLogOutPath = taskDefinition.getServiceLogOutPath();
  final Path parent = serviceLogOutPath.getParent();
  if (parent == null) {
    throw new IllegalStateException(
      "Service log path " + serviceLogOutPath + " has no parent"
    );
  }
  final Path logrotateToPath = parent.resolve(
    executorConfiguration.getLogrotateToDirectory()
  );

  if (!logrotateToPath.toFile().exists() || !logrotateToPath.toFile().isDirectory()) {
    LOG.warn(
      "Skipping uncompressed logrotated file cleanup for {} -- {} does not exist or is not a directory (task sandbox was probably garbage collected by Mesos)",
      taskDefinition.getTaskId(),
      logrotateToPath
    );
    return Collections.emptyIterator();
  }

  try {
    DirectoryStream<Path> dirStream = Files.newDirectoryStream(
      logrotateToPath,
      String.format("%s-*", serviceLogOutPath.getFileName())
    );
    return dirStream.iterator();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
源代码5 项目: db   文件: OnDiskHashedIndex.java
@Override
public Iterator<String> cardinality(String app, String table, String column) throws OperationException {
    Path path = Paths.get(PathUtil.indexColumnFolder(app, table, column));
    Iterator<String> iterator;
    try {
        DirectoryStream directoryStream = Files.newDirectoryStream(path);
        iterator = new Iterator<String>() {
            Iterator<Path> innerIterator = directoryStream.iterator();

            @Override
            public boolean hasNext() {
                boolean hasNext = innerIterator.hasNext();
                if (!hasNext) {
                    try {
                        directoryStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(OnDiskHashedIndex.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                return hasNext;
            }

            @Override
            public String next() {
                return innerIterator.next().getFileName().toString();
            }

            @Override
            public void remove() {
                innerIterator.remove();
            }
        };
    } catch (IOException ex) {
        return null;
    }

    return iterator;
}
 
源代码6 项目: openjdk-jdk8u   文件: TestDumpOnExit.java
private static Path findJFRFileInCurrentDirectory() {
    try {
        DirectoryStream<Path> ds = Files.newDirectoryStream(Paths.get("."), "*pid-*.jfr");
        Iterator<Path> pathIterator = ds.iterator();
        if (!pathIterator.hasNext()) {
            throw new RuntimeException("Could not find jfr file in current directory");
        }
        return pathIterator.next();
    } catch (IOException e) {
        throw new RuntimeException("Could not list jfr file in current directory");
    }
}
 
源代码7 项目: ats-framework   文件: LocalFileSystemOperations.java
/**
 * Deletes any contents that the directory might have, both files and folders; the directory
 * itself is not deleted
 *
 * @param directoryName the target directory name
 * @throws FileSystemOperationException if the parameter is not a directory or does not exist
 */
private void purgeContents(
        String directoryName ) {

    File file = new File(directoryName);

    boolean exists = checkFileExistence(file, false);

    if (exists) {
        if (file.isDirectory()) {
            DirectoryStream<Path> dirStream = null;
            try {
                dirStream = Files.newDirectoryStream(file.toPath());
                Iterator<Path> it = dirStream.iterator();
                while (it.hasNext()) {
                    Path path = it.next();
                    deleteRecursively(path.toFile());
                }
            } catch (Exception e) {
                throw new FileSystemOperationException("Could not purge contents of directory '" + directoryName
                                                       + "'", e);
            } finally {
                IoUtils.closeStream(dirStream);
            }
        } else {
            throw new FileSystemOperationException(directoryName + " is not a directory! ");
        }
    }
}
 
源代码8 项目: jsch-nio   文件: UnixSshFileSystemTest.java
@Test
public void testDirectoryStreamEmptyDir() throws IOException {
    final String root = UUID.randomUUID().toString();
    Path rootPath = Paths.get( filesystemPath, root );

    // create test dir
    assertFalse( Files.exists( rootPath ) );
    Files.createDirectories( rootPath );
    assertTrue( Files.exists( rootPath ) );
    assertTrue( Files.isDirectory( rootPath ) );

    // test dirstream
    DirectoryStream<Path> ds = Files.newDirectoryStream(
            FileSystems.getFileSystem( uri ).getPath( root ) );
    try {
        Iterator<Path> iter = ds.iterator();
        assertFalse( iter.hasNext() );
        try {
            iter.next();
            fail( "expected an exception" );
        }
        catch ( NoSuchElementException e ) {
            // pass
        }
    }
    finally {
        ds.close();
    }
}
 
源代码9 项目: CloverETL-Engine   文件: ArchiveDirectoryStream.java
/**
 * 
 */
protected ArchiveDirectoryStream(DirectoryStream<Input> parent, String glob) {
	this.parentStream = parent;
	this.parent = parent.iterator();
	if (glob == null) {
		glob = "";
	}
	this.glob = glob;
	boolean containsWildcard = (glob.contains("?") || glob.contains("*"));
	pattern = containsWildcard ? Pattern.compile(glob.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\.", "\\\\.").replaceAll("\\?", "\\.").replaceAll("\\*", ".*")) : null; 
}
 
源代码10 项目: dragonwell8_jdk   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码11 项目: openjdk-8   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码12 项目: TencentKona-8   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码13 项目: jdk8u60   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码14 项目: mycore   文件: MCRDirectoryStream.java
SimpleDirectoryIterator(MCRPath dir, DirectoryStream<Path> baseStream) {
    this.baseIterator = baseStream.iterator();
    this.dir = dir;
}
 
源代码15 项目: jdk8u_jdk   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码16 项目: openjdk-jdk8u   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码17 项目: CloverETL-Engine   文件: Wildcards.java
/**
 * @param parent
 */
public CompoundDirectoryStream(DirectoryStream<DirectoryStream<T>> parent) {
	this.parent = parent;
	this.streamIterator = parent.iterator();
}
 
源代码18 项目: openjdk-jdk8u-backup   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码19 项目: jdk8u-jdk   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}
 
源代码20 项目: openjdk-8-source   文件: FaultyFileSystem.java
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
    return new DirectoryStream<Path>() {
        @Override
        public Iterator<Path> iterator() {
            final Iterator<Path> itr = stream.iterator();
            return new Iterator<Path>() {
                private Path next = null;
                @Override
                public boolean hasNext() {
                    if (next == null) {
                        if (itr.hasNext()) {
                            next = itr.next();
                        } else {
                            return false;
                        }
                    }
                    if (next != null) {
                        try {
                            triggerEx(next, "DirectoryIteratorException");
                        } catch (IOException ioe) {
                            throw new DirectoryIteratorException(ioe);
                        } catch (SecurityException se) {
                            // ??? Does DS throw SecurityException during iteration?
                            next = null;
                            return hasNext();
                        }
                    }
                    return (next != null);
                }
                @Override
                public Path next() {
                    try {
                        if (next != null || hasNext()) {
                            return new PassThroughFileSystem.PassThroughPath(delegate, next);
                        } else {
                            throw new NoSuchElementException();
                        }
                    } finally {
                        next = null;
                    }
                }

                @Override
                public void remove() {
                    itr.remove();
                }
            };
        }
        @Override
        public void close() throws IOException {
            stream.close();
        }
    };
}