Skip to content

Commit b7c3081

Browse files
authored
Lazy initialize dataFile in PersistedInstallation (#3314)
1 parent f620db9 commit b7c3081

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallation.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @hide
3232
*/
3333
public class PersistedInstallation {
34-
private final File dataFile;
34+
private File dataFile;
3535
@NonNull private final FirebaseApp firebaseApp;
3636

3737
// Registration Status of each persisted fid entry
@@ -77,15 +77,27 @@ public enum RegistrationStatus {
7777
private static final String FIS_ERROR_KEY = "FisError";
7878

7979
public PersistedInstallation(@NonNull FirebaseApp firebaseApp) {
80-
// Different FirebaseApp in the same Android application should have the same application
81-
// context and same dir path
82-
dataFile =
83-
new File(
84-
firebaseApp.getApplicationContext().getFilesDir(),
85-
SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp.getPersistenceKey() + ".json");
8680
this.firebaseApp = firebaseApp;
8781
}
8882

83+
private File getDataFile() {
84+
85+
if (dataFile == null) {
86+
synchronized (this) {
87+
if (dataFile == null) {
88+
// Different FirebaseApp in the same Android application should have the same application
89+
// context and same dir path
90+
dataFile =
91+
new File(
92+
firebaseApp.getApplicationContext().getFilesDir(),
93+
SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp.getPersistenceKey() + ".json");
94+
}
95+
}
96+
}
97+
98+
return dataFile;
99+
}
100+
89101
@NonNull
90102
public PersistedInstallationEntry readPersistedInstallationEntryValue() {
91103
JSONObject json = readJSONFromFile();
@@ -114,7 +126,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
114126
private JSONObject readJSONFromFile() {
115127
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
116128
final byte[] tmpBuf = new byte[16 * 1024];
117-
try (FileInputStream fis = new FileInputStream(dataFile)) {
129+
try (FileInputStream fis = new FileInputStream(getDataFile())) {
118130
while (true) {
119131
int numRead = fis.read(tmpBuf, 0, tmpBuf.length);
120132
if (numRead < 0) {
@@ -156,7 +168,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
156168
fos.close();
157169

158170
// Snapshot the temp file to the actual file
159-
if (!tmpFile.renameTo(dataFile)) {
171+
if (!tmpFile.renameTo(getDataFile())) {
160172
throw new IOException("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX);
161173
}
162174
} catch (JSONException | IOException e) {
@@ -173,6 +185,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
173185

174186
/** Sets the state to ATTEMPT_MIGRATION. */
175187
public void clearForTesting() {
176-
dataFile.delete();
188+
getDataFile().delete();
177189
}
178190
}

0 commit comments

Comments
 (0)