[YouTube] Improve download speed (#9948)
This commit is contained in:
parent
ed1781133c
commit
fb00ee8cf9
2 changed files with 28 additions and 7 deletions
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue