Search in sources :

Example 21 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project camel by apache.

the class BOMResolver method canUseCache.

private boolean canUseCache() throws IOException {
    if (CACHE_FILE.exists()) {
        BasicFileAttributes attr = Files.readAttributes(CACHE_FILE.toPath(), BasicFileAttributes.class);
        FileTime fileTime = attr != null ? attr.creationTime() : null;
        Long time = fileTime != null ? fileTime.toMillis() : null;
        // Update the cache every day
        return time != null && time.compareTo(System.currentTimeMillis() - 1000 * 60 * 60 * 24) > 0;
    }
    return false;
}
Also used : FileTime(java.nio.file.attribute.FileTime) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 22 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project wire by square.

the class ParsingTester method main.

public static void main(String... args) throws IOException {
    final AtomicLong total = new AtomicLong();
    final AtomicLong failed = new AtomicLong();
    Files.walkFileTree(ROOT, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().endsWith(".proto")) {
                total.incrementAndGet();
                String data = new String(Files.readAllBytes(file), UTF_8);
                Location location = Location.get(ROOT.toString(), file.toString());
                try {
                    ProtoParser.parse(location, data);
                } catch (Exception e) {
                    e.printStackTrace();
                    failed.incrementAndGet();
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
    System.out.println("\nTotal: " + total.get() + "  Failed: " + failed.get());
    if (failed.get() == 0) {
        new SchemaLoader().addSource(ROOT).load();
        System.out.println("All files linked successfully.");
    }
}
Also used : Path(java.nio.file.Path) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) IOException(java.io.IOException)

Example 23 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project bazel by bazelbuild.

the class JavaIoFileSystem method stat.

/**
   * Returns the status of a file. See {@link Path#stat(Symlinks)} for
   * specification.
   *
   * <p>The default implementation of this method is a "lazy" one, based on
   * other accessor methods such as {@link #isFile}, etc. Subclasses may provide
   * more efficient specializations. However, we still try to follow Unix-like
   * semantics of failing fast in case of non-existent files (or in case of
   * permission issues).
   */
@Override
protected FileStatus stat(final Path path, final boolean followSymlinks) throws IOException {
    File file = getIoFile(path);
    final BasicFileAttributes attributes;
    try {
        attributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class, linkOpts(followSymlinks));
    } catch (java.nio.file.FileSystemException e) {
        throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
    }
    FileStatus status = new FileStatus() {

        @Override
        public boolean isFile() {
            return attributes.isRegularFile() || isSpecialFile();
        }

        @Override
        public boolean isSpecialFile() {
            return attributes.isOther();
        }

        @Override
        public boolean isDirectory() {
            return attributes.isDirectory();
        }

        @Override
        public boolean isSymbolicLink() {
            return attributes.isSymbolicLink();
        }

        @Override
        public long getSize() throws IOException {
            return attributes.size();
        }

        @Override
        public long getLastModifiedTime() throws IOException {
            return attributes.lastModifiedTime().toMillis();
        }

        @Override
        public long getLastChangeTime() {
            // This is the best we can do with Java NIO...
            return attributes.lastModifiedTime().toMillis();
        }

        @Override
        public long getNodeId() {
            // TODO(bazel-team): Consider making use of attributes.fileKey().
            return -1;
        }
    };
    return status;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 24 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project bazel by bazelbuild.

the class SimpleJavaLibraryBuilder method setUpSourceJars.

/**
   * Extracts the all source jars from the build request into the temporary directory specified in
   * the build request. Empties the temporary directory, if it exists.
   */
private void setUpSourceJars(JavaLibraryBuildRequest build) throws IOException {
    String sourcesDir = build.getTempDir();
    Path sourceDirFile = Paths.get(sourcesDir);
    if (Files.exists(sourceDirFile)) {
        cleanupDirectory(sourceDirFile);
    }
    if (build.getSourceJars().isEmpty()) {
        return;
    }
    final ByteArrayOutputStream protobufMetadataBuffer = new ByteArrayOutputStream();
    for (String sourceJar : build.getSourceJars()) {
        for (Path root : getJarFileSystem(Paths.get(sourceJar)).getRootDirectories()) {
            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                    String fileName = path.getFileName().toString();
                    if (fileName.endsWith(".java")) {
                        build.getSourceFiles().add(path);
                    } else if (fileName.equals(PROTOBUF_META_NAME)) {
                        Files.copy(path, protobufMetadataBuffer);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
    Path output = Paths.get(build.getClassDir(), PROTOBUF_META_NAME);
    if (protobufMetadataBuffer.size() > 0) {
        try (OutputStream outputStream = Files.newOutputStream(output)) {
            protobufMetadataBuffer.writeTo(outputStream);
        }
    } else if (Files.exists(output)) {
        // Delete stalled meta file.
        Files.delete(output);
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileVisitResult(java.nio.file.FileVisitResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 25 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project musiccabinet by hakko.

the class DirectoryBrowserService method getContent.

private DirectoryContent getContent(Directory dir) {
    Set<File> foundFiles = new HashSet<>();
    Set<String> foundSubDirs = new HashSet<>();
    DirectoryContent content = new DirectoryContent(dir.getPath(), foundSubDirs, foundFiles);
    Path path = Paths.get(dir.getPath());
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
        for (Path file : stream) {
            BasicFileAttributeView view = getFileAttributeView(file, BasicFileAttributeView.class);
            BasicFileAttributes attr = view.readAttributes();
            if (attr.isDirectory()) {
                foundSubDirs.add(file.toAbsolutePath().toString());
            } else if (attr.isRegularFile()) {
                foundFiles.add(new File(file, attr));
            }
        }
    } catch (IOException | DirectoryIteratorException e) {
        throw new ApplicationContextException("Couldn't read " + dir.getPath(), e);
    }
    return content;
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) ApplicationContextException(org.springframework.context.ApplicationContextException) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) DirectoryContent(com.github.hakko.musiccabinet.domain.model.aggr.DirectoryContent) File(com.github.hakko.musiccabinet.domain.model.library.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) HashSet(java.util.HashSet)

Aggregations

BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)119 Path (java.nio.file.Path)93 IOException (java.io.IOException)87 FileVisitResult (java.nio.file.FileVisitResult)66 File (java.io.File)18 Test (org.junit.Test)13 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 FileNotFoundException (java.io.FileNotFoundException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)5 SourcePath (com.facebook.buck.rules.SourcePath)4 ImmutableList (com.google.common.collect.ImmutableList)4 OutputStream (java.io.OutputStream)4 URI (java.net.URI)4 FileSystem (java.nio.file.FileSystem)4 FileVisitor (java.nio.file.FileVisitor)4