forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathStorage.java
More file actions
112 lines (99 loc) · 3.49 KB
/
PathStorage.java
File metadata and controls
112 lines (99 loc) · 3.49 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
103
104
105
106
107
108
109
110
111
112
package com.urise.webapp.storage;
import com.urise.webapp.exception.StorageException;
import com.urise.webapp.model.Resume;
import com.urise.webapp.storage.serializer.StreamSerializer;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class PathStorage extends AbstractStorage {
private Path directory;
private StreamSerializer streamSerializer;
protected PathStorage(String dir, StreamSerializer streamSerializer) {
Path directory = Paths.get(dir);
Objects.requireNonNull(directory, "directory must not null");
this.streamSerializer = streamSerializer;
if (!Files.isDirectory(directory) || !Files.isWritable(directory)) {
throw new IllegalArgumentException(dir + " is not directory or is not writable");
}
this.directory = directory;
}
@Override
protected void doUpdate(Resume resume, Object searchKey) {
Path file = (Path) searchKey;
try {
streamSerializer.doWrite(resume, new BufferedOutputStream(new FileOutputStream(file.toString())));
} catch (IOException e) {
throw new StorageException("File write error", resume.getUuid(), e);
}
}
@Override
protected boolean isExist(Object file) {
Path path = (Path) file;
return Files.isRegularFile(path);
}
@Override
protected void doSave(Resume resume, Object file) {
Path path = ((Path) file);
try {
Files.createFile(path);
} catch (IOException e) {
throw new StorageException("Couldn't create file " + path.toString(), path.getFileName().toString(), e);
}
doUpdate(resume, path);
}
@Override
protected Resume doGet(Object searchKey) {
Path path = (Path) searchKey;
try {
return streamSerializer.doRead(new BufferedInputStream(new FileInputStream(path.toString())));
} catch (IOException e) {
throw new StorageException("File read error", path.toString(), e);
}
}
@Override
protected void doDelete(Object searchKey) {
Path path = (Path) searchKey;
try {
Files.delete(path);
} catch (IOException e) {
throw new StorageException("File deleting error " + path.toString(), path.toString(), e);
}
//удаляет файл
}
@Override
protected Path getSearchKey(String uuid) {
return directory.resolve(uuid);
}
@Override
protected List<Resume> doCopyAll() {
//читает все файлы и делает doRead и возвращает list
List<Resume> resumes;
try {
resumes = Files.list(directory).map(this::doGet).collect(Collectors.toList());
} catch (IOException e) {
throw new StorageException("Files read error", null, e);
}
return resumes;
}
@Override
public void clear() {
try {
Files.list(directory).forEach(this::doDelete);
} catch (IOException e) {
throw new StorageException("File delete error", null, e);
}
}
@Override
public int size() {
//количество фалов в каталоге
try {
return (int) Files.list(directory).count();
} catch (IOException e) {
throw new StorageException("Directory file read error", null, e);
}
}
}