throw new ProtocolException("DOWNLOAD_CHUNK specified invalid file handle " + downloadChunk.getHandle());
if (downloadChunk.getNumChunks() <= 0)
throw new ProtocolException("DOWNLOAD_CHUNK: num_chunks must be >= 1");
if (file.getPricePerChunk() > 0) {
// How many chunks can the client afford with their current balance?
PaymentChannelServerState state = payments == null ? null : payments.state();
if (state == null)
throw new ProtocolException("Payment channel not initiated but this file is not free");
long balance = state.getBestValueToMe().longValue();
long affordableChunks = balance / file.getPricePerChunk();
if (affordableChunks < downloadChunk.getNumChunks())
throw new ProtocolException("Insufficient payment received for requested amount of data: got " + balance);
balance -= downloadChunk.getNumChunks();
}