Java Code Examples for java.nio.file.DirectoryStream#iterator()

The following examples show how to use java.nio.file.DirectoryStream#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SourcePath.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: Version1to2.java    From db with GNU Affero General Public License v3.0 6 votes vote down vote up
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");
}
 
Example 3
Source File: TestDirectoryStream.java    From jsr203-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: ArchiveDirectoryStream.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 
 */
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; 
}
 
Example 5
Source File: SingularityExecutorCleanup.java    From Singularity with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 6
Source File: UnixSshFileSystemTest.java    From jsch-nio with MIT License 5 votes vote down vote up
@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();
    }
}
 
Example 7
Source File: OnDiskHashedIndex.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: TestDumpOnExit.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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");
    }
}
 
Example 9
Source File: LocalFileSystemOperations.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * 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! ");
        }
    }
}
 
Example 10
Source File: FaultyFileSystem.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 11
Source File: FaultyFileSystem.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 12
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 13
Source File: FaultyFileSystem.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 14
Source File: Wildcards.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * @param parent
 */
public CompoundDirectoryStream(DirectoryStream<DirectoryStream<T>> parent) {
	this.parent = parent;
	this.streamIterator = parent.iterator();
}
 
Example 15
Source File: FaultyFileSystem.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 16
Source File: FaultyFileSystem.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 17
Source File: MCRDirectoryStream.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
SimpleDirectoryIterator(MCRPath dir, DirectoryStream<Path> baseStream) {
    this.baseIterator = baseStream.iterator();
    this.dir = dir;
}
 
Example 18
Source File: FaultyFileSystem.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 19
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}
 
Example 20
Source File: FaultyFileSystem.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    };
}