-
- public boolean isSuccess(int status) {
- return (status == HttpStatus.SC_OK);
- }
-
-
- protected int downloadFile(WebdavClient client, File targetFile) throws HttpException, IOException, OperationCancelledException {
- int status = -1;
- boolean savedFile = false;
- GetMethod get = new GetMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
- Iterator<OnDatatransferProgressListener> it = null;
-
- try {
- status = client.executeMethod(get);
- if (isSuccess(status)) {
- targetFile.createNewFile();
- BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
- FileOutputStream fos = new FileOutputStream(targetFile);
- long transferred = 0;
-
- byte[] bytes = new byte[4096];
- int readResult = 0;
- while ((readResult = bis.read(bytes)) != -1) {
- synchronized(mCancellationRequested) {
- if (mCancellationRequested) {
- throw new OperationCancelledException();
- }
- }
- fos.write(bytes, 0, readResult);
- transferred += readResult;
- it = mDataTransferListeners.iterator();
- while (it.hasNext()) {
- it.next().onTransferProgress(readResult, transferred, mSize, targetFile.getName());
- }
- }
- fos.close();
- savedFile = true;
-
- } else {
- client.exhaustResponse(get.getResponseBodyAsStream());
- }
-
- } finally {
- if (!savedFile && targetFile.exists()) {
- targetFile.delete();
- }
- get.releaseConnection(); // let the connection available for other methods