Reputation: 15
I have a Java program that downloads a file from a server, using 8 parallel connections. The file is downloaded in 8 parts and assembled at the end. How can I calculate the remaining time of downloading?
Upvotes: 0
Views: 60
Reputation: 15
This method worked for me :
private String calculateRemainingTime(long bytesDownloaded, long totalBytes, long elapsedTime){
if (bytesDownloaded == 0) {
return "Calculating...";
}
long bytesRemaining = totalBytes - bytesDownloaded;
long estimatedTimeRemaining = (bytesRemaining * elapsedTime) / bytesDownloaded;
long seconds = (estimatedTimeRemaining / 1000) % 60;
long minutes = (estimatedTimeRemaining / (1000 * 60)) % 60;
long hours = (estimatedTimeRemaining / (1000 * 60 * 60)) % 24;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
Upvotes: 0
Reputation: 36
first try to calculate download speed over 5-10 second period like
speed
= downloaded_bytes_from_all_8_connections_in_last_10seconds
/10
it'll give you average speed over last 10 seconds
now just use simple math to get the remaining time,
remaning_number_of_seconds
= total_remaining_bytes
/speed
Upvotes: 2