Package com.sun.javatest.util
Class BackupPolicy
java.lang.Object
com.sun.javatest.util.BackupPolicy
An abstract base class to provide a way of opening files subject
to a policy of what to do if a file of the same name already exists.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Backup a file by renaming it to have have a new name of old-name~n where n is chosen to be higher than any other for which a candidate new filename exists.backupAndOpenStream
(File file) Backup a file and open a new output stream to the file.backupAndOpenWriter
(File file) Backup a file and open a new Writer to the file.backupAndOpenWriter
(File file, String charsetName) Backup a file and open a new Writer to the file with specified charset.void
backupAndRename
(File source, File target) Rename (or move) the source file to the target name.abstract int
getNumBackupsToKeep
(File file) Get the number of backup files to keep for a particular file.abstract boolean
isBackupRequired
(File file) Determine if backups are enabled for this file.static BackupPolicy
Get a BackupPolicy object which does no backups for any files.static BackupPolicy
simpleBackups
(int n) Get a BackupPolicy object which does a set number of backups for all files.
-
Constructor Details
-
BackupPolicy
public BackupPolicy()
-
-
Method Details
-
noBackups
Get a BackupPolicy object which does no backups for any files.- Returns:
- a BackupPolicy object which does no backups for any files
-
simpleBackups
Get a BackupPolicy object which does a set number of backups for all files.- Parameters:
n
- The number of backups to kept for each file- Returns:
- a BackupPolicy object which does a set number of backups for all files
-
backup
Backup a file by renaming it to have have a new name of old-name~n where n is chosen to be higher than any other for which a candidate new filename exists. Thus, successive backups of a file named "x" will create files named "x~1~", "x~2~", "x~3~", etc. The number of backup files to keep is determined by getNumBackupsToKeep. In addition, backups can be suppressed by isBackupRequired if desired.- Parameters:
file
- the file to be backed up- Throws:
IOException
- if there is a problem renaming the fileSecurityException
- if the file could not be backed up because permission was not given by the security manager- See Also:
-
backupAndRename
Rename (or move) the source file to the target name. Backup the target if necessary. This method will return without action if the source file does not exist.- Parameters:
source
- The file to be backed up. It must be a file (not a directory) which is deleteable.target
- The new name for the file. It must be a file (not a directory) which is will be at a writable location.- Throws:
IOException
- if there is a problem renaming the file. This may happen if the source is a directory, the source file is not writable, or the rename operation fails. In all cases, the rename operation was not successful.SecurityException
- if the backup operation fails because of a security constraint.- Since:
- 3.0.1
- See Also:
-
backupAndOpenWriter
Backup a file and open a new Writer to the file.- Parameters:
file
- the file to be backed up, and for which a new Writer will be opened- Returns:
- a buffered file writer to the specified file
- Throws:
IOException
- if there is a problem backing up the file or creating the new writer objectSecurityException
- if the operation could not be completed because of a security constraint
-
backupAndOpenWriter
Backup a file and open a new Writer to the file with specified charset.- Parameters:
file
- the file to be backed up, and for which a new Writer will be openedcharsetName
- Create an OutputStreamWriter that uses the named charset- Returns:
- a buffered file writer to the specified file
- Throws:
IOException
- if there is a problem backing up the file or creating the new writer objectSecurityException
- if the operation could not be completed because of a security constraint
-
backupAndOpenStream
Backup a file and open a new output stream to the file.- Parameters:
file
- the file to be backed up, and for which a new output stream will be opened- Returns:
- a buffered output stream to the specified file
- Throws:
IOException
- if there is a problem backing up the file or creating the new output streamSecurityException
- if the operation could not be completed because of a security constraint
-
getNumBackupsToKeep
Get the number of backup files to keep for a particular file. When backup is called, the oldest backups are automatically deleted to limit the number of backup files.- Parameters:
file
- the file for which to check how many backups are required- Returns:
- the maximum number of backups to keep for this file
- See Also:
-
isBackupRequired
Determine if backups are enabled for this file. If backups are not enabled, backup will return without affecting the file.- Parameters:
file
- the file for which to check if backups are enabled- Returns:
- true if backups are enabled for this type of file, and false otherwise
-