summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionSerializer.java
blob: f45d4d064cad5f881af25b7ff2aec010c4bb6dcd (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
package com.keuin.kbackupfabric.backup.incremental;

import java.io.*;
import java.util.Objects;

/**
 * Serialize and deserialize ObjectCollection from/to the disk file.
 */
public class ObjectCollectionSerializer {
    public static ObjectCollection fromFile(File file) throws IOException {
        Objects.requireNonNull(file);
        ObjectCollection collection;
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
                collection = (ObjectCollection) objectInputStream.readObject();
            } catch (ClassNotFoundException ignored) {
                // this should not happen
                return null;
            }
        }
        return collection;
    }

    public static void toFile(ObjectCollection 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);
            }
        }
    }
}