diff --git a/changelog/unreleased/4806 b/changelog/unreleased/4806 new file mode 100644 index 00000000000..114f21d12f2 --- /dev/null +++ b/changelog/unreleased/4806 @@ -0,0 +1,6 @@ +Bugfix: Fix resource leak in CopyAndUploadContentUrisTask + +Input and output streams have been closed per URI iteration using try-with-resources to prevent file descriptor and memory leaks when copying multiple content URIs. + +https://github.com/owncloud/android/issues/4797 +https://github.com/owncloud/android/pull/4806 \ No newline at end of file diff --git a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java index 65eaab5b4b0..d689286b6d5 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java +++ b/owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java @@ -128,8 +128,6 @@ protected ResultCode doInBackground(Object[] params) { ResultCode result = ResultCode.UNKNOWN_ERROR; - InputStream inputStream = null; - FileOutputStream outputStream = null; String fullTempPath = null; Uri currentUri = null; @@ -148,19 +146,20 @@ protected ResultCode doInBackground(Object[] params) { currentRemotePath = uploadPath + UriUtils.getDisplayNameForUri(currentUri, mAppContext); fullTempPath = FileStorageUtils.getTemporalPath(account.name, spaceId) + currentRemotePath; - inputStream = leakedContentResolver.openInputStream(currentUri); File cacheFile = new File(fullTempPath); File tempDir = cacheFile.getParentFile(); if (!tempDir.exists()) { tempDir.mkdirs(); } cacheFile.createNewFile(); - outputStream = new FileOutputStream(fullTempPath); - byte[] buffer = new byte[4096]; - int count; - while ((count = inputStream.read(buffer)) > 0) { - outputStream.write(buffer, 0, count); + try (InputStream inputStream = leakedContentResolver.openInputStream(currentUri); + FileOutputStream outputStream = new FileOutputStream(fullTempPath)) { + byte[] buffer = new byte[4096]; + int count; + while ((count = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, count); + } } filesToUpload.add(fullTempPath); @@ -208,22 +207,6 @@ protected ResultCode doInBackground(Object[] params) { } } - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (Exception e) { - Timber.w("Ignoring exception of inputStream closure"); - } - } - - if (outputStream != null) { - try { - outputStream.close(); - } catch (Exception e) { - Timber.w("Ignoring exception of outStream closure"); - } - } } return result;