diff --git a/library/com/zhan_dui/download/CallBackable.java b/library/com/zhan_dui/download/CallBackable.java new file mode 100644 index 0000000..9a7b867 --- /dev/null +++ b/library/com/zhan_dui/download/CallBackable.java @@ -0,0 +1,7 @@ +package com.zhan_dui.download; + + +public interface CallBackable { + + V callback(DownloadMission mission) throws Exception; +} diff --git a/library/com/zhan_dui/download/DownloadMission.java b/library/com/zhan_dui/download/DownloadMission.java index 1468356..71ab453 100644 --- a/library/com/zhan_dui/download/DownloadMission.java +++ b/library/com/zhan_dui/download/DownloadMission.java @@ -67,6 +67,8 @@ public class DownloadMission { private static int MISSION_ID_COUNTER = 0; + private CallBackable callBackable; + static class RecoveryRunnableInfo { private int mStartPosition; @@ -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; @@ -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); + } } } @@ -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(); @@ -538,4 +549,8 @@ public void cancel() { mDownloadParts.clear(); mThreadPoolRef.cancel(mMissionID); } + + public void setlCallBackable(CallBackable callBackable) { + this.callBackable = callBackable; + } } diff --git a/library/com/zhan_dui/download/DownloadRunnable.java b/library/com/zhan_dui/download/DownloadRunnable.java index c3d92e1..f6dd630 100644 --- a/library/com/zhan_dui/download/DownloadRunnable.java +++ b/library/com/zhan_dui/download/DownloadRunnable.java @@ -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(); } @@ -119,7 +119,7 @@ public void run() { } bufferedInputStream.close(); randomAccessFile.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } }