Java Code Examples for java.nio.file.DirectoryStream#Filter

The following examples show how to use java.nio.file.DirectoryStream#Filter . 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: AbstractConfluenceDeployMojo.java    From maven-confluence-plugin with Apache License 2.0 6 votes vote down vote up
private DirectoryStream<Path> newDirectoryStream(Path attachmentPath, Site.Attachment attachment)
        throws IOException {

    if (StringUtils.isNotBlank(attachment.getName())) {
        return Files.newDirectoryStream(attachmentPath, attachment.getName());
    }

    final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
        @Override
        public boolean accept(Path entry) throws IOException {

            return !(Files.isDirectory(entry) || Files.isHidden(entry) || Files.isSymbolicLink(entry)
                    || (!Files.isReadable(entry)));
        }
    };
    return Files.newDirectoryStream(attachmentPath, filter);
}
 
Example 2
Source File: FileSystemView.java    From jimfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new directory stream for the directory located by the given path. The given {@code
 * basePathForStream} is that base path that the returned stream will use. This will be the same
 * as {@code dir} except for streams created relative to another secure stream.
 */
public DirectoryStream<Path> newDirectoryStream(
    JimfsPath dir,
    DirectoryStream.Filter<? super Path> filter,
    Set<? super LinkOption> options,
    JimfsPath basePathForStream)
    throws IOException {
  Directory file = (Directory) lookUpWithLock(dir, options).requireDirectory(dir).file();
  FileSystemView view = new FileSystemView(store, file, basePathForStream);
  JimfsSecureDirectoryStream stream = new JimfsSecureDirectoryStream(view, filter, state());
  return store.supportsFeature(Feature.SECURE_DIRECTORY_STREAM)
      ? stream
      : new DowngradedDirectoryStream(stream);
}
 
Example 3
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
    throws IOException
{
    triggerEx(dir, "newDirectoryStream");
    return wrap(Files.newDirectoryStream(unwrap(dir), filter));
}
 
Example 4
Source File: ZipDirectoryStream.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
ZipDirectoryStream(ZipPath zipPath,
                   DirectoryStream.Filter<? super java.nio.file.Path> filter)
    throws IOException
{
    this.zipfs = zipPath.getFileSystem();
    this.path = zipPath.getResolvedPath();
    this.filter = filter;
    // sanity check
    if (!zipfs.isDirectory(path))
        throw new NotDirectoryException(zipPath.toString());
}
 
Example 5
Source File: MCRDirectoryStream.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
static DirectoryStream<Path> getInstance(MCRDirectory dir, MCRPath path) throws IOException {
    DirectoryStream.Filter<Path> filter = (dir instanceof MCRFileCollection) ? MCRFileCollectionFilter.FILTER
        : AcceptAllFilter.FILTER;
    LOGGER.debug("Dir {}, class {}, filter {}", path, dir.getClass(), filter.getClass());
    DirectoryStream<Path> baseDirectoryStream = Files.newDirectoryStream(dir.getLocalPath(), filter);
    LOGGER.debug("baseStream {}", baseDirectoryStream.getClass());
    if (baseDirectoryStream instanceof java.nio.file.SecureDirectoryStream) {
        LOGGER.debug("Returning SecureDirectoryStream");
        return new SecureDirectoryStream(dir, path,
            (java.nio.file.SecureDirectoryStream<Path>) baseDirectoryStream);
    }
    return new SimpleDirectoryStream(path, baseDirectoryStream);
}
 
Example 6
Source File: Paths.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
/**
 * Expands the given directory path, collecting all of its descendant files that are accepted by
 * the filter.
 *
 * @param dir The directory to expand.
 * @param filter The filter to apply to the directory entries.
 * @return All of the files located under the directory accepted by the filter.
 * @throws IOException If an I/O error occurs.
 */
static List<Path> expandDir(Path dir, DirectoryStream.Filter<Path> filter) throws IOException {
  checkArgument(Files.isDirectory(dir), "%s is not a directory", dir);
  List<Path> paths = new LinkedList<>();
  try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
    for (Path path : stream) {
      if (Files.isDirectory(path)) {
        paths.addAll(expandDir(path, filter));
      } else {
        paths.add(path);
      }
    }
  }
  return paths;
}
 
Example 7
Source File: AbstractFilesystemTracker.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected final AnalysedDirectory getImportableDirectoriesInDirectory(ImportableItem directory, final int count)
{
    DirectoryStream.Filter<Path> filter = null;

	if (count != -1)
	{
        filter = new DirectoryStream.Filter<Path>()
        {
            private int i = count;

            @Override
            public boolean accept(Path entry) throws IOException
            {
                return Files.isDirectory(entry) && i-- > 0;
            }
        };
	}
	else
	{
        filter = new DirectoryStream.Filter<Path>()
        {
            @Override
            public boolean accept(Path entry) throws IOException
            {
                return Files.isDirectory(entry);
            }
        };
	}

    AnalysedDirectory analysedDirectory = directoryAnalyser.analyseDirectory(directory, filter);
    return analysedDirectory;
}
 
Example 8
Source File: DefaultProjectFilesystem.java    From buck with Apache License 2.0 5 votes vote down vote up
FileTreeWalker(
    Path root,
    Set<FileVisitOption> options,
    FileVisitor<Path> pathFileVisitor,
    DirectoryStream.Filter<? super Path> ignoreFilter) {
  this.followLinks = options.contains(FileVisitOption.FOLLOW_LINKS);
  this.visitor = pathFileVisitor;
  this.root = root;
  this.state = new ArrayDeque<>();
  this.ignoreFilter = ignoreFilter;
}
 
Example 9
Source File: DelegatingFileSystemProviderTest.java    From tessera with Apache License 2.0 5 votes vote down vote up
@Test
public void newDirectoryStream() throws IOException {
    final Path path = mock(Path.class);
    final DirectoryStream.Filter<Path> filter = mock(DirectoryStream.Filter.class);

    provider.newDirectoryStream(path, filter);

    verify(delegate).newDirectoryStream(path, filter);
}
 
Example 10
Source File: BackupCommitLogsOperation.java    From cassandra-backup with Apache License 2.0 5 votes vote down vote up
@Override
protected void run0() throws Exception {
    logger.info(request.toString());

    final FileLock fileLock = new GlobalLock(request.lockFile).waitForLock();

    try {
        // generate manifest (set of object keys and source files defining the upload)
        final Collection<ManifestEntry> manifest = new LinkedList<>(); // linked list to maintain order
        final Pattern pattern = Pattern.compile("CommitLog-\\d+-\\d+\\.log");
        final DirectoryStream.Filter<Path> filter = entry -> Files.isRegularFile(entry) && pattern.matcher(entry.getFileName().toString()).matches();

        final Path commitLogArchiveDirectory = resolveCommitLogsPath(request);
        final Path backupCommitLogRootKey = Paths.get(CASSANDRA_COMMIT_LOGS);

        try (final DirectoryStream<Path> commitLogs = Files.newDirectoryStream(commitLogArchiveDirectory, filter);
            final Backuper backuper = backuperFactoryMap.get(request.storageLocation.storageProvider).createCommitLogBackuper(request);
            final BucketService bucketService = bucketServiceMap.get(request.storageLocation.storageProvider).createBucketService(request)) {

            bucketService.createIfMissing(request.storageLocation.bucket);

            for (final Path commitLog : commitLogs) {
                // Append file modified date so we have some idea of the time range this commitlog covers

                // millisecond precision, on *nix, it trims milliseconds and returns "000" instead
                // when using File.lastModified
                long commitLogLastModified = Files.getLastModifiedTime(commitLog.toFile().toPath()).toMillis();

                final Path bucketKey = backupCommitLogRootKey.resolve(commitLog.getFileName().toString() + "." + commitLogLastModified);
                manifest.add(new ManifestEntry(bucketKey, commitLog, ManifestEntry.Type.FILE));
            }

            logger.debug("{} files in manifest for commitlog backup.", manifest.size());

            backuper.uploadOrFreshenFiles(this, manifest, new OperationProgressTracker(this, manifest.size()));
        }
    } finally {
        fileLock.release();
    }
}
 
Example 11
Source File: PeriodicFilesRollModeFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isFileRolled(LiveFile currentFile) throws IOException {
  DirectoryStream.Filter<Path> filter = new FileFilter(currentFile.getPath());
  try (DirectoryStream<Path> matches = Files.newDirectoryStream(currentFile.getPath().getParent(), filter)) {
    return matches.iterator().hasNext();
  }
}
 
Example 12
Source File: DefaultProjectFilesystem.java    From buck with Apache License 2.0 5 votes vote down vote up
void walkRelativeFileTree(
    Path pathRelativeToProjectRoot,
    EnumSet<FileVisitOption> visitOptions,
    FileVisitor<Path> fileVisitor,
    DirectoryStream.Filter<? super Path> ignoreFilter)
    throws IOException {
  Path rootPath = getPathForRelativePath(pathRelativeToProjectRoot);
  walkFileTreeWithPathMapping(
      rootPath, visitOptions, fileVisitor, ignoreFilter, path -> relativize(path).getPath());
}
 
Example 13
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
    throws IOException
{
    triggerEx(dir, "newDirectoryStream");
    return wrap(Files.newDirectoryStream(unwrap(dir), filter));
}
 
Example 14
Source File: ZipDirectoryStream.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
ZipDirectoryStream(ZipPath zipPath,
                   DirectoryStream.Filter<? super java.nio.file.Path> filter)
    throws IOException
{
    this.zipfs = zipPath.getFileSystem();
    this.path = zipPath.getResolvedPath();
    this.filter = filter;
    // sanity check
    if (!zipfs.isDirectory(path))
        throw new NotDirectoryException(zipPath.toString());
}
 
Example 15
Source File: SolrResourceLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to get the URLs of all paths under a given directory that match a filter
 *
 * @param libDir the root directory
 * @param filter the filter
 * @return all matching URLs
 * @throws IOException on error
 */
public static List<URL> getURLs(Path libDir, DirectoryStream.Filter<Path> filter) throws IOException {
  List<URL> urls = new ArrayList<>();
  try (DirectoryStream<Path> directory = Files.newDirectoryStream(libDir, filter)) {
    for (Path element : directory) {
      urls.add(element.toUri().normalize().toURL());
    }
  }
  return urls;
}
 
Example 16
Source File: JrtDirectoryStream.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
JrtDirectoryStream(JrtPath dir,
        DirectoryStream.Filter<? super java.nio.file.Path> filter)
        throws IOException
{
    this.dir = dir;
    if (!dir.jrtfs.isDirectory(dir, true)) {  // sanity check
        throw new NotDirectoryException(dir.toString());
    }
    this.filter = filter;
}
 
Example 17
Source File: SystemJimfsFileSystemProvider.java    From jimfs with Apache License 2.0 4 votes vote down vote up
@Override
public DirectoryStream<Path> newDirectoryStream(
    Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
  throw new UnsupportedOperationException();
}
 
Example 18
Source File: EncryptedFileSystemProvider.java    From encfs4j with Apache License 2.0 4 votes vote down vote up
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
		DirectoryStream.Filter<? super Path> filter) throws IOException {
	return mantle(Files.newDirectoryStream(
			EncryptedFileSystem.dismantle(dir), filter));
}
 
Example 19
Source File: FileSystemUtils.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an array of all files in the given directory matching.
 */
public static Path[] files(Path from, DirectoryStream.Filter<Path> filter) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(from, filter)) {
        return toArray(stream);
    }
}
 
Example 20
Source File: FileSystemProvider.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Opens a directory, returning a {@code DirectoryStream} to iterate over
 * the entries in the directory. This method works in exactly the manner
 * specified by the {@link
 * Files#newDirectoryStream(java.nio.file.Path, java.nio.file.DirectoryStream.Filter)}
 * method.
 *
 * @param   dir
 *          the path to the directory
 * @param   filter
 *          the directory stream filter
 *
 * @return  a new and open {@code DirectoryStream} object
 *
 * @throws  NotDirectoryException
 *          if the file could not otherwise be opened because it is not
 *          a directory <i>(optional specific exception)</i>
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 *          method is invoked to check read access to the directory.
 */
public abstract DirectoryStream<Path> newDirectoryStream(Path dir,
     DirectoryStream.Filter<? super Path> filter) throws IOException;