Class FileStreamRepository
- All Implemented Interfaces:
Serializable
,StreamRepository
StreamRepository
interface with
the added feature of automated backups.
Atomic updates are implemented using an AtomicUpdateFileOutputStream
.
When backups are configured, the base file must be copied, not moved, to the first backup on update to avoid
a small window where the base file doesn't exist. This class uses hard links to perform
this "copy" efficiently. This behavior can be altered by overriding copy()
on systems not supporting
hard links.
- See Also:
-
Constructor Summary
ConstructorDescriptionFileStreamRepository
(File file) Convenience constructor for the case where no backup copies are needed.FileStreamRepository
(File file, int numBackups) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Copy, via hard link if possible, a file.protected File
getBackupFile
(File file, int index) Generate a backup file name.final File
getFile()
Get the configuredFile
.long
Get the last modification timestamp of the target file as known to this instance.Get an input stream reading the current value of the underlying store.final int
Get the configured number of backup files.Get an output stream writing to the underlying store.void
setNumBackups
(int numBackups) Change the number of backup files to maintain.
-
Constructor Details
-
FileStreamRepository
Primary constructor.- Parameters:
file
- the file that will store the stream contentnumBackups
- number of backup copies to keep- Throws:
IllegalArgumentException
- iffile
is nullIllegalArgumentException
- ifnumBackups
is negative
-
FileStreamRepository
Convenience constructor for the case where no backup copies are needed.Equivalent to:
FileStreamRepository(file, 0);
- Parameters:
file
- the file that will store the stream content
-
-
Method Details
-
getFile
Get the configuredFile
.- Returns:
- the file that stores the stream content (same as given to constructor)
-
getNumBackups
public final int getNumBackups()Get the configured number of backup files.- Returns:
- the number of backup files to maintain
-
setNumBackups
public void setNumBackups(int numBackups) Change the number of backup files to maintain.If the number of backups is reduced, the "extra" backup files are not touched.
- Parameters:
numBackups
- number of backup files- Throws:
IllegalArgumentException
- ifnumBackups
is less than zero
-
getInputStream
Description copied from interface:StreamRepository
Get an input stream reading the current value of the underlying store.- Specified by:
getInputStream
in interfaceStreamRepository
- Returns:
- input containing current repository contents
- Throws:
IOException
- if an error occurs
-
getOutputStream
Description copied from interface:StreamRepository
Get an output stream writing to the underlying store. The underlying store is not modified until if/when the returned output stream is successfully closed, at which time it is atomically updated with the newly written content.If the returned stream throws an
IOException
at any time, including duringclose()
, or ifclose()
is never invoked, then no update to the underlying storage occurs.- Specified by:
getOutputStream
in interfaceStreamRepository
- Returns:
- output for overwriting the current repository contents
- Throws:
IOException
- if an error occurs
-
getFileTimestamp
public long getFileTimestamp()Get the last modification timestamp of the target file as known to this instance.This returns the modification timestamp as known by this instance; it does not ask the filesystem for the actual last modification timestamp. So if the two are different, then the file has been updated by some external process. In other words, this returns the modification timestamp of the underlying file as it was at the time of the most recent update.
- Returns:
- file's last modification timestamp as known by this instance, or zero if the file has not yet been successfully written to
-
getBackupFile
Generate a backup file name.The implementation in
FileStreamRepository
returns a file with the same name asfile
plus a suffix.1
,.2
,.3
, etc. corresponding toindex
. Subclasses may override as desired.- Parameters:
file
- the file that stores the current stream content (i.e., fromgetFile()
)index
- backup index, always greater than or equal to 1- Returns:
- the
index
'th backup file forfile
-
copy
Copy, via hard link if possible, a file. If the two files are the same, nothing should be done.The implementation in
FileStreamRepository
uses hard links. Subclasses must override this method if the platform does not support hard links.- Parameters:
src
- source filedst
- destination file- Throws:
IOException
- if unsuccessful
-