@@ -76,8 +76,11 @@ private ConfigStorageClient(Context context, String fileName) {
76
76
*/
77
77
public synchronized Void write (ConfigContainer container ) throws IOException {
78
78
// 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 {
80
81
outputStream .write (container .toString ().getBytes (JSON_STRING_ENCODING ));
82
+ } finally {
83
+ outputStream .close ();
81
84
}
82
85
return null ;
83
86
}
@@ -90,7 +93,13 @@ public synchronized Void write(ConfigContainer container) throws IOException {
90
93
*/
91
94
@ Nullable
92
95
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 {
94
103
byte [] bytes = new byte [fileInputStream .available ()];
95
104
fileInputStream .read (bytes , 0 , bytes .length );
96
105
String containerJsonString = new String (bytes , JSON_STRING_ENCODING );
@@ -100,6 +109,8 @@ public synchronized ConfigContainer read() throws IOException {
100
109
} catch (JSONException | FileNotFoundException e ) {
101
110
// File might not have been written to yet, so this not an irrecoverable error.
102
111
return null ;
112
+ } finally {
113
+ fileInputStream .close ();
103
114
}
104
115
}
105
116
0 commit comments