Skip to content

Commit ff6802d

Browse files
authored
Merge pull request #4806 from dataCenter430/technical/copy-and-upload-content-uris-resource-leak
[TECHNICAL] Resource leak in CopyAndUploadContentUrisTask
2 parents bcd4ff8 + 9fe453a commit ff6802d

2 files changed

Lines changed: 13 additions & 24 deletions

File tree

changelog/unreleased/4806

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Change: Resource leak in CopyAndUploadContentUrisTask
2+
3+
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.
4+
5+
https://github.com/owncloud/android/issues/4797
6+
https://github.com/owncloud/android/pull/4806

owncloudApp/src/main/java/com/owncloud/android/ui/asynctasks/CopyAndUploadContentUrisTask.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ protected ResultCode doInBackground(Object[] params) {
128128

129129
ResultCode result = ResultCode.UNKNOWN_ERROR;
130130

131-
InputStream inputStream = null;
132-
FileOutputStream outputStream = null;
133131
String fullTempPath = null;
134132
Uri currentUri = null;
135133

@@ -148,19 +146,20 @@ protected ResultCode doInBackground(Object[] params) {
148146
currentRemotePath = uploadPath + UriUtils.getDisplayNameForUri(currentUri, mAppContext);
149147

150148
fullTempPath = FileStorageUtils.getTemporalPath(account.name, spaceId) + currentRemotePath;
151-
inputStream = leakedContentResolver.openInputStream(currentUri);
152149
File cacheFile = new File(fullTempPath);
153150
File tempDir = cacheFile.getParentFile();
154151
if (!tempDir.exists()) {
155152
tempDir.mkdirs();
156153
}
157154
cacheFile.createNewFile();
158-
outputStream = new FileOutputStream(fullTempPath);
159-
byte[] buffer = new byte[4096];
160155

161-
int count;
162-
while ((count = inputStream.read(buffer)) > 0) {
163-
outputStream.write(buffer, 0, count);
156+
try (InputStream inputStream = leakedContentResolver.openInputStream(currentUri);
157+
FileOutputStream outputStream = new FileOutputStream(fullTempPath)) {
158+
byte[] buffer = new byte[4096];
159+
int count;
160+
while ((count = inputStream.read(buffer)) > 0) {
161+
outputStream.write(buffer, 0, count);
162+
}
164163
}
165164

166165
filesToUpload.add(fullTempPath);
@@ -208,22 +207,6 @@ protected ResultCode doInBackground(Object[] params) {
208207
}
209208
}
210209

211-
} finally {
212-
if (inputStream != null) {
213-
try {
214-
inputStream.close();
215-
} catch (Exception e) {
216-
Timber.w("Ignoring exception of inputStream closure");
217-
}
218-
}
219-
220-
if (outputStream != null) {
221-
try {
222-
outputStream.close();
223-
} catch (Exception e) {
224-
Timber.w("Ignoring exception of outStream closure");
225-
}
226-
}
227210
}
228211

229212
return result;

0 commit comments

Comments
 (0)