-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStorage.java
More file actions
102 lines (88 loc) · 2.99 KB
/
FileStorage.java
File metadata and controls
102 lines (88 loc) · 2.99 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package ru.javaops.webapp.storage;
import ru.javaops.webapp.exception.StorageException;
import ru.javaops.webapp.model.Resume;
import ru.javaops.webapp.storage.serialisation_strategy.SerialisationStrategy;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class FileStorage extends AbstractStorage<File> {
private final File directory;
private final SerialisationStrategy serialisationStrategy;
protected FileStorage(File directory, SerialisationStrategy serialisationStrategy) {
Objects.requireNonNull(directory, "directory must not be null");
if (!directory.isDirectory()) {
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not directory");
}
if (!directory.canRead() || !directory.canWrite()) {
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not readable/writable");
}
this.directory = directory;
this.serialisationStrategy = serialisationStrategy;
}
@Override
protected File getSearchKey(String uuid) {
return new File(directory, uuid);
}
@Override
protected void doSave(File file, Resume r) {
try {
file.createNewFile();
} catch (IOException e) {
throw new StorageException("Couldn't create file " + file.getAbsolutePath(), file.getName(), e);
}
doUpdate(file, r);
}
@Override
protected void doDelete(File file) {
if (!file.delete()) {
throw new StorageException("File delete error", file.getName());
}
}
@Override
protected void doUpdate(File file, Resume r) {
try {
serialisationStrategy.serialize(r, new BufferedOutputStream(new FileOutputStream(file)));
} catch (IOException e) {
throw new StorageException("File write error", file.getName(), e);
}
}
@Override
protected Resume doGet(File file) {
try {
return serialisationStrategy.deserialize(new BufferedInputStream(new FileInputStream(file)));
} catch (IOException e) {
throw new StorageException("File read error", file.getName(), e);
}
}
@Override
protected List<Resume> doCopyAll() {
File[] files = getAllFiles();
List<Resume> resumes = new ArrayList<>(files.length);
for (File file : getAllFiles()) {
resumes.add(doGet(file));
}
return resumes;
}
@Override
protected boolean isExist(File file) {
return file.exists();
}
@Override
public void clear() {
for (File file : getAllFiles()) {
doDelete(file);
}
}
@Override
public int size() {
return getAllFiles().length;
}
private File[] getAllFiles() {
File[] files = directory.listFiles();
if (files == null) {
throw new StorageException("Directory read error", directory.getName());
}
return files;
}
}