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

fixed some bugs. #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions library/com/zhan_dui/download/CallBackable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zhan_dui.download;


public interface CallBackable<V> {

V callback(DownloadMission mission) throws Exception;
}
23 changes: 19 additions & 4 deletions library/com/zhan_dui/download/DownloadMission.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class DownloadMission {

private static int MISSION_ID_COUNTER = 0;

private CallBackable callBackable;

static class RecoveryRunnableInfo {

private int mStartPosition;
Expand All @@ -75,7 +77,7 @@ static class RecoveryRunnableInfo {
private boolean isFinished = false;

public RecoveryRunnableInfo(int start, int current, int end) {
if (end > start && current > start) {
if (end > start && current >= start) {//if current == start,allow recovery.
mStartPosition = start;
mEndPosition = end;
mCurrentPosition = current;
Expand Down Expand Up @@ -121,10 +123,14 @@ public MissionMonitor(DownloadMission monitorBelongsTo) {
mHostMission = monitorBelongsTo;
}

public void down(int size) {
public void down(int size) throws Exception{
mDownloadedSize.addAndGet(size);
if (mDownloadedSize.intValue() == mHostMission.getFileSize()) {
if (mDownloadedSize.intValue() >= mHostMission.getFileSize()) {// sometimes the downloadsize bigger than the filesize.
mHostMission.setDownloadStatus(FINISHED);
System.out.println(" dsize = "+mDownloadedSize.intValue()+" fs = "+mHostMission.getFileSize());
if (mHostMission.callBackable != null) {// call callbackable to do sth.
mHostMission.callBackable.callback(mHostMission);
}
}
}

Expand Down Expand Up @@ -301,10 +307,15 @@ private void resumeMission() throws IOException {
try {
File progressFile = new File(FileUtils.getSafeDirPath(mProgressDir)
+ File.separator + mProgressFileName);


if (progressFile.exists() == false) {
throw new IOException("Progress File does not exsist");
}

if (progressFile.length() < 100) {//when first run ,the progressFile is almost empty,then exception occurred while jaxb unmarshal.
System.out.println("progressFile < 0.1 k ,no resume mission need!");
return;
}
JAXBContext context = JAXBContext
.newInstance(DownloadMission.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Expand Down Expand Up @@ -538,4 +549,8 @@ public void cancel() {
mDownloadParts.clear();
mThreadPoolRef.cancel(mMissionID);
}

public void setlCallBackable(CallBackable callBackable) {
this.callBackable = callBackable;
}
}
4 changes: 2 additions & 2 deletions library/com/zhan_dui/download/DownloadRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public DownloadRunnable(MissionMonitor monitor, String mFileUrl,
public void run() {
File targetFile;
synchronized (this) {
File dir = new File(mSaveDirectory + File.pathSeparator);
File dir = new File(mSaveDirectory + File.separator);
if (dir.exists() == false) {
dir.mkdirs();
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public void run() {
}
bufferedInputStream.close();
randomAccessFile.close();
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down