Skip to content

Commit

Permalink
fix: avoid error/pause status is covered by other processing-status w…
Browse files Browse the repository at this point in the history
…hich will cause resume-failed, task-never-end

this closes #769, closes #764, closes #761, closes #763, closes #721, closes #716
  • Loading branch information
Jacksgong committed Oct 12, 2017
1 parent 4cfef75 commit ca2f276
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
Expand Up @@ -107,6 +107,8 @@ public class DownloadLaunchRunnable implements Runnable, ProcessCallback {

private volatile boolean alive;
private volatile boolean paused;
private volatile boolean error;
private volatile Exception errorException;

private String redirectedUrl;

Expand Down Expand Up @@ -173,8 +175,6 @@ public void pause() {
// if runnable is null, then that one must be completed and removed
}
}

statusCallback.onPaused();
}

public void pending() {
Expand Down Expand Up @@ -336,6 +336,17 @@ public void run() {
} while (true);
} finally {
alive = false;
if (paused) {
statusCallback.onPaused();
} else if (error) {
statusCallback.onError(errorException);
} else {
try {
statusCallback.onCompleted();
} catch (IOException e) {
statusCallback.onError(e);
}
}
}
}

Expand Down Expand Up @@ -715,8 +726,7 @@ public void onProgress(long increaseBytes) {
}

@Override
public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long endOffset)
throws IOException {
public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long endOffset) {
if (paused) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "the task[%d] has already been paused, so pass the" +
Expand All @@ -725,8 +735,6 @@ public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long en
return;
}

boolean allConnectionCompleted = false;

final int doneConnectionIndex = doneRunnable == null ? -1 : doneRunnable.connectionIndex;
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "the connection has been completed(%d): [%d, %d) %d",
Expand All @@ -738,20 +746,10 @@ public void onCompleted(DownloadRunnable doneRunnable, long startOffset, long en
FileDownloadLog.e(this, "the single task not completed corrected(%d, %d != %d) " +
"for task(%d)", startOffset, endOffset, model.getTotal(), model.getId());
}

allConnectionCompleted = true;
} else {
synchronized (downloadRunnableList) {
downloadRunnableList.remove(doneRunnable);
}

if (downloadRunnableList.size() <= 0) {
allConnectionCompleted = true;
}
}

if (allConnectionCompleted) {
statusCallback.onCompleted();
}
}

Expand All @@ -776,6 +774,9 @@ public boolean isRetry(Exception exception) {

@Override
public void onError(Exception exception) {
error = true;
errorException = exception;

if (paused) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "the task[%d] has already been paused, so pass the" +
Expand All @@ -793,8 +794,6 @@ public void onError(Exception exception) {
// if runnable is null, then that one must be completed and removed
}
}

statusCallback.onError(exception);
}

@Override
Expand Down
Expand Up @@ -16,16 +16,14 @@

package com.liulishuo.filedownloader.download;

import java.io.IOException;

/**
* The process event callbacks.
*/
public interface ProcessCallback {

void onProgress(long increaseBytes);

void onCompleted(DownloadRunnable doneRunnable, long startOffset, long endOffset) throws IOException;
void onCompleted(DownloadRunnable doneRunnable, long startOffset, long endOffset);

boolean isRetry(Exception exception);

Expand Down
Expand Up @@ -88,7 +88,7 @@ public interface FileDownloadOutputStream {
* @see java.io.RandomAccessFile#seek(long)
* @see java.nio.channels.FileChannel#position(long)
*/
void seek(long offset) throws IOException;
void seek(long offset) throws IOException, IllegalAccessException;

/**
* Sets the length of this file.
Expand All @@ -108,5 +108,5 @@ public interface FileDownloadOutputStream {
* @throws IllegalAccessException If in this output stream doesn't support this function.
* @see java.io.RandomAccessFile#setLength(long)
*/
void setLength(final long newLength) throws IOException;
void setLength(final long newLength) throws IOException, IllegalAccessException;
}

0 comments on commit ca2f276

Please sign in to comment.