blob: fa411a09ef27bac415d6396d50904a53b61fd589 (
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
package com.keuin.kbackupfabric.backup.incremental;
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Objects;
/**
* 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 {
Objects.requireNonNull(file);
ObjectCollection2 collection;
try (FileInputStream fileInputStream = new FileInputStream(file)) {
try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
collection = (ObjectCollection2) objectInputStream.readObject();
} catch (ClassNotFoundException ignored) {
// this should not happen
return null;
}
}
return collection;
}
/**
* 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 {
if (!directory.isDirectory()) {
throw new IllegalArgumentException("Given directory is invalid.");
}
return new Iterable<ObjectCollection2>() {
private final Iterator<ObjectCollection2> iter = new Iterator<ObjectCollection2>() {
private final Iterator<Path> i = Files.walk(directory.toPath(), 1).filter(p -> {
File f = p.toFile();
return f.isFile() && f.getName().endsWith(".kbi");
}).iterator();
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public ObjectCollection2 next() {
try {
return fromFile(i.next().toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
@NotNull
@Override
public Iterator<ObjectCollection2> iterator() {
return iter;
}
};
}
}
|