Skip to content

Commit 2777b4c

Browse files
committed
Fix #973 Make sure all resources are closed
Make sure all FileStream and FileLock are closed using AutoClose pattern.
1 parent 486bb50 commit 2777b4c

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,38 @@ 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+
// try (FileLock lock = lockFileStream.getChannel().lock()) {
74+
// try (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.
78+
// outputStream.write(sizeBuffer.array());
79+
// outputStream.write(buffer);
80+
// } catch (Exception e) {
81+
// e.printStackTrace();
82+
// }
83+
// } catch (Exception e) {
84+
// e.printStackTrace();
85+
// }
86+
// } catch (Exception e) {
87+
// e.printStackTrace();
88+
// }
89+
try (FileOutputStream lockFileStream = context.openFileOutput(MessageWriter.LOCK_FILE, 0);
90+
// Acquire lock. This prevents the C++ code from consuming and clearing the file while we
91+
// append to it.
92+
FileLock lock = lockFileStream.getChannel().lock();
93+
FileOutputStream outputStream =
94+
context.openFileOutput(MessageWriter.STORAGE_FILE, Context.MODE_APPEND)) {
95+
// We send both the buffer length and the buffer itself so that we can potentially
96+
// process more than one event in the case where they get queued up.
8097
outputStream.write(sizeBuffer.array());
8198
outputStream.write(buffer);
82-
outputStream.close();
8399
} catch (Exception e) {
84-
e.printStackTrace();
85-
} finally {
86-
// Release the lock.
87-
try {
88-
if (lock != null) {
89-
lock.release();
90-
}
91-
} catch (Exception e) {
92100
e.printStackTrace();
93-
}
94101
}
95102
}
96103

0 commit comments

Comments
 (0)