Package org.apache.sshd.common.util.io
Class FileSnapshot
- java.lang.Object
-
- org.apache.sshd.common.util.io.FileSnapshot
-
public class FileSnapshot extends java.lang.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 Summary
Fields Modifier and Type Field Description private java.lang.Object
fileKey
private java.nio.file.attribute.FileTime
lastModified
static FileSnapshot
NO_FILE
AFileSnapshot
describing a non-existing file.private long
size
private java.time.Instant
snapTime
static long
UNKNOWN_SIZE
A value indicating an unknown file size.private static java.time.Duration
WORST_CASE_TIMESTAMP_RESOLUTION
-
Constructor Summary
Constructors Modifier Constructor Description protected
FileSnapshot(java.time.Instant snapTime, java.nio.file.attribute.FileTime lastModified, long size, java.lang.Object fileKey)
Creates a newFileSnapshot
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Object
getFileKey()
Retrieves the file key as recorded in thisFileSnapshot
.protected java.nio.file.attribute.FileTime
getLastModified()
Retrieves the "last modified" time as recorded in thisFileSnapshot
.protected long
getSize()
Retrieves the file size as recorded in thisFileSnapshot
.protected java.time.Instant
getTime()
Retrieves the time thisFileSnapshot
was taken.protected boolean
mayBeRacilyClean()
Determines whether thisFileSnapshot
was taken within the file timestamp resolution of the file system after the last modified time of the file.FileSnapshot
reload(java.nio.file.Path file, java.nio.file.LinkOption... options)
Reload theFileSnapshot
for the given file.boolean
same(FileSnapshot other)
Compares the snapshots' file metadata.static FileSnapshot
save(java.nio.file.Path file, java.nio.file.LinkOption... options)
Creates a newFileSnapshot
for the given path.
-
-
-
Field Detail
-
UNKNOWN_SIZE
public static final long UNKNOWN_SIZE
A value indicating an unknown file size.- See Also:
- Constant Field Values
-
NO_FILE
public static final FileSnapshot NO_FILE
AFileSnapshot
describing a non-existing file.
-
WORST_CASE_TIMESTAMP_RESOLUTION
private static final java.time.Duration WORST_CASE_TIMESTAMP_RESOLUTION
-
lastModified
private final java.nio.file.attribute.FileTime lastModified
-
size
private final long size
-
fileKey
private final java.lang.Object fileKey
-
snapTime
private final java.time.Instant snapTime
-
-
Constructor Detail
-
FileSnapshot
protected FileSnapshot(java.time.Instant snapTime, java.nio.file.attribute.FileTime lastModified, long size, java.lang.Object fileKey)
Creates a newFileSnapshot
instance.- Parameters:
snapTime
- theInstant
the snapshot was takenlastModified
- the "last modified"FileTime
size
- the file sizefileKey
- the file key
-
-
Method Detail
-
getLastModified
protected java.nio.file.attribute.FileTime 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_SIZE
for a snapshot of a non-existing file
-
getFileKey
protected java.lang.Object getFileKey()
Retrieves the file key as recorded in thisFileSnapshot
.- Returns:
- the file key, may be
null
-
getTime
protected java.time.Instant getTime()
Retrieves the time thisFileSnapshot
was taken.- Returns:
- the
Instant
the snapshot was taken, nevernull
-
save
public static FileSnapshot save(java.nio.file.Path file, java.nio.file.LinkOption... options) throws java.io.IOException
Creates a newFileSnapshot
for the given path.- Parameters:
file
- to take the snapshot ofoptions
-LinkOption
s to use- Returns:
- the
FileSnapshot
, nevernull
- Throws:
java.io.IOException
- if an I/O error occurs
-
reload
public FileSnapshot reload(java.nio.file.Path file, java.nio.file.LinkOption... options) throws java.io.IOException
Reload theFileSnapshot
for the given file.- Parameters:
file
- to take the snapshot ofoptions
-LinkOption
s to use- Returns:
- a
FileSnapshot
, nevernull
; if== this
, the file may be assumed unmodified - Throws:
java.io.IOException
- if an I/O error occurs
-
mayBeRacilyClean
protected boolean mayBeRacilyClean()
Determines whether thisFileSnapshot
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 samePath
)- Returns:
true
if the two snapshots have the same file metadata,false
otherwise
-
-