Skip to content

Commit a2279e9

Browse files
authored
Fix #973 Make sure all resources are closed in RegistrationIntentService (#998)
* Fix #973 Make sure all resources are closed Make sure all FileStream and FileLock are closed using AutoClose pattern.
1 parent 5f23f71 commit a2279e9

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

messaging/src/android/java/com/google/firebase/messaging/cpp/RegistrationIntentService.java

+8-20
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,19 @@ public static void writeTokenToInternalStorage(Context context, String token) {
6666
// Write out the buffer length into the first four bytes.
6767
sizeBuffer.order(ByteOrder.LITTLE_ENDIAN);
6868
sizeBuffer.putInt(buffer.length);
69-
FileLock lock = null;
70-
try {
71-
// Acquire lock. This prevents the C++ code from consuming and clearing the file while we
72-
// append to it.
73-
FileOutputStream lockFileStream = context.openFileOutput(MessageWriter.LOCK_FILE, 0);
74-
lock = lockFileStream.getChannel().lock();
7569

76-
FileOutputStream outputStream =
77-
context.openFileOutput(MessageWriter.STORAGE_FILE, Context.MODE_APPEND);
78-
// We send both the buffer length and the buffer itself so that we can potentially process
79-
// more than one event in the case where they get queued up.
70+
try (FileOutputStream lockFileStream = context.openFileOutput(MessageWriter.LOCK_FILE, 0);
71+
// Acquire lock. This prevents the C++ code from consuming and clearing the file while we
72+
// append to it.
73+
FileLock lock = lockFileStream.getChannel().lock();
74+
FileOutputStream outputStream =
75+
context.openFileOutput(MessageWriter.STORAGE_FILE, Context.MODE_APPEND)) {
76+
// We send both the buffer length and the buffer itself so that we can potentially
77+
// process more than one event in the case where they get queued up.
8078
outputStream.write(sizeBuffer.array());
8179
outputStream.write(buffer);
82-
outputStream.close();
8380
} catch (Exception e) {
8481
e.printStackTrace();
85-
} finally {
86-
// Release the lock.
87-
try {
88-
if (lock != null) {
89-
lock.release();
90-
}
91-
} catch (Exception e) {
92-
e.printStackTrace();
93-
}
9482
}
9583
}
9684

release_build_files/readme.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ All Firebase SDKs | platform(com.google.firebase:firebase-bom:30.1.0)
8282
| | (Android Bill of Materials)
8383
Firebase AdMob | libfirebase_admob.a
8484
| | libfirebase_app.a
85-
| | com.google.firebase:firebase-analytics
85+
| | com.google.firebase:firebase-analytics
8686
| | (Maven package)
87-
| | com.google.firebase:firebase-ads:19.8.0
87+
| | com.google.firebase:firebase-ads:19.8.0
8888
| | (Maven package)
8989
Firebase Analytics | libfirebase_analytics.a
9090
| | libfirebase_app.a
91-
| | com.google.firebase:firebase-analytics
91+
| | com.google.firebase:firebase-analytics
9292
| | (Maven package)
9393
Firebase Authentication | libfirebase_auth.a
9494
| | libfirebase_app.a
95-
| | com.google.firebase:firebase-analytics
95+
| | com.google.firebase:firebase-analytics
9696
| | (Maven package)
97-
| | com.google.firebase:firebase-auth
97+
| | com.google.firebase:firebase-auth
9898
| | (Maven package)
9999
Firebase Dynamic Links | libfirebase_dynamic_links.a
100100
| | libfirebase_app.a
101-
| | com.google.firebase:firebase-analytics
101+
| | com.google.firebase:firebase-analytics
102102
| | (Maven package)
103103
| | com.google.firebase:firebase-dynamic-links
104104
| | (Maven package)
@@ -156,7 +156,7 @@ Firebase Storage | libfirebase_storage.a
156156
| | (Maven package)
157157
| | com.google.firebase:firebase-auth
158158
| | (Maven package)
159-
Google Play services module| com.google.android.gms:play-services-base:18.0.1
159+
Google Play services module| com.google.android.gms:play-services-base:18.0.1
160160
| | (Maven package)
161161

162162
The Firebase C++ SDK uses an Android BoM (Bill of Materials) to specify a single
@@ -615,6 +615,8 @@ code.
615615
- Changes
616616
- General (Android): Switched over to Android BoM (Bill of Materials)
617617
for dependency versions. This requires Gradle 5.
618+
- Messaging (Android): Fixed #973. Make sure all the resources are closed in
619+
`RegistrationIntentService`.
618620

619621
### 9.1.0
620622
- Changes
@@ -710,16 +712,16 @@ code.
710712
and include support for ARM-based Mac systems.
711713
- General (iOS): iOS SDKs are now built using Xcode 12.2.
712714
- Messaging (Android): Fixes an issue to receive token when
713-
initialize the app.
715+
initialize the app.
714716
([#667](https://github.com/firebase/firebase-cpp-sdk/pull/667)).
715717
- Auth (Desktop): Fix a crash that would occur if parsing the JSON
716718
response from the server failed
717719
([#692](https://github.com/firebase/firebase-cpp-sdk/pull/692)).
718-
720+
719721
### 8.5.0
720722
- Changes
721723
- General: Updating Android and iOS dependencies to the latest.
722-
- General: Fixes an issue with generating Proguard files.
724+
- General: Fixes an issue with generating Proguard files.
723725
([#664](https://github.com/firebase/firebase-cpp-sdk/pull/664)).
724726

725727
### 8.4.0

0 commit comments

Comments
 (0)