Class BackupPolicy


  • public abstract class BackupPolicy
    extends java.lang.Object
    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 
      Constructor Description
      BackupPolicy()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void backup​(java.io.File file)
      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.
      java.io.OutputStream backupAndOpenStream​(java.io.File file)
      Backup a file and open a new output stream to the file.
      java.io.Writer backupAndOpenWriter​(java.io.File file)
      Backup a file and open a new Writer to the file.
      java.io.Writer backupAndOpenWriter​(java.io.File file, java.lang.String charsetName)
      Backup a file and open a new Writer to the file with specified charset.
      void backupAndRename​(java.io.File source, java.io.File target)
      Rename (or move) the source file to the target name.
      abstract int getNumBackupsToKeep​(java.io.File file)
      Get the number of backup files to keep for a particular file.
      abstract boolean isBackupRequired​(java.io.File file)
      Determine if backups are enabled for this file.
      static BackupPolicy noBackups()
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BackupPolicy

        public BackupPolicy()
    • Method Detail

      • backup

        public void backup​(java.io.File file)
                    throws java.io.IOException,
                           java.lang.SecurityException
        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:
        java.io.IOException - if there is a problem renaming the file
        java.lang.SecurityException - if the file could not be backed up because permission was not given by the security manager
        See Also:
        getNumBackupsToKeep(java.io.File), isBackupRequired(java.io.File)
      • backupAndRename

        public void backupAndRename​(java.io.File source,
                                    java.io.File target)
                             throws java.io.IOException,
                                    java.lang.SecurityException
        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:
        java.io.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.
        java.lang.SecurityException - if the backup operation fails because of a security constraint.
        Since:
        3.0.1
        See Also:
        backup(File)
      • backupAndOpenWriter

        public java.io.Writer backupAndOpenWriter​(java.io.File file)
                                           throws java.io.IOException,
                                                  java.lang.SecurityException
        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:
        java.io.IOException - if there is a problem backing up the file or creating the new writer object
        java.lang.SecurityException - if the operation could not be completed because of a security constraint
      • backupAndOpenWriter

        public java.io.Writer backupAndOpenWriter​(java.io.File file,
                                                  java.lang.String charsetName)
                                           throws java.io.IOException,
                                                  java.lang.SecurityException
        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 opened
        charsetName - Create an OutputStreamWriter that uses the named charset
        Returns:
        a buffered file writer to the specified file
        Throws:
        java.io.IOException - if there is a problem backing up the file or creating the new writer object
        java.lang.SecurityException - if the operation could not be completed because of a security constraint
      • backupAndOpenStream

        public java.io.OutputStream backupAndOpenStream​(java.io.File file)
                                                 throws java.io.IOException,
                                                        java.lang.SecurityException
        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:
        java.io.IOException - if there is a problem backing up the file or creating the new output stream
        java.lang.SecurityException - if the operation could not be completed because of a security constraint
      • getNumBackupsToKeep

        public abstract int getNumBackupsToKeep​(java.io.File file)
        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:
        backup(java.io.File)
      • isBackupRequired

        public abstract boolean isBackupRequired​(java.io.File file)
        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
      • noBackups

        public static BackupPolicy noBackups()
        Get a BackupPolicy object which does no backups for any files.
        Returns:
        a BackupPolicy object which does no backups for any files
      • simpleBackups

        public static BackupPolicy simpleBackups​(int n)
        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