public class FileSnapshot extends Object
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.
| Modifier and Type | Field and Description |
|---|---|
static FileSnapshot |
NO_FILE
A
FileSnapshot describing a non-existing file. |
static long |
UNKNOWN_SIZE
A value indicating an unknown file size.
|
| Modifier | Constructor and Description |
|---|---|
protected |
FileSnapshot(Instant snapTime,
FileTime lastModified,
long size,
Object fileKey)
Creates a new
FileSnapshot instance. |
| Modifier and Type | Method and Description |
|---|---|
protected Object |
getFileKey()
Retrieves the file key as recorded in this
FileSnapshot. |
protected FileTime |
getLastModified()
Retrieves the "last modified" time as recorded in this
FileSnapshot. |
protected long |
getSize()
Retrieves the file size as recorded in this
FileSnapshot. |
protected Instant |
getTime()
Retrieves the time this
FileSnapshot was taken. |
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. |
FileSnapshot |
reload(Path file,
LinkOption... options)
Reload the
FileSnapshot for the given file. |
boolean |
same(FileSnapshot other)
Compares the snapshots' file metadata.
|
static FileSnapshot |
save(Path file,
LinkOption... options)
Creates a new
FileSnapshot for the given path. |
public static final long UNKNOWN_SIZE
public static final FileSnapshot NO_FILE
FileSnapshot describing a non-existing file.protected FileTime getLastModified()
FileSnapshot.FileTime, may be nullprotected long getSize()
FileSnapshot.UNKNOWN_SIZE for a snapshot of a non-existing fileprotected Object getFileKey()
FileSnapshot.nullprotected Instant getTime()
FileSnapshot was taken.Instant the snapshot was taken, never nullpublic static FileSnapshot save(Path file, LinkOption... options) throws IOException
FileSnapshot for the given path.file - to take the snapshot ofoptions - LinkOptions to useFileSnapshot, never nullIOException - if an I/O error occurspublic FileSnapshot reload(Path file, LinkOption... options) throws IOException
FileSnapshot for the given file.file - to take the snapshot ofoptions - LinkOptions to useFileSnapshot, never null; if == this, the file may be assumed
unmodifiedIOException - if an I/O error occursprotected boolean mayBeRacilyClean()
FileSnapshot was taken within the file timestamp resolution of the file system
after the last modified time of the file.true if so, false otherwisepublic boolean same(FileSnapshot other)
other - FileSnapshot to compare to (should be for the same Path)true if the two snapshots have the same file metadata, false otherwiseCopyright © 2018–2024 The Apache Software Foundation. All rights reserved.