Skip to content

Commit 1852750

Browse files
committed
Lazy initialize dataFile in PersistedInstallation
1 parent 46b3dfd commit 1852750

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

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

Lines changed: 19 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,24 @@ 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+
if (dataFile == null) {
85+
synchronized (this) {
86+
if (dataFile == null) {
87+
// Different FirebaseApp in the same Android application should have the same application
88+
// context and same dir path
89+
dataFile = new File(
90+
firebaseApp.getApplicationContext().getFilesDir(),
91+
SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp.getPersistenceKey() + ".json");
92+
}
93+
}
94+
return dataFile
95+
}
96+
}
97+
8998
@NonNull
9099
public PersistedInstallationEntry readPersistedInstallationEntryValue() {
91100
JSONObject json = readJSONFromFile();
@@ -114,7 +123,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
114123
private JSONObject readJSONFromFile() {
115124
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
116125
final byte[] tmpBuf = new byte[16 * 1024];
117-
try (FileInputStream fis = new FileInputStream(dataFile)) {
126+
try (FileInputStream fis = new FileInputStream(getDataFile())) {
118127
while (true) {
119128
int numRead = fis.read(tmpBuf, 0, tmpBuf.length);
120129
if (numRead < 0) {
@@ -156,7 +165,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
156165
fos.close();
157166

158167
// Snapshot the temp file to the actual file
159-
if (!tmpFile.renameTo(dataFile)) {
168+
if (!tmpFile.renameTo(getDataFile())) {
160169
throw new IOException("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX);
161170
}
162171
} catch (JSONException | IOException e) {
@@ -173,6 +182,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
173182

174183
/** Sets the state to ATTEMPT_MIGRATION. */
175184
public void clearForTesting() {
176-
dataFile.delete();
185+
getDataFile().delete();
177186
}
178187
}

0 commit comments

Comments
 (0)