Skip to content

Commit e58be5f

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

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

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

Lines changed: 21 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,26 @@ 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 = new File(
91+
firebaseApp.getApplicationContext().getFilesDir(),
92+
SETTINGS_FILE_NAME_PREFIX + "." + firebaseApp.getPersistenceKey() + ".json");
93+
}
94+
}
95+
}
96+
97+
return dataFile
98+
}
99+
89100
@NonNull
90101
public PersistedInstallationEntry readPersistedInstallationEntryValue() {
91102
JSONObject json = readJSONFromFile();
@@ -114,7 +125,7 @@ public PersistedInstallationEntry readPersistedInstallationEntryValue() {
114125
private JSONObject readJSONFromFile() {
115126
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
116127
final byte[] tmpBuf = new byte[16 * 1024];
117-
try (FileInputStream fis = new FileInputStream(dataFile)) {
128+
try (FileInputStream fis = new FileInputStream(getDataFile())) {
118129
while (true) {
119130
int numRead = fis.read(tmpBuf, 0, tmpBuf.length);
120131
if (numRead < 0) {
@@ -156,7 +167,7 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
156167
fos.close();
157168

158169
// Snapshot the temp file to the actual file
159-
if (!tmpFile.renameTo(dataFile)) {
170+
if (!tmpFile.renameTo(getDataFile())) {
160171
throw new IOException("unable to rename the tmpfile to " + SETTINGS_FILE_NAME_PREFIX);
161172
}
162173
} catch (JSONException | IOException e) {
@@ -173,6 +184,6 @@ public PersistedInstallationEntry insertOrUpdatePersistedInstallationEntry(
173184

174185
/** Sets the state to ATTEMPT_MIGRATION. */
175186
public void clearForTesting() {
176-
dataFile.delete();
187+
getDataFile().delete();
177188
}
178189
}

0 commit comments

Comments
 (0)