Skip to content

Java06. ДЗ 05, Горячева Валерия#5

Open
lergor wants to merge 15 commits intomasterfrom
torrent
Open

Java06. ДЗ 05, Горячева Валерия#5
lergor wants to merge 15 commits intomasterfrom
torrent

Conversation

@lergor
Copy link
Copy Markdown
Owner

@lergor lergor commented Dec 22, 2018

No description provided.

Copy link
Copy Markdown

@sproshev sproshev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

очень хорошо, подвела работа с ресурсами( в основном либо потенциально незакрытые, либо всегда незакрытые потоки

} catch (IOException e) {
throw new TorrentException("cannot close tracker properly", e);
}
state.storeToFile();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему состояние не сохраняется, если не удалось закрыть сокет?

public GetResponse(InputStream in, int size) throws IOException {
this.size = size;
this.content = new byte[size];
in.read(content);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

см доку по возвращаемому значению

downloader.close();
sourcesUpdater.close();
seeder.close();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут тоже надо быть аккуратным с тем, что исключение в первой строке приведет к неисполнению оставшихся закрытий

throw new TorrentException("file '" + file + "' does not exists");
}
UploadRequest request = new UploadRequest(file);
UploadResponse response = (UploadResponse) sendRequest(request);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кстати, можно Request сделать дженерик-классом, где E будет тип возвращаемого Response. Тогда здесь не придется выполнять приведение типов. Если не очень понятна мысль, могу поподробнее расписать

public boolean downloadFile(int fileId) throws IOException, TorrentException {
if (localFilesManager.getPartsManager().fileIsPresent(fileId)) {
System.err.println("file with id " + fileId + " already added as local file");
return false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно IllegalArgumentException бросать в таком случае (и ниже)

}

public void storeSplitted(LocalFileReference reference, Path targetFile) throws IOException {
InputStream is = Files.newInputStream(targetFile);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а закрыть?

InputStream is = Files.newInputStream(targetFile);
for (int i = 0; i < reference.getNumberOfParts(); i++) {
byte[] buf = new byte[reference.getBlockSizeForPart(i)];
int readed = is.read(buf);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тот же коммент про возвращаемое значение

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants