-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat[downloader]: downloader improvements (#6428)
- Add download size queries through a HEAD request - Use the file size for progress instead of file count when all file size are available - Add download speed meter
- Loading branch information
Showing
5 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/SpeedCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package net.kdt.pojavlaunch.tasks; | ||
|
||
/** | ||
* A simple class to calculate the average Internet speed using a simple moving average. | ||
*/ | ||
public class SpeedCalculator { | ||
private long mLastMillis; | ||
private long mLastBytes; | ||
private int mIndex; | ||
private final double[] mPreviousInputs; | ||
private double mSum; | ||
|
||
public SpeedCalculator() { | ||
this(64); | ||
} | ||
|
||
public SpeedCalculator(int averageDepth) { | ||
mPreviousInputs = new double[averageDepth]; | ||
} | ||
|
||
private double addToAverage(double speed) { | ||
mSum -= mPreviousInputs[mIndex]; | ||
mSum += speed; | ||
mPreviousInputs[mIndex] = speed; | ||
if(++mIndex == mPreviousInputs.length) mIndex = 0; | ||
double dLength = mPreviousInputs.length; | ||
return (mSum + (dLength / 2d)) / dLength; | ||
} | ||
|
||
/** | ||
* Update the current amount of bytes downloaded. | ||
* @param bytes the new amount of bytes downloaded | ||
* @return the current download speed in bytes per second | ||
*/ | ||
public double feed(long bytes) { | ||
long millis = System.currentTimeMillis(); | ||
long deltaBytes = bytes - mLastBytes; | ||
long deltaMillis = millis - mLastMillis; | ||
mLastBytes = bytes; | ||
mLastMillis = millis; | ||
double speed = (double)deltaBytes / ((double)deltaMillis / 1000d); | ||
return addToAverage(speed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters