From 7f965b0829fc01b33231ac87ec4aa3d254ed8333 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Tue, 15 Sep 2015 23:03:17 -0400 Subject: [PATCH] Properly close I/O streams that were not being properly closed --- .../acr/browser/lightning/database/BookmarkManager.java | 6 +++--- app/src/main/java/acr/browser/lightning/utils/AdBlock.java | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java index 4c3fd2b..5e07d76 100644 --- a/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java +++ b/app/src/main/java/acr/browser/lightning/database/BookmarkManager.java @@ -101,15 +101,14 @@ public class BookmarkManager { final File bookmarksFile = new File(mFilesDir, FILE_BOOKMARKS); BufferedReader bookmarksReader = null; + InputStream inputStream = null; try { - final InputStream inputStream; if (bookmarksFile.exists() && bookmarksFile.isFile()) { inputStream = new FileInputStream(bookmarksFile); } else { inputStream = mContext.getResources().openRawResource(R.raw.default_bookmarks); } - bookmarksReader = - new BufferedReader(new InputStreamReader(inputStream)); + bookmarksReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bookmarksReader.readLine()) != null) { try { @@ -130,6 +129,7 @@ public class BookmarkManager { Log.e(TAG, "Error reading the bookmarks file", e); } finally { Utils.close(bookmarksReader); + Utils.close(inputStream); } mBookmarksMap = bookmarks; mReady = true; diff --git a/app/src/main/java/acr/browser/lightning/utils/AdBlock.java b/app/src/main/java/acr/browser/lightning/utils/AdBlock.java index 0f06386..706596f 100644 --- a/app/src/main/java/acr/browser/lightning/utils/AdBlock.java +++ b/app/src/main/java/acr/browser/lightning/utils/AdBlock.java @@ -57,8 +57,9 @@ public class AdBlock { @Override public void run() { AssetManager asset = context.getAssets(); + BufferedReader reader = null; try { - BufferedReader reader = new BufferedReader(new InputStreamReader( + reader = new BufferedReader(new InputStreamReader( asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME))); String line; while ((line = reader.readLine()) != null) { @@ -67,6 +68,8 @@ public class AdBlock { } catch (IOException e) { Log.wtf(TAG, "Reading blocked domains list from file '" + BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e); + } finally { + Utils.close(reader); } } });