Bug 1261965 - Correct leak FileOutputStream in FilePickerResultHandler. r=grisha
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 04 Apr 2016 14:51:11 -0700
changeset 292512 18434342c5638551756e00bec2463c30a4a9abcc
parent 292511 d5064e39467cc518c50603cb5603caad8e9510e0
child 292513 81f58f4715ac5ec76ee7514ac172c2c66b1df6e1
push id74863
push userryanvm@gmail.com
push dateSat, 09 Apr 2016 19:23:17 +0000
treeherdermozilla-inbound@ee048ce0f8d5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgrisha
bugs1261965
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1261965 - Correct leak FileOutputStream in FilePickerResultHandler. r=grisha MozReview-Commit-ID: 6gfRS4SPM8E
mobile/android/base/java/org/mozilla/gecko/FilePickerResultHandler.java
--- a/mobile/android/base/java/org/mozilla/gecko/FilePickerResultHandler.java
+++ b/mobile/android/base/java/org/mozilla/gecko/FilePickerResultHandler.java
@@ -210,21 +210,22 @@ class FilePickerResultHandler implements
                     String mimeType = cr.getType(uri);
                     fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType);
                 } else {
                     fileExt = name.substring(period);
                     fileName += name.substring(0, period);
                 }
 
                 // Now write the data to the temp file
+                FileOutputStream fos = null;
                 try {
                     cacheDir.mkdir();
 
                     File file = File.createTempFile(fileName, fileExt, cacheDir);
-                    FileOutputStream fos = new FileOutputStream(file);
+                    fos = new FileOutputStream(file);
                     InputStream is = cr.openInputStream(uri);
                     byte[] buf = new byte[4096];
                     int len = is.read(buf);
                     while (len != -1) {
                         fos.write(buf, 0, len);
                         len = is.read(buf);
                     }
                     fos.close();
@@ -232,16 +233,22 @@ class FilePickerResultHandler implements
                     tempFile = file.getAbsolutePath();
                     sendResult((tempFile == null) ? "" : tempFile);
 
                     if (tabId > -1 && !TextUtils.isEmpty(tempFile)) {
                         Tabs.registerOnTabsChangedListener(this);
                     }
                 } catch(IOException ex) {
                     Log.i(LOGTAG, "Error writing file", ex);
+                } finally {
+                    if (fos != null) {
+                        try {
+                            fos.close();
+                        } catch (IOException e) { /* not much to do here */ }
+                    }
                 }
             } else {
                 sendResult("");
             }
         }
 
         @Override
         public void onLoaderReset(Loader<Cursor> loader) { }