Search in sources :

Example 21 with FileTime

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);
}
Also used : FileTime(java.nio.file.attribute.FileTime)

Example 22 with FileTime

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;
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) FileTime(java.nio.file.attribute.FileTime) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 23 with FileTime

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();
    }
}
Also used : FileTime(java.nio.file.attribute.FileTime) IOException(java.io.IOException)

Example 24 with FileTime

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();
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ProcessResult(com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult) FileTime(java.nio.file.attribute.FileTime) Test(org.junit.Test)

Example 25 with FileTime

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);
    }
}
Also used : FileTime(java.nio.file.attribute.FileTime) URL(java.net.URL) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Aggregations

FileTime (java.nio.file.attribute.FileTime)40 Path (java.nio.file.Path)11 File (java.io.File)7 Test (org.junit.Test)7 Test (org.testng.annotations.Test)7 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 IOException (java.io.IOException)5 SegmentMetadataImpl (com.linkedin.pinot.core.segment.index.SegmentMetadataImpl)4 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)3 ProcessResult (com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult)2 IndexSegment (com.linkedin.pinot.core.indexsegment.IndexSegment)2 SegmentV1V2ToV3FormatConverter (com.linkedin.pinot.core.segment.index.converter.SegmentV1V2ToV3FormatConverter)2 FileNotFoundException (java.io.FileNotFoundException)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 Date (java.util.Date)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1