From: Reimar Döffinger Date: Fri, 12 Feb 2016 23:28:54 +0000 (+0100) Subject: Fix some resource leaks. X-Git-Url: http://gitweb.fperrin.net/?a=commitdiff_plain;h=d2f222c300c999693f7ecf1ccb91e7baa47b550e;hp=2e8e801d4538ab26392361e23a7a0f1131b73c2c;p=Dictionary.git Fix some resource leaks. --- diff --git a/src/com/hughes/android/dictionary/DictionaryApplication.java b/src/com/hughes/android/dictionary/DictionaryApplication.java index 80bac89..61fd524 100644 --- a/src/com/hughes/android/dictionary/DictionaryApplication.java +++ b/src/com/hughes/android/dictionary/DictionaryApplication.java @@ -314,10 +314,10 @@ public class DictionaryApplication extends Application { DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put( dictionaryInfo.uncompressedFilename, dictionaryInfo); } - reader.close(); } catch (IOException e) { Log.e(LOG, "Failed to load downloadable dictionary lists.", e); } + try { reader.close(); } catch (IOException e) {} } private File dictDir; diff --git a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java index e05da14..673c256 100644 --- a/src/com/hughes/android/dictionary/DictionaryManagerActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryManagerActivity.java @@ -164,20 +164,21 @@ public class DictionaryManagerActivity extends ActionBarActivity { final File localZipFile = new File(Uri.parse(dest).getPath()); + ZipFile zipFile = null; + InputStream zipIn = null; + OutputStream zipOut = null; try { - ZipFile zipFile = new ZipFile(localZipFile); + zipFile = new ZipFile(localZipFile); final ZipEntry zipEntry = zipFile.entries().nextElement(); Log.d(LOG, "Unzipping entry: " + zipEntry.getName()); - final InputStream zipIn = zipFile.getInputStream(zipEntry); + zipIn = zipFile.getInputStream(zipEntry); File targetFile = new File(application.getDictDir(), zipEntry.getName()); if (targetFile.exists()) { targetFile.renameTo(new File(targetFile.getAbsolutePath().replace(".quickdic", ".bak.quickdic"))); targetFile = new File(application.getDictDir(), zipEntry.getName()); } - final OutputStream zipOut = new FileOutputStream(targetFile); + zipOut = new FileOutputStream(targetFile); copyStream(zipIn, zipOut); - zipFile.close(); - zipOut.close(); application.backgroundUpdateDictionaries(dictionaryUpdater); Toast.makeText(context, getString(R.string.installationFinished, dest), Toast.LENGTH_LONG).show(); @@ -190,6 +191,9 @@ public class DictionaryManagerActivity extends ActionBarActivity { new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(msg).setNeutralButton("Close", null).show(); Log.e(LOG, "Failed to unzip.", e); } finally { + try { if (zipOut != null) zipOut.close(); } catch (IOException e) {} + try { if (zipIn != null) zipIn.close(); } catch (IOException e) {} + try { if (zipFile != null) zipFile.close(); } catch (IOException e) {} localZipFile.delete(); } }