-
Notifications
You must be signed in to change notification settings - Fork 0
Java06. ДЗ 05, Егоров Антон #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
Ещё дописано команд в REPL.
Сделаны join потоков сервера.
Добавлена команда resume.
До этого для него выделялся новый id.
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.
10
| /** | ||
| * | ||
| */ | ||
| private static final long serialVersionUID = 1L; |
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.
зачем? оно ж не сериализуется
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.
Это подсказка IDE была.
|
|
||
| ExecutorService pool = Executors.newCachedThreadPool(); | ||
|
|
||
| Map<Integer, Future<?>> fileDownloadsFutures = new HashMap<>(); |
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.
модификаторы доступа? не, не слышал)
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.
если сделали старт, но не делали стоп, получается мапа будет содержать выполненные future сколь угодно долго?
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 void stopFileDownload(int fileId) { | ||
| if (!filesHolder.fileStatus.containsKey(fileId)) { | ||
| throw new NullPointerException("This file wasn't been downloading"); |
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.
это исключение про разыменование нулевого указателя, здесь скорее IllegalArgumentException/IllegalStateException/NoSuchElementException
| try { | ||
| return Math.toIntExact((files.get(fileId).length() + pieceSize - 1) / pieceSize); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); |
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.
можно еще java.io.UncheckedIOException
| } | ||
| } | ||
|
|
||
| public int pieceOffset(int fileId, int numPart) { |
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.
не используется
| } | ||
| } catch (IOException e) { | ||
| out.println(e.getMessage()); | ||
| } catch (InterruptedException e) { |
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.
возможно лучше finally
| } catch (IOException e) { | ||
| // TODO Auto-generated catch block | ||
| e.printStackTrace(); | ||
| } |
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.
не очень хорошая обработка IOException
семафор может быть не релизнут и/или канал не закрыт
| dout.writeByte(1); | ||
|
|
||
| fileSizes.clear(); | ||
| filesNames.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.
лучше все-таки не менять передаваемые параметры, а результат работы "возвращать"
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.
Но тут параметров много.
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.
и?) это же не оправдывает, в плюсах так исторически сложилось, что можно, в джаве скорее нет
| dinp.close(); | ||
| dout.close(); | ||
| s.close(); | ||
| } catch (NullPointerException npe) { |
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.
это совсем беда, NPE это ошибка программирования, и оно никак не может оказаться в ожидаемых исключениях, вместо этого должна использоваться проверка на null
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.
Кажется что это красивее чем делать три if и пытаться сообщить что кто-то оказался null.
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.
сокет закрывает свои потоки
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.
да и try-with-resources есть
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.
Сделал вместо этого проверку на null.
| dout = new DataOutputStream(s.getOutputStream()); | ||
| dinp = new DataInputStream(s.getInputStream()); | ||
|
|
||
| dout.writeByte(1); |
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.
flush
В частности сделал загрузку файлового дискриптора ленивой (только когда надо считать или записать кусок). Future из мапы в FilesDownloader удаляется при завершении загрузки.
Исправил catch NPE
No description provided.