Skip to content

Commit 6b3ff25

Browse files
committed
safely close file streams
1 parent b396b5e commit 6b3ff25

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

android/app/src/main/java/com/expensify/chat/customairshipextender/NotificationCache.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,55 @@ public static void setNotificationData(long reportID, NotificationData data) {
6060

6161
private static void writeToInternalStorage() {
6262
Context context = UAirship.getApplicationContext();
63+
64+
FileOutputStream fos = null;
65+
ObjectOutputStream oos = null;
6366
try {
6467
File outputFile = new File(context.getFilesDir(), CACHE_FILE_NAME);
65-
FileOutputStream fos = new FileOutputStream(outputFile);
66-
ObjectOutputStream oos = new ObjectOutputStream(fos);
67-
68+
fos = new FileOutputStream(outputFile);
69+
oos = new ObjectOutputStream(fos);
6870
oos.writeObject(cache);
69-
70-
oos.close();
71-
fos.close();
7271
} catch (IOException e) {
7372
e.printStackTrace();
73+
} finally {
74+
try {
75+
if (oos != null) {
76+
oos.close();
77+
}
78+
if (fos != null) {
79+
fos.close();
80+
}
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
7484
}
7585
}
7686

7787
private static HashMap<String, NotificationData> readFromInternalStorage() {
7888
HashMap<String, NotificationData> result;
7989
Context context = UAirship.getApplicationContext();
90+
91+
FileInputStream fis = null;
92+
ObjectInputStream ois = null;
8093
try {
8194
File fileCache = new File(context.getFilesDir(), CACHE_FILE_NAME);
82-
FileInputStream fis = new FileInputStream(fileCache);
83-
ObjectInputStream ois = new ObjectInputStream(fis);
84-
95+
fis = new FileInputStream(fileCache);
96+
ois = new ObjectInputStream(fis);
8597
result = (HashMap<String, NotificationData>) ois.readObject();
86-
87-
ois.close();
88-
fis.close();
8998
} catch (IOException | ClassNotFoundException e) {
9099
e.printStackTrace();
91100
result = new HashMap<>();
101+
} finally {
102+
try {
103+
if (ois != null) {
104+
ois.close();
105+
}
106+
if (fis != null) {
107+
fis.close();
108+
}
109+
} catch (IOException e) {
110+
e.printStackTrace();
111+
}
92112
}
93113

94114
return result;

0 commit comments

Comments
 (0)