Bug 1267486 - Replace FileUtils.getFileContents with read*FromFile. r=sebastian draft
authorMichael Comella <michael.l.comella@gmail.com>
Tue, 26 Apr 2016 15:36:04 -0700
changeset 356712 5a11566d50a215123df52361bcba1ae1ab31e307
parent 356711 9ef8586a7cfa458082c83484117c8f5c31fce7cb
child 519458 785aa2d5d392727f8cebebbd8590fe0b1fedbdbc
push id16574
push usermichael.l.comella@gmail.com
push dateTue, 26 Apr 2016 22:48:19 +0000
reviewerssebastian
bugs1267486
milestone48.0a1
Bug 1267486 - Replace FileUtils.getFileContents with read*FromFile. r=sebastian Reasons for replacement: * The old method was untested * The Scanner class is supposedly slower than using Streams directly (which the new methods do) * If possible, it's generally better not to duplicate solutions - using the Scanner works around the infrastructure this changeset series built (i.e. using Streams). MozReview-Commit-ID: 6ESPss29emU
mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
mobile/android/base/java/org/mozilla/gecko/search/SearchEngineManager.java
mobile/android/base/java/org/mozilla/gecko/util/FileUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
+++ b/mobile/android/base/java/org/mozilla/gecko/distribution/Distribution.java
@@ -355,17 +355,17 @@ public class Distribution {
     public DistributionDescriptor getDescriptor() {
         File descFile = getDistributionFile("preferences.json");
         if (descFile == null) {
             // Logging and existence checks are handled in getDistributionFile.
             return null;
         }
 
         try {
-            JSONObject all = new JSONObject(FileUtils.getFileContents(descFile));
+            JSONObject all = FileUtils.readJSONObjectFromFile(descFile);
 
             if (!all.has("Global")) {
                 Log.e(LOGTAG, "Distribution preferences.json has no Global entry!");
                 return null;
             }
 
             return new DistributionDescriptor(all.getJSONObject("Global"));
 
@@ -387,17 +387,17 @@ public class Distribution {
     public JSONObject getAndroidPreferences() {
         final File descFile = getDistributionFile("preferences.json");
         if (descFile == null) {
             // Logging and existence checks are handled in getDistributionFile.
             return new JSONObject();
         }
 
         try {
-            final JSONObject all = new JSONObject(FileUtils.getFileContents(descFile));
+            final JSONObject all = FileUtils.readJSONObjectFromFile(descFile);
 
             if (!all.has("AndroidPreferences")) {
                 return new JSONObject();
             }
 
             return all.getJSONObject("AndroidPreferences");
 
         } catch (IOException e) {
@@ -414,17 +414,17 @@ public class Distribution {
     public JSONArray getBookmarks() {
         File bookmarks = getDistributionFile("bookmarks.json");
         if (bookmarks == null) {
             // Logging and existence checks are handled in getDistributionFile.
             return null;
         }
 
         try {
-            return new JSONArray(FileUtils.getFileContents(bookmarks));
+            return new JSONArray(FileUtils.readStringFromFile(bookmarks));
         } catch (IOException e) {
             Log.e(LOGTAG, "Error getting bookmarks", e);
             Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION);
             return null;
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error parsing bookmarks.json", e);
             Telemetry.addToHistogram(HISTOGRAM_CODE_CATEGORY, CODE_CATEGORY_MALFORMED_DISTRIBUTION);
             return null;
--- a/mobile/android/base/java/org/mozilla/gecko/search/SearchEngineManager.java
+++ b/mobile/android/base/java/org/mozilla/gecko/search/SearchEngineManager.java
@@ -296,17 +296,17 @@ public class SearchEngineManager impleme
         }
 
         final File prefFile = distribution.getDistributionFile("preferences.json");
         if (prefFile == null) {
             return null;
         }
 
         try {
-            final JSONObject all = new JSONObject(FileUtils.getFileContents(prefFile));
+            final JSONObject all = FileUtils.readJSONObjectFromFile(prefFile);
 
             // First, look for a default locale specified by the distribution.
             if (all.has("Preferences")) {
                 final JSONObject prefs = all.getJSONObject("Preferences");
                 if (prefs.has(PREF_GECKO_DEFAULT_LOCALE)) {
                     Log.d(LOG_TAG, "Found default searchplugin locale in distribution Preferences.");
                     distributionLocale = prefs.getString(PREF_GECKO_DEFAULT_LOCALE);
                 }
--- a/mobile/android/base/java/org/mozilla/gecko/util/FileUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/FileUtils.java
@@ -89,29 +89,16 @@ public class FileUtils {
                 }
             }
         }
 
         // Even if this is a dir, it should now be empty and delete should work
         return file.delete();
     }
 
-    // Shortcut to slurp a file without messing around with streams.
-    public static String getFileContents(File file) throws IOException {
-        Scanner scanner = null;
-        try {
-            scanner = new Scanner(file, "UTF-8");
-            return scanner.useDelimiter("\\A").next();
-        } finally {
-            if (scanner != null) {
-                scanner.close();
-            }
-        }
-    }
-
     /**
      * A generic solution to read a JSONObject from a file. See
      * {@link #readStringFromFile(File)} for more details.
      *
      * @throws IOException if the file is empty, or another IOException occurs
      * @throws JSONException if the file could not be converted to a JSONObject.
      */
     public static JSONObject readJSONObjectFromFile(final File file) throws IOException, JSONException {