Properly close I/O streams that were not being properly closed

This commit is contained in:
Anthony Restaino 2015-09-15 23:03:17 -04:00
parent 5c8fd41c6b
commit 7f965b0829
2 changed files with 7 additions and 4 deletions

View File

@ -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;

View File

@ -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);
}
}
});