Skip to content

Conversation

@nizshee
Copy link
Owner

@nizshee nizshee commented Dec 10, 2016

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.

8 + 10

FileOutputStream fos = new FileOutputStream(".server-keeper");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(keeper);
fos.close();

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

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

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

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

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

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

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

Choose a reason for hiding this comment

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

если исключение ловится ниже, то зачем бросать?

}
}

public List<Integer> update() {

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

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