1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Release input stream on IO exceptions so that next download in the queue does not get stucked

This commit is contained in:
David A. Velasco 2017-04-20 11:30:29 +02:00
parent e6f9eb43d9
commit 02e3a90df3

View File

@ -35,7 +35,6 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
@ -60,7 +59,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
private static final int FORBIDDEN_ERROR = 403; private static final int FORBIDDEN_ERROR = 403;
private static final int SERVICE_UNAVAILABLE_ERROR = 503; private static final int SERVICE_UNAVAILABLE_ERROR = 503;
private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>(); private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>();
private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false); private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
private long mModificationTimestamp = 0; private long mModificationTimestamp = 0;
private String mEtag = ""; private String mEtag = "";
@ -76,7 +75,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
@Override @Override
protected RemoteOperationResult run(OwnCloudClient client) { protected RemoteOperationResult run(OwnCloudClient client) {
RemoteOperationResult result = null; RemoteOperationResult result;
/// download will be performed to a temporal file, then moved to the final location /// download will be performed to a temporal file, then moved to the final location
File tmpFile = new File(getTmpPath()); File tmpFile = new File(getTmpPath());
@ -102,17 +101,18 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
IOException, OperationCancelledException { IOException, OperationCancelledException {
RemoteOperationResult result; RemoteOperationResult result;
int status = -1; int status;
boolean savedFile = false; boolean savedFile = false;
mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath)); mGet = new GetMethod(client.getWebdavUri() + WebdavUtils.encodePath(mRemotePath));
Iterator<OnDatatransferProgressListener> it = null; Iterator<OnDatatransferProgressListener> it;
FileOutputStream fos = null; FileOutputStream fos = null;
BufferedInputStream bis = null;
try { try {
status = client.executeMethod(mGet); status = client.executeMethod(mGet);
if (isSuccess(status)) { if (isSuccess(status)) {
targetFile.createNewFile(); targetFile.createNewFile();
BufferedInputStream bis = new BufferedInputStream(mGet.getResponseBodyAsStream()); bis = new BufferedInputStream(mGet.getResponseBodyAsStream());
fos = new FileOutputStream(targetFile); fos = new FileOutputStream(targetFile);
long transferred = 0; long transferred = 0;
@ -122,7 +122,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
Long.parseLong(contentLength.getValue()) : 0; Long.parseLong(contentLength.getValue()) : 0;
byte[] bytes = new byte[4096]; byte[] bytes = new byte[4096];
int readResult = 0; int readResult;
while ((readResult = bis.read(bytes)) != -1) { while ((readResult = bis.read(bytes)) != -1) {
synchronized (mCancellationRequested) { synchronized (mCancellationRequested) {
if (mCancellationRequested.get()) { if (mCancellationRequested.get()) {
@ -147,7 +147,7 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
modificationTime = mGet.getResponseHeader("last-modified"); modificationTime = mGet.getResponseHeader("last-modified");
} }
if (modificationTime != null) { if (modificationTime != null) {
Date d = WebdavUtils.parseResponseDate((String) modificationTime.getValue()); Date d = WebdavUtils.parseResponseDate(modificationTime.getValue());
mModificationTimestamp = (d != null) ? d.getTime() : 0; mModificationTimestamp = (d != null) ? d.getTime() : 0;
} else { } else {
Log_OC.e(TAG, "Could not read modification time from response downloading " + mRemotePath); Log_OC.e(TAG, "Could not read modification time from response downloading " + mRemotePath);
@ -163,15 +163,20 @@ public class DownloadRemoteFileOperation extends RemoteOperation {
// TODO some kind of error control! // TODO some kind of error control!
} }
} else if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR){ } else if (status != FORBIDDEN_ERROR && status != SERVICE_UNAVAILABLE_ERROR) {
client.exhaustResponse(mGet.getResponseBodyAsStream()); client.exhaustResponse(mGet.getResponseBodyAsStream());
} // else, body read by RemoteOeprationResult constructor } // else, body read by RemoteOeprationResult constructor
result = new RemoteOperationResult(isSuccess(status), mGet); result = new RemoteOperationResult(isSuccess(status), mGet);
/*} catch (Exception e) {
client.exhaustResponse(mGet.getResponseBodyAsStream());
throw e;*/
} finally { } finally {
if (fos != null) fos.close(); if (fos != null) fos.close();
if (bis != null) bis.close();
if (!savedFile && targetFile.exists()) { if (!savedFile && targetFile.exists()) {
targetFile.delete(); targetFile.delete();
} }