Java Code Examples for java.nio.file.attribute.BasicFileAttributes.isRegularFile()

The following are Jave code examples for showing how to use isRegularFile() of the java.nio.file.attribute.BasicFileAttributes class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: JavaRushTasks   File: SearchFileVisitor.java   View Source Code Vote up 6 votes
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    if (!attrs.isRegularFile()) return CONTINUE;

    if (partOfNameCheck && file.getFileName().toString().indexOf(this.partOfName) == -1)
        return CONTINUE;

    if (minSizeCheck && attrs.size() < minSize)
        return CONTINUE;

    if (maxSizeCheck && attrs.size() > maxSize)
        return CONTINUE;

    if (partOfContentCheck && new String(Files.readAllBytes(file)).indexOf(partOfContent) == -1)
        return CONTINUE;

    foundFiles.add(file);

    return CONTINUE;
}
 
Example 2
Project: elasticsearch_my   File: FsBlobContainer.java   View Source Code Vote up 6 votes
@Override
public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException {
    // If we get duplicate files we should just take the last entry
    Map<String, BlobMetaData> builder = new HashMap<>();

    blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) {
        for (Path file : stream) {
            final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
            if (attrs.isRegularFile()) {
                builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size()));
            }
        }
    }
    return unmodifiableMap(builder);
}
 
Example 3
Project: Elasticsearch   File: FsBlobContainer.java   View Source Code Vote up 6 votes
@Override
public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException {
    // using MapBuilder and not ImmutableMap.Builder as it seems like File#listFiles might return duplicate files!
    MapBuilder<String, BlobMetaData> builder = MapBuilder.newMapBuilder();

    blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) {
        for (Path file : stream) {
            final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
            if (attrs.isRegularFile()) {
                builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size()));
            }
        }
    }
    return builder.immutableMap();
}
 
Example 4
Project: openjdk-jdk10   File: ExplodedImage.java   View Source Code Vote up 6 votes
Node findModulesNode(String str) {
    PathNode node = nodes.get(str);
    if (node != null) {
        return node;
    }
    // lazily created "/modules/xyz/abc/" Node
    // This is mapped to default file system path "<JDK_MODULES_DIR>/xyz/abc"
    Path p = underlyingPath(str);
    if (p != null) {
        try {
            BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class);
            if (attrs.isRegularFile()) {
                Path f = p.getFileName();
                if (f.toString().startsWith("_the."))
                    return null;
            }
            node = new PathNode(str, p, attrs);
            nodes.put(str, node);
            return node;
        } catch (IOException x) {
            // does not exists or unable to determine
        }
    }
    return null;
}
 
Example 5
Project: truevfs   File: FileNode.java   View Source Code Vote up 5 votes
@Override
public final BitField<Type> getTypes() {
    try {
        final BasicFileAttributes attr = readBasicFileAttributes();
        if      (attr.isRegularFile())  return FILE_TYPE;
        else if (attr.isDirectory())    return DIRECTORY_TYPE;
        else if (attr.isSymbolicLink()) return SYMLINK_TYPE;
        else if (attr.isOther())        return SPECIAL_TYPE;
    } catch (IOException ignore) {
        // This doesn't exist or may be inaccessible. In either case...
    }
    return NO_TYPES;
}
 
Example 6
Project: athena   File: Tools.java   View Source Code Vote up 5 votes
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
        throws IOException {
    if (attributes.isRegularFile()) {
        delete(file);
    }
    return FileVisitResult.CONTINUE;
}
 
Example 7
Project: openjdk-jdk10   File: LauncherHelper.java   View Source Code Vote up 5 votes
/**
 * Scan an element on a module path. The element is a directory
 * of modules, an exploded module, or a JAR file.
 */
void scan(Path entry) {
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(entry, BasicFileAttributes.class);
    } catch (NoSuchFileException ignore) {
        return;
    } catch (IOException ioe) {
        ostream.println(entry + " " + ioe);
        errorFound = true;
        return;
    }

    String fn = entry.getFileName().toString();
    if (attrs.isRegularFile() && fn.endsWith(".jar")) {
        // JAR file, explicit or automatic module
        scanModule(entry).ifPresent(this::process);
    } else if (attrs.isDirectory()) {
        Path mi = entry.resolve(MODULE_INFO);
        if (Files.exists(mi)) {
            // exploded module
            scanModule(entry).ifPresent(this::process);
        } else {
            // directory of modules
            scanDirectory(entry);
        }
    }
}
 
Example 8
Project: openjdk-jdk10   File: Basic.java   View Source Code Vote up 5 votes
@Override
public FileVisitResult visitFile(Path file,
                                 BasicFileAttributes attrs)
{
    indent();
    System.out.print(file.getFileName());
    if (attrs.isRegularFile())
        System.out.format("%n%s%n", attrs);

    System.out.println();
    return FileVisitResult.CONTINUE;
}
 
Example 9
Project: klask-io   File: FileSystemVisitorCrawler.java   View Source Code Vote up 5 votes
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    log.trace("visitFile {}",file);
    if(attrs.isRegularFile() && !this.crawler.isFileInExclusion(file)) {
        this.indexedFiles++;
        this.crawler.addFile(file);
    }
    else{
        log.trace("exclude file {}",file);
    }
    return FileVisitResult.CONTINUE;

}
 
Example 10
Project: java-tutorial   File: PrintFiles.java   View Source Code Vote up 5 votes
@Override
public FileVisitResult visitFile(Path file,
                                 BasicFileAttributes attr) {
    if (attr.isSymbolicLink()) {
        System.out.format("Symbolic link: %s ", file);
    } else if (attr.isRegularFile()) {
        System.out.format("Regular file: %s ", file);
    } else {
        System.out.format("Other: %s ", file);
    }
    System.out.println("(" + attr.size() + "bytes)");
    return CONTINUE;
}
 
Example 11
Project: Elasticsearch   File: HttpServer.java   View Source Code Vote up 4 votes
void handlePluginSite(HttpRequest request, HttpChannel channel) throws IOException {
    if (disableSites) {
        channel.sendResponse(new BytesRestResponse(FORBIDDEN));
        return;
    }
    if (request.method() == RestRequest.Method.OPTIONS) {
        // when we have OPTIONS request, simply send OK by default (with the Access Control Origin header which gets automatically added)
        channel.sendResponse(new BytesRestResponse(OK));
        return;
    }
    if (request.method() != RestRequest.Method.GET) {
        channel.sendResponse(new BytesRestResponse(FORBIDDEN));
        return;
    }
    // TODO for a "/_plugin" endpoint, we should have a page that lists all the plugins?

    String path = request.rawPath().substring("/_plugin/".length());
    int i1 = path.indexOf('/');
    String pluginName;
    String sitePath;
    if (i1 == -1) {
        pluginName = path;
        sitePath = null;
        // If a trailing / is missing, we redirect to the right page #2654
        String redirectUrl = request.rawPath() + "/";
        BytesRestResponse restResponse = new BytesRestResponse(RestStatus.MOVED_PERMANENTLY, "text/html", "<head><meta http-equiv=\"refresh\" content=\"0; URL=" + redirectUrl + "\"></head>");
        restResponse.addHeader("Location", redirectUrl);
        channel.sendResponse(restResponse);
        return;
    } else {
        pluginName = path.substring(0, i1);
        sitePath = path.substring(i1 + 1);
    }

    // we default to index.html, or what the plugin provides (as a unix-style path)
    // this is a relative path under _site configured by the plugin.
    if (sitePath.length() == 0) {
        sitePath = "index.html";
    } else {
        // remove extraneous leading slashes, its not an absolute path.
        while (sitePath.length() > 0 && sitePath.charAt(0) == '/') {
            sitePath = sitePath.substring(1);
        }
    }
    final Path siteFile = environment.pluginsFile().resolve(pluginName).resolve("_site");

    final String separator = siteFile.getFileSystem().getSeparator();
    // Convert file separators.
    sitePath = sitePath.replace("/", separator);
    
    Path file = siteFile.resolve(sitePath);

    // return not found instead of forbidden to prevent malicious requests to find out if files exist or dont exist
    if (!Files.exists(file) || FileSystemUtils.isHidden(file) || !file.toAbsolutePath().normalize().startsWith(siteFile.toAbsolutePath().normalize())) {
        channel.sendResponse(new BytesRestResponse(NOT_FOUND));
        return;
    }

    BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);
    if (!attributes.isRegularFile()) {
        // If it's not a dir, we send a 403
        if (!attributes.isDirectory()) {
            channel.sendResponse(new BytesRestResponse(FORBIDDEN));
            return;
        }
        // We don't serve dir but if index.html exists in dir we should serve it
        file = file.resolve("index.html");
        if (!Files.exists(file) || FileSystemUtils.isHidden(file) || !Files.isRegularFile(file)) {
            channel.sendResponse(new BytesRestResponse(FORBIDDEN));
            return;
        }
    }

    try {
        byte[] data = Files.readAllBytes(file);
        channel.sendResponse(new BytesRestResponse(OK, guessMimeType(sitePath), data));
    } catch (IOException e) {
        channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR));
    }
}
 
Example 12
Project: openjdk-jdk10   File: ModulePath.java   View Source Code Vote up 4 votes
/**
 * Reads a packaged or exploded module, returning a {@code ModuleReference}
 * to the module. Returns {@code null} if the entry is not recognized.
 *
 * @throws IOException if an I/O error occurs
 * @throws FindException if an error occurs parsing its module descriptor
 */
private ModuleReference readModule(Path entry, BasicFileAttributes attrs)
    throws IOException
{
    try {

        // exploded module
        if (attrs.isDirectory()) {
            return readExplodedModule(entry); // may return null
        }

        // JAR or JMOD file
        if (attrs.isRegularFile()) {
            String fn = entry.getFileName().toString();
            boolean isDefaultFileSystem = isDefaultFileSystem(entry);

            // JAR file
            if (fn.endsWith(".jar")) {
                if (isDefaultFileSystem) {
                    return readJar(entry);
                } else {
                    // the JAR file is in a custom file system so
                    // need to copy it to the local file system
                    Path tmpdir = Files.createTempDirectory("mlib");
                    Path target = Files.copy(entry, tmpdir.resolve(fn));
                    return readJar(target);
                }
            }

            // JMOD file
            if (isDefaultFileSystem && isLinkPhase && fn.endsWith(".jmod")) {
                return readJMod(entry);
            }
        }

        return null;

    } catch (InvalidModuleDescriptorException e) {
        throw new FindException("Error reading module: " + entry, e);
    }
}