Skip to content

Conversation

@lergor
Copy link
Owner

@lergor lergor commented Dec 22, 2018

No description provided.

Copy link

@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();

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);

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();
}

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);

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;

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);

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);

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.

3 participants