use of java.nio.file.attribute.FileTime in project jetty.project by eclipse.
the class FS method touch.
public static void touch(Path path) throws IOException {
FileTime now = FileTime.fromMillis(System.currentTimeMillis());
Files.setLastModifiedTime(path, now);
}
use of java.nio.file.attribute.FileTime in project buck by facebook.
the class PathListing method listMatchingPathsWithFilters.
/**
* Lists paths in descending modified time order,
* excluding any paths which bring the number of files over {@code maxNumPaths}
* or over {@code totalSizeFilter} bytes in size.
*/
public static ImmutableSortedSet<Path> listMatchingPathsWithFilters(Path pathToGlob, String globPattern, PathModifiedTimeFetcher pathModifiedTimeFetcher, FilterMode filterMode, Optional<Integer> maxPathsFilter, Optional<Long> totalSizeFilter) throws IOException {
// Fetch the modification time of each path and build a map of
// (path => modification time) pairs.
ImmutableMap.Builder<Path, FileTime> pathFileTimesBuilder = ImmutableMap.builder();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pathToGlob, globPattern)) {
for (Path path : stream) {
try {
pathFileTimesBuilder.put(path, pathModifiedTimeFetcher.getLastModifiedTime(path));
} catch (NoSuchFileException e) {
// Ignore the path.
continue;
}
}
}
ImmutableMap<Path, FileTime> pathFileTimes = pathFileTimesBuilder.build();
ImmutableSortedSet<Path> paths = ImmutableSortedSet.copyOf(Ordering.natural().onResultOf(Functions.forMap(pathFileTimes)).compound(Ordering.natural()).reverse(), pathFileTimes.keySet());
paths = applyNumPathsFilter(paths, filterMode, maxPathsFilter);
paths = applyTotalSizeFilter(paths, filterMode, totalSizeFilter);
return paths;
}
use of java.nio.file.attribute.FileTime in project buck by facebook.
the class MoreFiles method sortFilesByAccessTime.
/**
* Does an in-place sort of the specified {@code files} array. Most recently accessed files will
* be at the front of the array when sorted.
*/
public static void sortFilesByAccessTime(File[] files) {
FileAccessedEntry[] fileAccessedEntries = new FileAccessedEntry[files.length];
for (int i = 0; i < files.length; ++i) {
FileTime lastAccess;
try {
lastAccess = Files.readAttributes(files[i].toPath(), BasicFileAttributes.class).lastAccessTime();
} catch (IOException e) {
lastAccess = FileTime.fromMillis(files[i].lastModified());
}
fileAccessedEntries[i] = new FileAccessedEntry(files[i], lastAccess);
}
Arrays.sort(fileAccessedEntries, SORT_BY_LAST_ACCESSED_TIME_DESC);
for (int i = 0; i < files.length; i++) {
files[i] = fileAccessedEntries[i].getFile();
}
}
use of java.nio.file.attribute.FileTime in project buck by facebook.
the class ProjectIntegrationTest method testBuckProjectDoesNotCauseUnnecessaryWrites.
@Test
public void testBuckProjectDoesNotCauseUnnecessaryWrites() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "project_with_root_iml_already_present", temporaryFolder);
workspace.setUp();
// We're gonna party like it's 1970!
FileTime lastModified = FileTime.fromMillis(0);
Path path = temporaryFolder.getRoot().resolve("root.iml");
Files.setLastModifiedTime(path, lastModified);
assertEquals(lastModified, Files.getLastModifiedTime(path));
ProcessResult result = workspace.runBuckCommand("project", "--deprecated-ij-generation");
result.assertSuccess("buck project should exit cleanly");
assertEquals(lastModified, Files.getLastModifiedTime(path));
workspace.verify();
}
use of java.nio.file.attribute.FileTime in project jimfs by google.
the class UrlTest method headers.
@Test
public void headers() throws IOException {
byte[] bytes = { 1, 2, 3 };
Files.write(path, bytes);
FileTime lastModified = Files.getLastModifiedTime(path);
URL url = path.toUri().toURL();
URLConnection conn = url.openConnection();
// read header fields directly
assertThat(conn.getHeaderFields()).containsEntry("content-length", ImmutableList.of("3"));
assertThat(conn.getHeaderFields()).containsEntry("content-type", ImmutableList.of("application/octet-stream"));
if (lastModified != null) {
assertThat(conn.getHeaderFields()).containsKey("last-modified");
assertThat(conn.getHeaderFields()).hasSize(3);
} else {
assertThat(conn.getHeaderFields()).hasSize(2);
}
// use the specific methods for reading the expected headers
assertThat(conn.getContentLengthLong()).isEqualTo(Files.size(path));
assertThat(conn.getContentType()).isEqualTo("application/octet-stream");
if (lastModified != null) {
// The HTTP date format does not include milliseconds, which means that the last modified time
// returned from the connection may not be exactly the same as that of the file system itself.
// The difference should less than 1000ms though, and should never be greater.
long difference = lastModified.toMillis() - conn.getLastModified();
assertThat(difference).isIn(Range.closedOpen(0L, 1000L));
} else {
assertThat(conn.getLastModified()).isEqualTo(0L);
}
}
Aggregations