@@ -60,35 +60,55 @@ public static void setNotificationData(long reportID, NotificationData data) {
60
60
61
61
private static void writeToInternalStorage () {
62
62
Context context = UAirship .getApplicationContext ();
63
+
64
+ FileOutputStream fos = null ;
65
+ ObjectOutputStream oos = null ;
63
66
try {
64
67
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 );
68
70
oos .writeObject (cache );
69
-
70
- oos .close ();
71
- fos .close ();
72
71
} catch (IOException e ) {
73
72
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
+ }
74
84
}
75
85
}
76
86
77
87
private static HashMap <String , NotificationData > readFromInternalStorage () {
78
88
HashMap <String , NotificationData > result ;
79
89
Context context = UAirship .getApplicationContext ();
90
+
91
+ FileInputStream fis = null ;
92
+ ObjectInputStream ois = null ;
80
93
try {
81
94
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 );
85
97
result = (HashMap <String , NotificationData >) ois .readObject ();
86
-
87
- ois .close ();
88
- fis .close ();
89
98
} catch (IOException | ClassNotFoundException e ) {
90
99
e .printStackTrace ();
91
100
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
+ }
92
112
}
93
113
94
114
return result ;
0 commit comments