|
| 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 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.function.Consumer; |
| 14 | + |
| 15 | +public abstract class AbstractPathStorage extends AbstractStorage { |
| 16 | + private Path directory; |
| 17 | + |
| 18 | + protected AbstractPathStorage(String dir) { |
| 19 | + Path directory = Paths.get(dir); |
| 20 | + Objects.requireNonNull(directory, "directory must not null"); |
| 21 | + if (!Files.isDirectory(directory)||!Files.isWritable(directory) ) { |
| 22 | + throw new IllegalArgumentException(dir + " is not directory or is not writable"); |
| 23 | + } |
| 24 | + this.directory = directory; |
| 25 | + } |
| 26 | + |
| 27 | + protected abstract void doWrite(Resume resume, OutputStream os) throws IOException; |
| 28 | + |
| 29 | + protected abstract Resume doRead(InputStream is) throws IOException; |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void doUpdate(Resume resume, Object searchKey) { |
| 33 | + File file = (File) searchKey; |
| 34 | + try { |
| 35 | + doWrite(resume, new BufferedOutputStream(new FileOutputStream(file))); |
| 36 | + } catch (IOException e) { |
| 37 | + throw new StorageException("File write error", resume.getUuid(), e); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected boolean isExist(Object file) { |
| 43 | + return ((File) file).exists(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void doSave(Resume resume, Object file) { |
| 48 | + File f = ((File) file); |
| 49 | + try { |
| 50 | + |
| 51 | + f.createNewFile(); |
| 52 | + } catch (IOException e) { |
| 53 | + throw new StorageException("Couldn't create file " + f.getAbsolutePath(), f.getName(), e); |
| 54 | + } |
| 55 | + doUpdate(resume, f); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Resume doGet(Object searchKey) { |
| 60 | + File file = (File) searchKey; |
| 61 | + try { |
| 62 | + return doRead(new BufferedInputStream(new FileInputStream(file))); |
| 63 | + } catch (IOException e) { |
| 64 | + throw new StorageException("File read error", file.getName(), e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void doDelete(Object searchKey) { |
| 70 | + File f = (File) searchKey; |
| 71 | + f.delete(); |
| 72 | +//удаляет файл |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected File getSearchKey(String uuid) { |
| 77 | + return new File(directory, uuid); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + 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)); |
| 90 | + } |
| 91 | + return resumes; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void clear() { |
| 96 | + try { |
| 97 | + Files.list(directory).forEach(this::doDelete); |
| 98 | + } catch (IOException e) { |
| 99 | + throw new StorageException("Path delete error",null); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int size() { |
| 105 | + //количество фалов в каталоге |
| 106 | + File[] files = directory.listFiles(); |
| 107 | + return files == null ? 0 : files.length; |
| 108 | + } |
| 109 | +} |
0 commit comments