Skip to content

Commit 8cd2a4f

Browse files
committed
lesson 9 видео № 3
Класс AbstractPathStorage переделаy на Path Тест ObjectStreamPathStorageTest
1 parent fa9b1f6 commit 8cd2a4f

File tree

6 files changed

+86
-31
lines changed

6 files changed

+86
-31
lines changed

src/com/urise/webapp/storage/MainFile.java renamed to src/com/urise/webapp/MainFile.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.urise.webapp.storage;
1+
package com.urise.webapp;
22

33
import java.io.File;
44
import java.io.IOException;
55

66
public class MainFile {
7+
static StringBuilder sb=new StringBuilder();
78
public static void main(String[] args) {
89
/*текущий каталог*/
910
File dir = new File(".\\.");
@@ -16,16 +17,20 @@ public static void main(String[] args) {
1617

1718
public static void findFiles(File file) throws IOException {
1819
if (file.isDirectory()) {
20+
System.out.println(sb.toString() + "Directory: " + file.getName());
1921
File[] list = file.listFiles();
2022
if (list == null) {
21-
System.out.println("Files not found!");
23+
System.out.println("Directory is empty!");
2224
return;
2325
}
26+
sb.append(" ");
2427
for (int i = list.length; --i >= 0; ) {
2528
findFiles(list[i]);
2629
}
30+
sb.delete(sb.length()-1,sb.length());
31+
2732
} else {
28-
System.out.println(file.getName());
33+
System.out.println(sb.toString() + file.getName());
2934
}
3035
}
3136

src/com/urise/webapp/exception/StorageException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ public StorageException(String message, String uuid) {
1010
this.uuid = uuid;
1111
}
1212

13+
public StorageException(String message) {
14+
this (message,null,null);
15+
}
16+
17+
public StorageException(String message, Exception e) {
18+
this (message,null,e);
19+
}
1320
public StorageException(String message, String uuid, Exception e) {
1421
super(message,e);
1522
this.uuid = uuid;
16-
1723
}
1824

1925
public String getUuid() {

src/com/urise/webapp/storage/AbstractPathStorage.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Objects;
1313
import java.util.function.Consumer;
14+
import java.util.stream.Collectors;
1415

1516
public abstract class AbstractPathStorage extends AbstractStorage {
1617
private Path directory;
@@ -30,63 +31,65 @@ protected AbstractPathStorage(String dir) {
3031

3132
@Override
3233
protected void doUpdate(Resume resume, Object searchKey) {
33-
File file = (File) searchKey;
34+
Path file = (Path) searchKey;
3435
try {
35-
doWrite(resume, new BufferedOutputStream(new FileOutputStream(file)));
36+
doWrite(resume, new BufferedOutputStream(new FileOutputStream(file.toString())));
3637
} catch (IOException e) {
3738
throw new StorageException("File write error", resume.getUuid(), e);
3839
}
3940
}
4041

4142
@Override
4243
protected boolean isExist(Object file) {
43-
return ((File) file).exists();
44+
Path path=(Path)file;
45+
return Files.isRegularFile(path);
4446
}
4547

4648
@Override
4749
protected void doSave(Resume resume, Object file) {
48-
File f = ((File) file);
50+
Path path = ((Path) file);
4951
try {
50-
51-
f.createNewFile();
52+
Files.createFile(path);
5253
} catch (IOException e) {
53-
throw new StorageException("Couldn't create file " + f.getAbsolutePath(), f.getName(), e);
54+
throw new StorageException("Couldn't create file " + path.toString(), path.getFileName().toString(), e);
5455
}
55-
doUpdate(resume, f);
56+
doUpdate(resume, path);
5657
}
5758

5859
@Override
5960
protected Resume doGet(Object searchKey) {
60-
File file = (File) searchKey;
61+
Path path = (Path) searchKey;
6162
try {
62-
return doRead(new BufferedInputStream(new FileInputStream(file)));
63+
return doRead(new BufferedInputStream(new FileInputStream(path.toString())));
6364
} catch (IOException e) {
64-
throw new StorageException("File read error", file.getName(), e);
65+
throw new StorageException("File read error", path.toString(), e);
6566
}
6667
}
6768

6869
@Override
6970
protected void doDelete(Object searchKey) {
70-
File f = (File) searchKey;
71-
f.delete();
71+
Path path = (Path) searchKey;
72+
try {
73+
Files.delete(path);
74+
} catch (IOException e) {
75+
throw new StorageException("File deleting error " + path.toString(),path.toString(),e);
76+
}
7277
//удаляет файл
7378
}
7479

7580
@Override
76-
protected File getSearchKey(String uuid) {
77-
return new File(directory, uuid);
81+
protected Path getSearchKey(String uuid) {
82+
return directory.resolve(uuid);
7883
}
7984

8085
@Override
8186
protected List<Resume> doCopyAll() {
82-
//читает все файлы и делает do Read и возвращает list
83-
List<Resume> resumes = new ArrayList<>();
84-
File[] files = directory.listFiles();
85-
if (files == null) {
86-
return resumes;
87-
}
88-
for (File f : directory.listFiles()) {
89-
resumes.add(doGet(f));
87+
//читает все файлы и делает doRead и возвращает list
88+
List<Resume> resumes;
89+
try {
90+
resumes=Files.list(directory).map(this::doGet).collect(Collectors.toList());
91+
} catch (IOException e) {
92+
throw new StorageException("Files read error", null,e);
9093
}
9194
return resumes;
9295
}
@@ -96,14 +99,17 @@ public void clear() {
9699
try {
97100
Files.list(directory).forEach(this::doDelete);
98101
} catch (IOException e) {
99-
throw new StorageException("Path delete error",null);
102+
throw new StorageException("File delete error",null,e);
100103
}
101104
}
102105

103106
@Override
104107
public int size() {
105108
//количество фалов в каталоге
106-
File[] files = directory.listFiles();
107-
return files == null ? 0 : files.length;
109+
try {
110+
return (int)Files.list(directory).count();
111+
} catch (IOException e) {
112+
throw new StorageException("Directory file read error",null,e);
113+
}
108114
}
109115
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.exception.StorageException;
4+
import com.urise.webapp.model.Resume;
5+
6+
import java.io.*;
7+
8+
public class ObjectStreamPathStorage extends AbstractPathStorage {
9+
10+
public ObjectStreamPathStorage(String directory) {
11+
super(directory);
12+
}
13+
14+
@Override
15+
protected void doWrite(Resume resume, OutputStream os) throws IOException {
16+
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
17+
oos.writeObject(resume);
18+
}
19+
}
20+
21+
@Override
22+
protected Resume doRead(InputStream is) throws IOException {
23+
try (ObjectInputStream ois = new ObjectInputStream(is)) {
24+
return (Resume) ois.readObject();
25+
} catch (ClassNotFoundException e) {
26+
throw new StorageException("Error read resume", null, e);
27+
}
28+
}
29+
}

test/com/urise/webapp/storage/AllStorageTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
ListStorageTest.class,
1111
MapStorageTest.class,
1212
MapResumeStorageTest.class,
13-
ObjectStreamStorageTest.class
13+
ObjectStreamStorageTest.class,
14+
ObjectStreamPathStorageTest.class
1415
})
1516
public class AllStorageTest {
1617
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.urise.webapp.storage;
2+
3+
public class ObjectStreamPathStorageTest extends AbstractStorageTest {
4+
5+
public ObjectStreamPathStorageTest() {
6+
super(new ObjectStreamPathStorage(STORAGE_DIR.getAbsolutePath()));
7+
}
8+
}

0 commit comments

Comments
 (0)