diff --git a/src/com/androidquery/AbstractAQuery.java b/src/com/androidquery/AbstractAQuery.java index 07141fa9..a166d0ff 100644 --- a/src/com/androidquery/AbstractAQuery.java +++ b/src/com/androidquery/AbstractAQuery.java @@ -2707,5 +2707,43 @@ public T download(String url, File target, Object handler, String callback){ return download(url, target, cb); } - + + + /** + * Download a file asynchronously. + * + * @param url url + * @param target the file to be saved + * @param resume resume download flag + * @param cb callback to be called when done + * @return self + * + */ + + public T download(String url, File target, boolean resume, AjaxCallback cb){ + + cb.url(url).type(File.class).targetFile(target).resume(resume); + return ajax(cb); + + } + + /** + * Download a file asynchronously. + * + * @param url url + * @param target the file to be saved + * @param resume resume download flag + * @param handler the callback handler + * @param callback the callback method + * @return self + * + */ + + public T download(String url, File target, boolean resume, Object handler, String callback){ + + AjaxCallback cb = new AjaxCallback(); + cb.weakHandler(handler, callback); + return download(url, target, resume, cb); + + } } diff --git a/src/com/androidquery/callback/AbstractAjaxCallback.java b/src/com/androidquery/callback/AbstractAjaxCallback.java index 1fbb68a7..4e5d5252 100644 --- a/src/com/androidquery/callback/AbstractAjaxCallback.java +++ b/src/com/androidquery/callback/AbstractAjaxCallback.java @@ -165,6 +165,31 @@ private void clear(){ transformer = null; ah = null; act = null; + + callback = null; + targetFile = null; + cacheDir = null; + cookies = null; + headers = null; + params = null; + proxy = null; + networkUrl = null; + + abort = false; + blocked = false; + completed = false; + fileCache = false; + memCache = false; + reauth = false; + refresh = false; + + method = Constants.METHOD_DETECT; + policy = Constants.CACHE_DEFAULT; + lastStatus = 200; + expire = 0; + retry = 0; + timeout = 0; + uiCallback = true; } /** @@ -1183,6 +1208,11 @@ private void filePut(){ } } + }else if(status.getFile() != null && status.getSource() == AjaxStatus.NETWORK && status.getInvalid()){ + File file = status.getFile(); + if (file.exists()) { + file.delete(); + } } }catch(Exception e){ AQUtility.debug(e); @@ -1523,6 +1553,11 @@ private HttpResponse execute(HttpUriRequest hr, DefaultHttpClient client, HttpCo return response; } + private boolean resume; + public K resume(boolean resume){ + this.resume = resume; + return self(); + } private void httpDo(HttpUriRequest hr, String url, Map headers, AjaxStatus status) throws ClientProtocolException, IOException{ @@ -1546,6 +1581,13 @@ private void httpDo(HttpUriRequest hr, String url, Map headers, hr.addHeader("Cookie", cookie); } + if(resume && targetFile != null){ + long len = targetFile.length(); + if (len > 0){ + hr.addHeader("Range", "bytes=" + len + "-"); + } + } + if(ah != null){ ah.applyToken(this, hr); } @@ -1642,8 +1684,8 @@ private void httpDo(HttpUriRequest hr, String url, Map headers, if(file == null){ os = new PredefinedBAOS(size); }else{ - file.createNewFile(); - os = new BufferedOutputStream(new FileOutputStream(file)); + if(!resume || !file.exists()) file.createNewFile(); + os = new BufferedOutputStream(new FileOutputStream(file, true)); } is = entity.getContent(); @@ -1663,7 +1705,15 @@ private void httpDo(HttpUriRequest hr, String url, Map headers, file = null; } } - + + }catch(IOException e){ + if (file != null && !resume) { + AQUtility.close(os); + os = null; + file.delete(); + } + throw e; + }finally{ AQUtility.close(is); AQUtility.close(os);