下面列出了java.nio.file.DirectoryStream#Filter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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;
}
/**
* 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);
}
/**
* 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;
}
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());
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
throws IOException
{
triggerEx(dir, "newDirectoryStream");
return wrap(Files.newDirectoryStream(unwrap(dir), filter));
}
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());
}
@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();
}
}
@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();
}
}
@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);
}
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;
}
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;
}
/**
* 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;
}
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);
}
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());
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
throws IOException
{
triggerEx(dir, "newDirectoryStream");
return wrap(Files.newDirectoryStream(unwrap(dir), filter));
}
@Override
public DirectoryStream<Path> newDirectoryStream(
Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir,
DirectoryStream.Filter<? super Path> filter) throws IOException {
return mantle(Files.newDirectoryStream(
EncryptedFileSystem.dismantle(dir), filter));
}
/**
* 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);
}
}
/**
* 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;