Bug 1474967: Fix resource leaks. draft
authorRobert Bartlensky <rbartlensky@mozilla.com>
Wed, 11 Jul 2018 18:30:25 +0100
changeset 817315 351fae1c2170
parent 817312 fe17acc6e291
push id116010
push userbmo:rbartlensky@mozilla.com
push dateThu, 12 Jul 2018 10:29:40 +0000
bugs1474967
milestone63.0a1
Bug 1474967: Fix resource leaks. MozReview-Commit-ID: GzENpXSdltH
mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoMediaDrmBridgeV21.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoMediaDrmBridgeV21.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoMediaDrmBridgeV21.java
@@ -468,34 +468,35 @@ public class GeckoMediaDrmBridgeV21 impl
         PostRequestTask(int promiseId, String url, byte[] drmRequest) {
             this.mPromiseId = promiseId;
             this.mURL = url;
             this.mDrmRequest = drmRequest;
         }
 
         @Override
         protected Void doInBackground(Void... params) {
+            HttpURLConnection urlConnection = null;
+            BufferedReader in = null;
             try {
                 URI finalURI = new URI(mURL + "&signedRequest=" + URLEncoder.encode(new String(mDrmRequest), "UTF-8"));
-                HttpURLConnection urlConnection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(finalURI);
+                urlConnection = (HttpURLConnection) ProxySelector.openConnectionWithProxy(finalURI);
                 urlConnection.setRequestMethod("POST");
                 if (DEBUG) Log.d(LOGTAG, "Provisioning, posting url =" + finalURI.toString());
 
                 // Add data
                 urlConnection.setRequestProperty("Accept", "*/*");
                 urlConnection.setRequestProperty("User-Agent", getCDMUserAgent());
                 urlConnection.setRequestProperty("Content-Type", "application/json");
 
                 // Execute HTTP Post Request
                 urlConnection.connect();
 
                 int responseCode = urlConnection.getResponseCode();
                 if (responseCode == HttpURLConnection.HTTP_OK) {
-                    BufferedReader in =
-                      new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StringUtils.UTF_8));
+                    in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StringUtils.UTF_8));
                     String inputLine;
                     StringBuffer response = new StringBuffer();
 
                     while ((inputLine = in.readLine()) != null) {
                         response.append(inputLine);
                     }
                     in.close();
                     mResponseBody = String.valueOf(response).getBytes(StringUtils.UTF_8);
@@ -503,16 +504,27 @@ public class GeckoMediaDrmBridgeV21 impl
                     if (mResponseBody != null) Log.d(LOGTAG, "response length=" + mResponseBody.length);
                 } else {
                     Log.d(LOGTAG, "Provisioning, server returned HTTP error code :" + responseCode);
                 }
             } catch (IOException e) {
                 Log.e(LOGTAG, "Got exception during posting provisioning request ...", e);
             } catch (URISyntaxException e) {
                 Log.e(LOGTAG, "Got exception during creating uri ...", e);
+            } finally {
+                if (urlConnection != null) {
+                    urlConnection.disconnect();
+                }
+                try {
+                    if (in != null) {
+                        in.close();
+                    }
+                } catch (IOException e) {
+                    Log.e(LOGTAG, "Exception during closing in ...", e);
+                }
             }
             return null;
         }
 
         @Override
         protected void onPostExecute(Void v) {
             onProvisionResponse(mPromiseId, mResponseBody);
         }