Use try-with-resources to ensure input stream gets closed.
"new File" never returns null; a File object is created even if no file
exists at that path. Using try-with-resources guarantees that the stream
is closed even if an exception is thrown: go/java-practices/closing#try
Test: m TeleService -j && atest TeleServiceTests --start-avd
Bug: 274764512
Change-Id: I68d50bae2dd000170adbf1f7008bcd39a45c4fce
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index 4cb2c5d..ba1ba3e 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -1121,11 +1121,8 @@
}
PersistableBundle restoredBundle = null;
- File file = null;
- FileInputStream inFile = null;
- try {
- file = new File(mContext.getFilesDir(),fileName);
- inFile = new FileInputStream(file);
+ File file = new File(mContext.getFilesDir(), fileName);
+ try (FileInputStream inFile = new FileInputStream(file)) {
restoredBundle = PersistableBundle.readFromStream(inFile);
String savedVersion = restoredBundle.getString(KEY_VERSION);
@@ -1135,19 +1132,15 @@
loge("Saved version mismatch: " + version + " vs " + savedVersion);
restoredBundle = null;
}
-
- inFile.close();
} catch (FileNotFoundException e) {
// Missing file is normal occurrence that might occur with a new sim or when restoring
// an override file during boot and should not be treated as an error.
- if (file != null) {
- if (isNoSimConfig) {
- logd("File not found: " + file.getPath());
- } else {
- String filePath = file.getPath();
- filePath = getFilePathForLogging(filePath, iccid);
- logd("File not found : " + filePath);
- }
+ if (isNoSimConfig) {
+ logd("File not found: " + file.getPath());
+ } else {
+ String filePath = file.getPath();
+ filePath = getFilePathForLogging(filePath, iccid);
+ logd("File not found : " + filePath);
}
} catch (IOException e) {
loge(e.toString());