From 07edcb6d00fafa901735b5531de46636e4aefc4e Mon Sep 17 00:00:00 2001 From: demasterr Date: Sat, 9 Mar 2019 15:09:06 +0100 Subject: [PATCH] Leak in InputStreams Same concept as before. However, this time the inputstreams were not closed at all. With the try-with-resources the inputstreams will also be closed and will prevent a leak from happening. To come back to my last commit, the usage of 'memory leak' was wrong. This is a leak, not a memory leak. The garbage collector may eventually close the resources. However, some operating systems will not allow many simultaneous open resources. --- .../com/mapswithme/util/log/ZipLogsTask.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/android/src/com/mapswithme/util/log/ZipLogsTask.java b/android/src/com/mapswithme/util/log/ZipLogsTask.java index c607eb3c46..b880d8d289 100644 --- a/android/src/com/mapswithme/util/log/ZipLogsTask.java +++ b/android/src/com/mapswithme/util/log/ZipLogsTask.java @@ -58,8 +58,6 @@ class ZipLogsTask implements Runnable try(FileOutputStream dest = new FileOutputStream(toLocation, false); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest))) { - BufferedInputStream origin; - if (sourceFile.isDirectory()) { zipSubFolder(out, sourceFile, sourceFile.getParent().length()); @@ -67,14 +65,19 @@ class ZipLogsTask implements Runnable else { byte data[] = new byte[BUFFER_SIZE]; - FileInputStream fi = new FileInputStream(sourcePath); - origin = new BufferedInputStream(fi, BUFFER_SIZE); - ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath)); - out.putNextEntry(entry); - int count; - while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) + try(FileInputStream fi = new FileInputStream(sourcePath); + BufferedInputStream origin = new BufferedInputStream(fi, BUFFER_SIZE);) { + ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath)); + out.putNextEntry(entry); + int count; + while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { + out.write(data, 0, count); + } + } catch (Exception e) { - out.write(data, 0, count); + Log.e(TAG, "Failed to read zip file entry '" + sourcePath +"' to location '" + + toLocation + "'", e); + return false; } } }