-
Notifications
You must be signed in to change notification settings - Fork 0
Java06. ДЗ 04, Васильев Роман #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
sproshev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8 + 10
| FileOutputStream fos = new FileOutputStream(".server-keeper"); | ||
| ObjectOutputStream oos = new ObjectOutputStream(fos); | ||
| oos.writeObject(keeper); | ||
| fos.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
до этого места исполнение может и не дойти, а закрыть ресуры надо постараться -1
|
|
||
| private static ServerKeeperImpl restoreKeeper() throws IOException, ClassNotFoundException { | ||
| try (FileInputStream fis = new FileInputStream(".server-keeper")) { | ||
| ObjectInputStream ois = new ObjectInputStream(fis); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше закрывать ois, т.к. он внутри себя закроет fis, и оба ресурса будут корректно закрыты
| System.out.println("input 'exit'"); | ||
| } | ||
| } | ||
| server.runNow(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
обычно run подразумевает, что его будет достаточно один раз запустить и всё
| } | ||
| } | ||
| server.runNow(); | ||
| serverKeeper.clearOld(LocalDateTime.now()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
странно, что последние две штуки надо запускать "вручную"
| @Override | ||
| public int upload(UploadItem item) { | ||
| files.add(new FileDescriptor(identifierCounter, item.name, item.size)); | ||
| return identifierCounter++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
результат этой функции однозначно вычисляется по размеру files
| .filter(id -> id == identifier) | ||
| .findFirst() | ||
| .map(downloaded::get) | ||
| .orElse(new HashSet<>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
downloaded.getOrDefault(id, Collections.emptySet())
| long seek = (long) filePart.part * PART_SIZE; | ||
| raf.seek(seek); | ||
| byte[] bytes = new byte[partSize(item.size, filePart.part)]; | ||
| raf.read(bytes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
см документацию на возвращаемое значение -1
| if (!optional.isPresent()) return new byte[0]; | ||
| FileDescriptor item = optional.get(); | ||
| try (RandomAccessFile raf = new RandomAccessFile(item.name, "rw")) { | ||
| if (raf.length() != item.size) throw new Exception("file has different size"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
если исключение ловится ниже, то зачем бросать?
| } | ||
| } | ||
|
|
||
| public List<Integer> update() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
метод ничего не обновляет
|
|
||
| public synchronized void runNow() throws IOException { | ||
| if (updateTime.isBefore(LocalDateTime.now())) { | ||
| downloader.clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
зачем отключать пиров?
@sproshev