Package org.apache.sshd.common.util.io
Class FileSnapshot
java.lang.Object
org.apache.sshd.common.util.io.FileSnapshot
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final Objectprivate final FileTimestatic final FileSnapshotAFileSnapshotdescribing a non-existing file.private final longprivate final Instantstatic final longA value indicating an unknown file size.private static final Duration -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFileSnapshot(Instant snapTime, FileTime lastModified, long size, Object fileKey) Creates a newFileSnapshotinstance. -
Method Summary
Modifier and TypeMethodDescriptionprotected ObjectRetrieves the file key as recorded in thisFileSnapshot.protected FileTimeRetrieves the "last modified" time as recorded in thisFileSnapshot.protected longgetSize()Retrieves the file size as recorded in thisFileSnapshot.protected InstantgetTime()Retrieves the time thisFileSnapshotwas taken.protected booleanDetermines whether thisFileSnapshotwas taken within the file timestamp resolution of the file system after the last modified time of the file.reload(Path file, LinkOption... options) Reload theFileSnapshotfor the given file.booleansame(FileSnapshot other) Compares the snapshots' file metadata.static FileSnapshotsave(Path file, LinkOption... options) Creates a newFileSnapshotfor the given path.
-
Field Details
-
UNKNOWN_SIZE
public static final long UNKNOWN_SIZEA value indicating an unknown file size.- See Also:
-
NO_FILE
AFileSnapshotdescribing a non-existing file. -
WORST_CASE_TIMESTAMP_RESOLUTION
-
lastModified
-
size
private final long size -
fileKey
-
snapTime
-
-
Constructor Details
-
FileSnapshot
Creates a newFileSnapshotinstance.
-
-
Method Details
-
getLastModified
Retrieves the "last modified" time as recorded in thisFileSnapshot.- Returns:
- the
FileTime, may benull
-
getSize
protected long getSize()Retrieves the file size as recorded in thisFileSnapshot.- Returns:
- the size,
UNKNOWN_SIZEfor a snapshot of a non-existing file
-
getFileKey
Retrieves the file key as recorded in thisFileSnapshot.- Returns:
- the file key, may be
null
-
getTime
Retrieves the time thisFileSnapshotwas taken.- Returns:
- the
Instantthe snapshot was taken, nevernull
-
save
Creates a newFileSnapshotfor the given path.- Parameters:
file- to take the snapshot ofoptions-LinkOptions to use- Returns:
- the
FileSnapshot, nevernull - Throws:
IOException- if an I/O error occurs
-
reload
Reload theFileSnapshotfor the given file.- Parameters:
file- to take the snapshot ofoptions-LinkOptions to use- Returns:
- a
FileSnapshot, nevernull; if== this, the file may be assumed unmodified - Throws:
IOException- if an I/O error occurs
-
mayBeRacilyClean
protected boolean mayBeRacilyClean()Determines whether thisFileSnapshotwas taken within the file timestamp resolution of the file system after the last modified time of the file.- Returns:
trueif so,falseotherwise
-
same
Compares the snapshots' file metadata.- Parameters:
other-FileSnapshotto compare to (should be for the samePath)- Returns:
trueif the two snapshots have the same file metadata,falseotherwise
-