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;
}
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.");
}
}
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;
}
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);
}
}
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;
}
Aggregations