summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
blob: 7a395d899446d332e8521e98fa2de4411b99e4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.keuin.kbackupfabric.backup.incremental;

import com.keuin.kbackupfabric.backup.incremental.serializer.IncBackupInfoSerializer;
import com.keuin.kbackupfabric.backup.incremental.serializer.SavedIncrementalBackup;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

/**
 * Serialize and deserialize ObjectCollection from/to the disk file.
 * Now we want to save additional metadata in incremental backups. So the serializer on pure ObjectCollection is depreciated.
 */
public class ObjectCollectionSerializer {

    /**
     * This doesn't work with the latest format. Use IncBackupInfoSerializer instead.
     */
    @Deprecated
    public static ObjectCollection2 fromFile(File file) throws IOException {
        throw new RuntimeException("This method has been depreciated.");
    }

    /**
     * Only used for testing backward-compatibility with legacy backups.
     */
    public static void toFile(ObjectCollection2 collection, File file) throws IOException {
        Objects.requireNonNull(collection);
        Objects.requireNonNull(file);
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
                objectOutputStream.writeObject(collection);
            }
        }
    }

    public static Iterable<ObjectCollection2> fromDirectory(File directory) throws IOException {
        List<ObjectCollection2> list = new ArrayList<>();
        IncBackupInfoSerializer.fromDirectory(directory)
                .forEach(o -> Optional.ofNullable(o)
                        .map(SavedIncrementalBackup::getObjectCollection)
                        .ifPresent(list::add));
        return Collections.unmodifiableCollection(list);
    }
}