PackageManager: Avoid creating encoders for appIds being written /config/sdcardfs
We know they will be ASCII, so use String.getBytes(US-ASCII) which is
much faster than creating a new ICU based CharsetEncoder for UTF-8 every time
we write one of these files (there will be a lot of them).
Test: make + manual.
Change-Id: I8eb62862ea1083e194d3f23ac68bb40aaae5efd5
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java
index 44ae6ee..760df45 100644
--- a/core/java/android/os/FileUtils.java
+++ b/core/java/android/os/FileUtils.java
@@ -316,6 +316,16 @@
stringToFile(file.getAbsolutePath(), string);
}
+ /*
+ * Writes the bytes given in {@code content} to the file whose absolute path
+ * is {@code filename}.
+ */
+ public static void bytesToFile(String filename, byte[] content) throws IOException {
+ try (FileOutputStream fos = new FileOutputStream(filename)) {
+ fos.write(content);
+ }
+ }
+
/**
* Writes string to file. Basically same as "echo -n $string > $filename"
*
diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java
index eef8ce2..e330585 100644
--- a/services/core/java/com/android/server/pm/Settings.java
+++ b/services/core/java/com/android/server/pm/Settings.java
@@ -2470,7 +2470,10 @@
final File file = new File(dir, "appid");
try {
- FileUtils.stringToFile(file, Integer.toString(ps.appId));
+ // Note that the use of US_ASCII here is safe, we're only writing a decimal
+ // number to the file.
+ FileUtils.bytesToFile(file.getAbsolutePath(),
+ Integer.toString(ps.appId).getBytes(StandardCharsets.US_ASCII));
mKernelMapping.put(ps.name, ps.appId);
} catch (IOException ignored) {
}