Skip to content

Commit a734af6

Browse files
authored
Replace try-with-resources with try and finally so that we don't have desugaring issues (#626)
* update rc * address comments * fix file not found exception * no if needed * gJF
1 parent bd6f553 commit a734af6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigStorageClient.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ private ConfigStorageClient(Context context, String fileName) {
7676
*/
7777
public synchronized Void write(ConfigContainer container) throws IOException {
7878
// TODO(issues/262): Consider using the AtomicFile class instead.
79-
try (FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE)) {
79+
FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
80+
try {
8081
outputStream.write(container.toString().getBytes(JSON_STRING_ENCODING));
82+
} finally {
83+
outputStream.close();
8184
}
8285
return null;
8386
}
@@ -90,7 +93,13 @@ public synchronized Void write(ConfigContainer container) throws IOException {
9093
*/
9194
@Nullable
9295
public synchronized ConfigContainer read() throws IOException {
93-
try (FileInputStream fileInputStream = context.openFileInput(fileName)) {
96+
try {
97+
context.openFileInput(fileName);
98+
} catch (FileNotFoundException e) {
99+
return null;
100+
}
101+
FileInputStream fileInputStream = context.openFileInput(fileName);
102+
try {
94103
byte[] bytes = new byte[fileInputStream.available()];
95104
fileInputStream.read(bytes, 0, bytes.length);
96105
String containerJsonString = new String(bytes, JSON_STRING_ENCODING);
@@ -100,6 +109,8 @@ public synchronized ConfigContainer read() throws IOException {
100109
} catch (JSONException | FileNotFoundException e) {
101110
// File might not have been written to yet, so this not an irrecoverable error.
102111
return null;
112+
} finally {
113+
fileInputStream.close();
103114
}
104115
}
105116

0 commit comments

Comments
 (0)