Skip to content

Commit 69daf49

Browse files
Theta-DevNullPointerException
authored andcommitted
[YouTube] Improve download speed
1 parent 32c7222 commit 69daf49

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

app/src/main/java/us/shandian/giga/get/DownloadInitializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public void run() {
5454
long lowestSize = Long.MAX_VALUE;
5555

5656
for (int i = 0; i < mMission.urls.length && mMission.running; i++) {
57-
mConn = mMission.openConnection(mMission.urls[i], true, -1, -1);
57+
mConn = mMission.openConnection(mMission.urls[i], true, 0, 0);
5858
mMission.establishConnection(mId, mConn);
5959
dispose();
6060

6161
if (Thread.interrupted()) return;
62-
long length = Utility.getContentLength(mConn);
62+
long length = Utility.getTotalContentLength(mConn);
6363

6464
if (i == 0) {
6565
httpCode = mConn.getResponseCode();
@@ -84,14 +84,14 @@ public void run() {
8484
}
8585
} else {
8686
// ask for the current resource length
87-
mConn = mMission.openConnection(true, -1, -1);
87+
mConn = mMission.openConnection(true, 0, 0);
8888
mMission.establishConnection(mId, mConn);
8989
dispose();
9090

9191
if (!mMission.running || Thread.interrupted()) return;
9292

9393
httpCode = mConn.getResponseCode();
94-
mMission.length = Utility.getContentLength(mConn);
94+
mMission.length = Utility.getTotalContentLength(mConn);
9595
}
9696

9797
if (mMission.length == 0 || httpCode == 204) {

app/src/main/java/us/shandian/giga/util/Utility.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import android.content.Context;
66
import android.os.Build;
77
import android.util.Log;
8-
import android.widget.Toast;
98

9+
import android.widget.Toast;
1010
import androidx.annotation.ColorInt;
1111
import androidx.annotation.DrawableRes;
1212
import androidx.annotation.NonNull;
@@ -28,7 +28,10 @@
2828
import java.security.MessageDigest;
2929
import java.security.NoSuchAlgorithmException;
3030
import java.util.Locale;
31+
import java.util.Random;
3132

33+
import okio.ByteString;
34+
import us.shandian.giga.get.DownloadMission;
3235
import org.schabi.newpipe.streams.io.StoredFileHelper;
3336

3437
public class Utility {
@@ -269,6 +272,28 @@ public static long getContentLength(HttpURLConnection connection) {
269272
return -1;
270273
}
271274

275+
/**
276+
* Get the content length of the entire file even if the HTTP response is partial
277+
* (response code 206).
278+
* @param connection http connection
279+
* @return content length
280+
*/
281+
public static long getTotalContentLength(final HttpURLConnection connection) {
282+
try {
283+
if (connection.getResponseCode() == 206) {
284+
final String rangeStr = connection.getHeaderField("Content-Range");
285+
final String bytesStr = rangeStr.split("/", 2)[1];
286+
return Long.parseLong(bytesStr);
287+
} else {
288+
return getContentLength(connection);
289+
}
290+
} catch (Exception err) {
291+
// nothing to do
292+
}
293+
294+
return -1;
295+
}
296+
272297
private static String pad(int number) {
273298
return number < 10 ? ("0" + number) : String.valueOf(number);
274299
}

0 commit comments

Comments
 (0)