22
33import com .urise .webapp .exception .StorageException ;
44import com .urise .webapp .model .Resume ;
5+ import com .urise .webapp .storage .serializer .StreamSerializer ;
56
67import java .io .*;
78import java .nio .file .Files ;
1314import java .util .function .Consumer ;
1415import java .util .stream .Collectors ;
1516
16- public abstract class AbstractPathStorage extends AbstractStorage {
17+ public class PathStorage extends AbstractStorage {
1718 private Path directory ;
19+ private StreamSerializer streamSerializer ;
1820
19- protected AbstractPathStorage (String dir ) {
21+ protected PathStorage (String dir , StreamSerializer streamSerializer ) {
2022 Path directory = Paths .get (dir );
2123 Objects .requireNonNull (directory , "directory must not null" );
24+ this .streamSerializer =streamSerializer ;
2225 if (!Files .isDirectory (directory )||!Files .isWritable (directory ) ) {
2326 throw new IllegalArgumentException (dir + " is not directory or is not writable" );
2427 }
2528 this .directory = directory ;
2629 }
2730
28- protected abstract void doWrite (Resume resume , OutputStream os ) throws IOException ;
29-
30- protected abstract Resume doRead (InputStream is ) throws IOException ;
31-
3231 @ Override
3332 protected void doUpdate (Resume resume , Object searchKey ) {
3433 Path file = (Path ) searchKey ;
3534 try {
36- doWrite (resume , new BufferedOutputStream (new FileOutputStream (file .toString ())));
35+ streamSerializer . doWrite (resume , new BufferedOutputStream (new FileOutputStream (file .toString ())));
3736 } catch (IOException e ) {
3837 throw new StorageException ("File write error" , resume .getUuid (), e );
3938 }
@@ -60,7 +59,7 @@ protected void doSave(Resume resume, Object file) {
6059 protected Resume doGet (Object searchKey ) {
6160 Path path = (Path ) searchKey ;
6261 try {
63- return doRead (new BufferedInputStream (new FileInputStream (path .toString ())));
62+ return streamSerializer . doRead (new BufferedInputStream (new FileInputStream (path .toString ())));
6463 } catch (IOException e ) {
6564 throw new StorageException ("File read error" , path .toString (), e );
6665 }
0 commit comments