From 988815ef61ca8ed8d145455244cbc0d81cf9284e Mon Sep 17 00:00:00 2001 From: zhaoyong <12374011@163.com> Date: Thu, 3 Dec 2015 00:12:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=B0=8Fbug,=E5=88=AB=E4=B8=8E=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E6=94=BE=E7=9B=AE=E5=BD=95=EF=BC=8C=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=EF=BC=8C=E6=96=87=E4=BB=B6=E6=8C=89=E5=9D=97?= =?UTF-8?q?=E5=88=86=E5=89=B2=E7=AD=89=E3=80=82=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=BA=86=E4=B8=AACallBackable=E6=8E=A5=E5=8F=A3=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=9C=A8=E4=B8=8B=E8=BD=BD=E5=AE=8C=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=9B=9E=E8=B0=83=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhan_dui/download/DownloadMission.java | 23 +++++++++++++++---- .../zhan_dui/download/DownloadRunnable.java | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-) 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(); } } From bd88422671e6f73806050cc529fed216971bc865 Mon Sep 17 00:00:00 2001 From: zhaoyong <12374011@163.com> Date: Thu, 3 Dec 2015 23:48:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=B0=8Fbug,=E5=88=AB=E4=B8=8E=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AD=98=E6=94=BE=E7=9B=AE=E5=BD=95=EF=BC=8C=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=EF=BC=8C=E6=96=87=E4=BB=B6=E6=8C=89=E5=9D=97?= =?UTF-8?q?=E5=88=86=E5=89=B2=E7=AD=89=E3=80=82=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=BA=86=E4=B8=AACallBackable=E6=8E=A5=E5=8F=A3=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=9C=A8=E4=B8=8B=E8=BD=BD=E5=AE=8C=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=9B=9E=E8=B0=83=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/com/zhan_dui/download/CallBackable.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 library/com/zhan_dui/download/CallBackable.java 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; +}