}
private void handleData(Payfile.Data data) throws IOException, ProtocolException {
File file = handleToFile(data.getHandle());
if (file == null)
throw new ProtocolException("Unknown handle");
if (data.getChunkId() == file.nextChunk - 1) {
final byte[] bits = data.getData().toByteArray();
file.bytesDownloaded += bits.length;
file.downloadStream.write(bits);
if ((data.getChunkId() + 1) * chunkSize >= file.getSize()) {
// File is done.
file.downloadStream.close();
currentDownloads.remove(file);
file.completionFuture.complete(null);
currentFuture = null;
} else {
downloadNextChunk(file);
}
} else {
throw new ProtocolException("Server sent wrong part of file");
}
}