diff options
author | Keuin <[email protected]> | 2021-01-23 14:10:32 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-23 14:10:32 +0800 |
commit | 4a1d885afa7217b47d6183488c3dc6537cef05b6 (patch) | |
tree | 1b499db6b834cb0709029e30c0d52c0ddf200ffa /src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java | |
parent | 4ac575330130ac4e1b4b35386ffc0aacd431a5a4 (diff) |
Version 1.4.6 (preview): added metadata for incremental backup (need integrated test and display implementation)
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java new file mode 100644 index 0000000..45590ba --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/serializer/IncBackupInfoSerializer.java @@ -0,0 +1,65 @@ +package com.keuin.kbackupfabric.backup.incremental.serializer; + +import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; +import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionConverter; +import com.keuin.kbackupfabric.backup.name.BackupFileNameEncoder; +import com.keuin.kbackupfabric.backup.name.IncrementalBackupFileNameEncoder; +import com.keuin.kbackupfabric.util.backup.incremental.ObjectCollection; + +import java.io.*; +import java.util.Objects; + +public class IncBackupInfoSerializer { + /** + * Load incremental backup index file into object, no matter what version it is. + * + * @param file a valid incremental backup file. (with a valid file name) + * @return the object. Not null. + * @throws IOException when failed due to an I/O error. + */ + public static SavedIncrementalBackup fromFile(File file) throws IOException { + Objects.requireNonNull(file); + try (FileInputStream fileInputStream = new FileInputStream(file)) { + try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) { + Object o = objectInputStream.readObject(); + if (o instanceof SavedIncrementalBackup) { + return (SavedIncrementalBackup) o; + } else if (o instanceof ObjectCollection) { + // backward compatibility with old-style (v0) incremental backup + BackupFileNameEncoder.BackupBasicInformation info = new IncrementalBackupFileNameEncoder().decode(file.getName()); + if (info == null) + throw new IOException("Invalid backup file name."); + return new SavedIncBackupV0(ObjectCollectionConverter.convert((ObjectCollection) o), info); + } else if (o instanceof ObjectCollection2) { + // compatible with 1.4.6 implementation + BackupFileNameEncoder.BackupBasicInformation info = new IncrementalBackupFileNameEncoder().decode(file.getName()); + if (info == null) + throw new IOException("Invalid backup file name."); + return new SavedIncBackupV0((ObjectCollection2) o, info); + } else { + throw new RuntimeException("Unrecognized backup file format: unknown class " + o.getClass().getCanonicalName()); + } + } catch (ClassNotFoundException e) { + // this should not happen + throw new RuntimeException(e); + } + } + } + + /** + * Save incremental backup index and metadata into file. + * + * @param file the file. + * @param backup the backup. + * @throws IOException when failed due to an I/O error. + */ + public static void toFile(File file, SavedIncrementalBackup backup) throws IOException { + Objects.requireNonNull(file); + Objects.requireNonNull(backup); + try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { + try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) { + objectOutputStream.writeObject(backup); + } + } + } +} |