Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix downloads stuck at 99.9% #2440

Merged
merged 3 commits into from
Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions app/src/main/java/us/shandian/giga/get/DownloadInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public void run() {
int retryCount = 0;
while (true) {
try {
mMission.currentThreadCount = mMission.threadCount;

if (mMission.blocks < 0 && mMission.current == 0) {
if (mMission.blocks == null && mMission.current == 0) {
// calculate the whole size of the mission
long finalLength = 0;
long lowestSize = Long.MAX_VALUE;
Expand Down Expand Up @@ -83,11 +81,9 @@ public void run() {

// check for dynamic generated content
if (mMission.length == -1 && mConn.getResponseCode() == 200) {
mMission.blocks = 0;
mMission.blocks = new int[0];
mMission.length = 0;
mMission.fallback = true;
mMission.unknownLength = true;
mMission.currentThreadCount = 1;

if (DEBUG) {
Log.d(TAG, "falling back (unknown length)");
Expand All @@ -99,24 +95,17 @@ public void run() {

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

synchronized (mMission.blockState) {
synchronized (mMission.LOCK) {
if (mConn.getResponseCode() == 206) {
if (mMission.currentThreadCount > 1) {
mMission.blocks = mMission.length / DownloadMission.BLOCK_SIZE;

if (mMission.currentThreadCount > mMission.blocks) {
mMission.currentThreadCount = (int) mMission.blocks;
}
if (mMission.currentThreadCount <= 0) {
mMission.currentThreadCount = 1;
}
if (mMission.blocks * DownloadMission.BLOCK_SIZE < mMission.length) {
mMission.blocks++;
}

if (mMission.threadCount > 1) {
int count = (int) (mMission.length / DownloadMission.BLOCK_SIZE);
if ((count * DownloadMission.BLOCK_SIZE) < mMission.length) count++;

mMission.blocks = new int[count];
} else {
// if one thread is solicited don't calculate blocks, is useless
mMission.blocks = 1;
mMission.fallback = true;
// if one thread is required don't calculate blocks, is useless
mMission.blocks = new int[0];
mMission.unknownLength = false;
}

Expand All @@ -125,20 +114,13 @@ public void run() {
}
} else {
// Fallback to single thread
mMission.blocks = 0;
mMission.fallback = true;
mMission.blocks = new int[0];
mMission.unknownLength = false;
mMission.currentThreadCount = 1;

if (DEBUG) {
Log.d(TAG, "falling back due http response code = " + mConn.getResponseCode());
}
}

for (long i = 0; i < mMission.currentThreadCount; i++) {
mMission.threadBlockPositions.add(i);
mMission.threadBytePositions.add(0L);
}
}

if (!mMission.running || Thread.interrupted()) return;
Expand Down
Loading