Top Description Fields Constructors Methods
org.apache.sshd.common.util.io

public Class FileSnapshot

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.file.Files, .LinkOption, .NoSuchFileException, .Path, java.nio.file.attribute.BasicFileAttributes, .FileTime, java.time.Duration, .Instant, java.util.Objects

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.

Author
Apache MINA SSHD Project

Field Summary

Modifier and TypeField and Description
private final Object
private final FileTime
public static final FileSnapshot
NO_FILE

A FileSnapshot describing a non-existing file.

private final long
private final Instant
public static final long
UNKNOWN_SIZE

A value indicating an unknown file size.

private static final Duration

Constructor Summary

AccessConstructor and Description
protected
FileSnapshot(Instant
the Instant the snapshot was taken
snapTime
,
FileTime
the "last modified" FileTime
lastModified
,
long
the file size
size
,
Object
the file key
fileKey
)

Creates a new FileSnapshot instance.

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

the file key, may be null
getFileKey
()

Retrieves the file key as recorded in this FileSnapshot.

protected FileTime

Returns:

the FileTime, may be null
getLastModified
()

Retrieves the "last modified" time as recorded in this FileSnapshot.

protected long

Returns:

the size, UNKNOWN_SIZE for a snapshot of a non-existing file
getSize
()

Retrieves the file size as recorded in this FileSnapshot.

protected Instant

Returns:

the Instant the snapshot was taken, never null
getTime
()

Retrieves the time this FileSnapshot was taken.

protected boolean

Returns:

true if so, false otherwise
mayBeRacilyClean
()

Determines whether this FileSnapshot was taken within the file timestamp resolution of the file system after the last modified time of the file.

public FileSnapshot

Returns:

a FileSnapshot, never null; if == this, the file may be assumed unmodified
reload
(Path
to take the snapshot of
file
,
LinkOption...
LinkOptions to use
options
)

Reload the FileSnapshot for the given file.

public boolean

Returns:

true if the two snapshots have the same file metadata, false otherwise
same
(FileSnapshot
FileSnapshot to compare to (should be for the same Path)
other
)

Compares the snapshots' file metadata.

public static FileSnapshot

Returns:

the FileSnapshot, never null
save
(Path
to take the snapshot of
file
,
LinkOption...
LinkOptions to use
options
)

Creates a new FileSnapshot for the given path.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fileKeyback to summary
private final Object fileKey
lastModifiedback to summary
private final FileTime lastModified
NO_FILEback to summary
public static final FileSnapshot NO_FILE

A FileSnapshot describing a non-existing file.

sizeback to summary
private final long size
snapTimeback to summary
private final Instant snapTime
UNKNOWN_SIZEback to summary
public static final long UNKNOWN_SIZE

A value indicating an unknown file size.

WORST_CASE_TIMESTAMP_RESOLUTIONback to summary
private static final Duration WORST_CASE_TIMESTAMP_RESOLUTION

Constructor Detail

FileSnapshotback to summary
protected FileSnapshot(Instant snapTime, FileTime lastModified, long size, Object fileKey)

Creates a new FileSnapshot instance.

Parameters
snapTime:Instant

the Instant the snapshot was taken

lastModified:FileTime

the "last modified" FileTime

size:long

the file size

fileKey:Object

the file key

Method Detail

getFileKeyback to summary
protected Object getFileKey()

Retrieves the file key as recorded in this FileSnapshot.

Returns:Object

the file key, may be null

getLastModifiedback to summary
protected FileTime getLastModified()

Retrieves the "last modified" time as recorded in this FileSnapshot.

Returns:FileTime

the FileTime, may be null

getSizeback to summary
protected long getSize()

Retrieves the file size as recorded in this FileSnapshot.

Returns:long

the size, UNKNOWN_SIZE for a snapshot of a non-existing file

getTimeback to summary
protected Instant getTime()

Retrieves the time this FileSnapshot was taken.

Returns:Instant

the Instant the snapshot was taken, never null

mayBeRacilyCleanback to summary
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:boolean

true if so, false otherwise

reloadback to summary
public FileSnapshot reload(Path file, LinkOption... options) throws IOException

Reload the FileSnapshot for the given file.

Parameters
file:Path

to take the snapshot of

options:LinkOption[]

LinkOptions to use

Returns:FileSnapshot

a FileSnapshot, never null; if == this, the file may be assumed unmodified

Exceptions
IOException:
if an I/O error occurs
sameback to summary
public boolean same(FileSnapshot other)

Compares the snapshots' file metadata.

Parameters
other:FileSnapshot

FileSnapshot to compare to (should be for the same Path)

Returns:boolean

true if the two snapshots have the same file metadata, false otherwise

saveback to summary
public static FileSnapshot save(Path file, LinkOption... options) throws IOException

Creates a new FileSnapshot for the given path.

Parameters
file:Path

to take the snapshot of

options:LinkOption[]

LinkOptions to use

Returns:FileSnapshot

the FileSnapshot, never null

Exceptions
IOException:
if an I/O error occurs