From fb00ee8cf9012358d7a0ebf7682bf34d9db2e752 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Mon, 1 May 2023 19:26:42 +0200 Subject: [PATCH] [YouTube] Improve download speed (#9948) --- .../giga/get/DownloadInitializer.java | 8 +++--- .../java/us/shandian/giga/util/Utility.java | 27 ++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java index 7c0fa9012..1d5a93588 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadInitializer.java @@ -54,12 +54,12 @@ public class DownloadInitializer extends Thread { long lowestSize = Long.MAX_VALUE; for (int i = 0; i < mMission.urls.length && mMission.running; i++) { - mConn = mMission.openConnection(mMission.urls[i], true, -1, -1); + mConn = mMission.openConnection(mMission.urls[i], true, 0, 0); mMission.establishConnection(mId, mConn); dispose(); if (Thread.interrupted()) return; - long length = Utility.getContentLength(mConn); + long length = Utility.getTotalContentLength(mConn); if (i == 0) { httpCode = mConn.getResponseCode(); @@ -84,14 +84,14 @@ public class DownloadInitializer extends Thread { } } else { // ask for the current resource length - mConn = mMission.openConnection(true, -1, -1); + mConn = mMission.openConnection(true, 0, 0); mMission.establishConnection(mId, mConn); dispose(); if (!mMission.running || Thread.interrupted()) return; httpCode = mConn.getResponseCode(); - mMission.length = Utility.getContentLength(mConn); + mMission.length = Utility.getTotalContentLength(mConn); } if (mMission.length == 0 || httpCode == 204) { diff --git a/app/src/main/java/us/shandian/giga/util/Utility.java b/app/src/main/java/us/shandian/giga/util/Utility.java index ecce6639e..3cfa22bd9 100644 --- a/app/src/main/java/us/shandian/giga/util/Utility.java +++ b/app/src/main/java/us/shandian/giga/util/Utility.java @@ -1,11 +1,8 @@ package us.shandian.giga.util; -import android.content.ClipData; -import android.content.ClipboardManager; import android.content.Context; import android.os.Build; import android.util.Log; -import android.widget.Toast; import androidx.annotation.ColorInt; import androidx.annotation.DrawableRes; @@ -29,8 +26,10 @@ import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.HttpURLConnection; import java.util.Locale; +import java.util.Random; import okio.ByteString; +import us.shandian.giga.get.DownloadMission; public class Utility { @@ -232,6 +231,28 @@ public class Utility { return -1; } + /** + * Get the content length of the entire file even if the HTTP response is partial + * (response code 206). + * @param connection http connection + * @return content length + */ + public static long getTotalContentLength(final HttpURLConnection connection) { + try { + if (connection.getResponseCode() == 206) { + final String rangeStr = connection.getHeaderField("Content-Range"); + final String bytesStr = rangeStr.split("/", 2)[1]; + return Long.parseLong(bytesStr); + } else { + return getContentLength(connection); + } + } catch (Exception err) { + // nothing to do + } + + return -1; + } + private static String pad(int number) { return number < 10 ? ("0" + number) : String.valueOf(number); }