Skip to content

Commit

Permalink
Fix[downloader]: downloader improvement fixes
Browse files Browse the repository at this point in the history
- improve docs, fix types
- increase speed calculator default queue depth
- reenable usage of size counter by default (whoops)
  • Loading branch information
artdeell committed Dec 30, 2024
1 parent 1410a72 commit 03c7942
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void downloadFileMirrored(int downloadClass, String urlInput, File
return;
}catch (FileNotFoundException e) {
Log.w("DownloadMirror", "Cannot find the file on the mirror", e);
Log.i("DownloadMirror", "Failling back to default source");
Log.i("DownloadMirror", "Falling back to default source");
}
DownloadUtils.downloadFileMonitored(urlInput, outputFile, buffer, monitor);
}
Expand All @@ -63,24 +63,24 @@ public static void downloadFileMirrored(int downloadClass, String urlInput, File
return;
}catch (FileNotFoundException e) {
Log.w("DownloadMirror", "Cannot find the file on the mirror", e);
Log.i("DownloadMirror", "Failling back to default source");
Log.i("DownloadMirror", "Falling back to default source");
}
DownloadUtils.downloadFile(urlInput, outputFile);
}

/**
* Getht he content length of a file on the current mirror. If the file is missing on the mirror,
* Get the content length of a file on the current mirror. If the file is missing on the mirror,
* or the mirror does not give out the length, request the length from the original source
* @param downloadClass Class of the download. Can either be DOWNLOAD_CLASS_LIBRARIES,
* DOWNLOAD_CLASS_METADATA or DOWNLOAD_CLASS_ASSETS
* @param urlInput The original (Mojang) URL for the download
* @return the length of the file denoted by the URL
* @return the length of the file denoted by the URL in bytes, or -1 if not available
*/
public static long getContentLengthMirrored(int downloadClass, String urlInput) throws IOException {
long length = DownloadUtils.getContentLength(getMirrorMapping(downloadClass, urlInput));
if(length < 1) {
Log.w("DownloadMirror", "Unable to get content length from mirror");
Log.i("DownloadMirror", "Failling back to default source");
Log.i("DownloadMirror", "Falling back to default source");
return DownloadUtils.getContentLength(urlInput);
}else {
return length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void downloadGame(Activity activity, JMinecraftVersionList.Version verIn
mProcessedSizeCounter = new AtomicLong(0);
mInternetUsageCounter = new AtomicLong(0);
mDownloaderThreadException = new AtomicReference<>(null);
mUseFileCounter = true;
mUseFileCounter = false;

if(!downloadAndProcessMetadata(activity, verInfo, versionName)) {
throw new RuntimeException(activity.getString(R.string.exception_failed_to_unpack_jre17));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SpeedCalculator {
private double mSum;

public SpeedCalculator() {
this(4);
this(64);
}

public SpeedCalculator(int averageDepth) {
Expand All @@ -27,13 +27,18 @@ private double addToAverage(double speed) {
return (mSum + (dLength / 2d)) / dLength;
}

public double feed(long newBytes) {
/**
* 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 = newBytes - mLastBytes;
long deltaBytes = bytes - mLastBytes;
long deltaMillis = millis - mLastMillis;
mLastBytes = deltaBytes;
mLastBytes = bytes;
mLastMillis = millis;
double speed = (double)deltaBytes / (double)deltaMillis;
double speed = (double)deltaBytes / ((double)deltaMillis / 1000d);
return addToAverage(speed);
}
}

0 comments on commit 03c7942

Please sign in to comment.