Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e45ffa5

Browse files
author
Tianhua Ran
committedMar 5, 2018
refactor: retry from offset saved in db
1 parent 5646236 commit e45ffa5

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed
 

‎library/src/main/java/com/liulishuo/filedownloader/download/DownloadLaunchRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long en
791791
return;
792792
}
793793

794-
final int doneConnectionIndex = doneRunnable == null ? -1 : doneRunnable.connectionIndex;
794+
final int doneConnectionIndex = doneRunnable.connectionIndex;
795795
if (FileDownloadLog.NEED_LOG) {
796796
FileDownloadLog.d(this, "the connection has been completed(%d): [%d, %d) %d",
797797
doneConnectionIndex, startOffset, endOffset, model.getTotal());

‎library/src/main/java/com/liulishuo/filedownloader/download/DownloadRunnable.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
import android.os.Process;
2020

2121
import com.liulishuo.filedownloader.connection.FileDownloadConnection;
22+
import com.liulishuo.filedownloader.database.FileDownloadDatabase;
2223
import com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException;
24+
import com.liulishuo.filedownloader.model.ConnectionModel;
2325
import com.liulishuo.filedownloader.model.FileDownloadHeader;
26+
import com.liulishuo.filedownloader.model.FileDownloadModel;
2427
import com.liulishuo.filedownloader.util.FileDownloadLog;
2528
import com.liulishuo.filedownloader.util.FileDownloadUtils;
2629

2730
import java.io.IOException;
2831
import java.net.HttpURLConnection;
2932
import java.net.SocketException;
33+
import java.util.List;
3034

3135
/**
3236
* The single download runnable used for establish one connection and fetch data from it.
@@ -131,7 +135,10 @@ public void run() {
131135
} else {
132136
if (fetchDataTask != null) {
133137
//update currentOffset in ConnectionProfile
134-
connectTask.updateConnectionProfile(fetchDataTask.currentOffset);
138+
final long downloadedOffset = getDownloadedOffset();
139+
if (downloadedOffset > 0) {
140+
connectTask.updateConnectionProfile(fetchDataTask.currentOffset);
141+
}
135142
}
136143
callback.onRetry(e);
137144
}
@@ -147,6 +154,24 @@ public void run() {
147154

148155
}
149156

157+
private long getDownloadedOffset() {
158+
final FileDownloadDatabase database = CustomComponentHolder.getImpl().getDatabaseInstance();
159+
if (connectionIndex >= 0) {
160+
// is multi connection
161+
List<ConnectionModel> connectionModels = database.findConnectionModel(downloadId);
162+
for (ConnectionModel connectionModel : connectionModels) {
163+
if (connectionModel.getIndex() == connectionIndex) {
164+
return connectionModel.getCurrentOffset();
165+
}
166+
}
167+
} else {
168+
// is single connection
169+
FileDownloadModel downloadModel = database.find(downloadId);
170+
return downloadModel.getSoFar();
171+
}
172+
return 0;
173+
}
174+
150175
public static class Builder {
151176
private final ConnectTask.Builder connectTaskBuilder = new ConnectTask.Builder();
152177
private ProcessCallback callback;

‎library/src/main/java/com/liulishuo/filedownloader/download/FetchDataTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private void sync() {
229229
}
230230

231231
if (bufferPersistToDevice) {
232-
final boolean isBelongMultiConnection = hostRunnable != null;
232+
final boolean isBelongMultiConnection = connectionIndex >= 0;
233233
if (isBelongMultiConnection) {
234234
// only need update the connection table.
235235
database.updateConnectionModel(downloadId, connectionIndex, currentOffset);

0 commit comments

Comments
 (0)
Failed to load comments.