[YouTube] Improve download speed (#9948)

This commit is contained in:
ThetaDev 2023-05-01 19:26:42 +02:00 committed by GitHub
parent ed1781133c
commit fb00ee8cf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View file

@ -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);
}