Skip to content

Java06. ДЗ 04, Васильев Роман#10

Open
nizshee wants to merge 3 commits intomasterfrom
torrent-gui
Open

Java06. ДЗ 04, Васильев Роман#10
nizshee wants to merge 3 commits intomasterfrom
torrent-gui

Conversation

@nizshee
Copy link
Copy Markdown
Owner

@nizshee nizshee commented Dec 10, 2016

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.

8 + 10

FileOutputStream fos = new FileOutputStream(".server-keeper");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(keeper);
fos.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.

до этого места исполнение может и не дойти, а закрыть ресуры надо постараться -1


private static ServerKeeperImpl restoreKeeper() throws IOException, ClassNotFoundException {
try (FileInputStream fis = new FileInputStream(".server-keeper")) {
ObjectInputStream ois = new ObjectInputStream(fis);
Copy link
Copy Markdown

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();
Copy link
Copy Markdown

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());
Copy link
Copy Markdown

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++;
Copy link
Copy Markdown

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<>());
Copy link
Copy Markdown

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);
Copy link
Copy Markdown

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");
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 List<Integer> update() {
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 synchronized void runNow() throws IOException {
if (updateTime.isBefore(LocalDateTime.now())) {
downloader.clear();
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