Class FileSnapshot

java.lang.Object
org.apache.sshd.common.util.io.FileSnapshot

public class FileSnapshot extends Object
A snapshot of file metadata that can be used to determine whether a file has been modified since the last time it was read. Intended usage:
 FileSnapshot fileSnapshot = FileSnapshot.save(path);
 byte[] content = Files.readAllBytes(path);
 ...
 FileSnapshot newSnapshot = oldSnapshot.reload(path);
 if (newSnapshot == fileSnapshot) {
   // File was not modified
 } else {
   // File may have been modified
   fileSnapshot = newSnapshot;
   content = Files.readAllBytes(path);
 }
 

File modifications that occur quicker than the resolution of the system's "last modified" timestamp of a file cannot be detected reliably. This implementation assumes a worst-case filesystem timestamp resolution of 2 seconds (as it exists on FAT file systems). A snapshot taken within 2 seconds since the last modified time is considered "racily clean" only: the file will be considered potentially modified even if the metadata matches.

  • Field Details

    • UNKNOWN_SIZE

      public static final long UNKNOWN_SIZE
      A value indicating an unknown file size.
      See Also:
    • NO_FILE

      public static final FileSnapshot NO_FILE
      A FileSnapshot describing a non-existing file.
    • WORST_CASE_TIMESTAMP_RESOLUTION

      private static final Duration WORST_CASE_TIMESTAMP_RESOLUTION
    • lastModified

      private final FileTime lastModified
    • size

      private final long size
    • fileKey

      private final Object fileKey
    • snapTime

      private final Instant snapTime
  • Constructor Details

    • FileSnapshot

      protected FileSnapshot(Instant snapTime, FileTime lastModified, long size, Object fileKey)
      Creates a new FileSnapshot instance.
      Parameters:
      snapTime - the Instant the snapshot was taken
      lastModified - the "last modified" FileTime
      size - the file size
      fileKey - the file key
  • Method Details

    • getLastModified

      protected FileTime getLastModified()
      Retrieves the "last modified" time as recorded in this FileSnapshot.
      Returns:
      the FileTime, may be null
    • getSize

      protected long getSize()
      Retrieves the file size as recorded in this FileSnapshot.
      Returns:
      the size, UNKNOWN_SIZE for a snapshot of a non-existing file
    • getFileKey

      protected Object getFileKey()
      Retrieves the file key as recorded in this FileSnapshot.
      Returns:
      the file key, may be null
    • getTime

      protected Instant getTime()
      Retrieves the time this FileSnapshot was taken.
      Returns:
      the Instant the snapshot was taken, never null
    • save

      public static FileSnapshot save(Path file, LinkOption... options) throws IOException
      Creates a new FileSnapshot for the given path.
      Parameters:
      file - to take the snapshot of
      options - LinkOptions to use
      Returns:
      the FileSnapshot, never null
      Throws:
      IOException - if an I/O error occurs
    • reload

      public FileSnapshot reload(Path file, LinkOption... options) throws IOException
      Reload the FileSnapshot for the given file.
      Parameters:
      file - to take the snapshot of
      options - LinkOptions to use
      Returns:
      a FileSnapshot, never null; if == this, the file may be assumed unmodified
      Throws:
      IOException - if an I/O error occurs
    • mayBeRacilyClean

      protected boolean mayBeRacilyClean()
      Determines whether this FileSnapshot was taken within the file timestamp resolution of the file system after the last modified time of the file.
      Returns:
      true if so, false otherwise
    • same

      public boolean same(FileSnapshot other)
      Compares the snapshots' file metadata.
      Parameters:
      other - FileSnapshot to compare to (should be for the same Path)
      Returns:
      true if the two snapshots have the same file metadata, false otherwise