package com.google.firebase.crashlytics.internal.common; import com.google.firebase.crashlytics.internal.Logger; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; /* loaded from: classes2.dex */ class MetaDataStore { private static final String KEYDATA_SUFFIX = "keys"; private static final String KEY_USER_ID = "userId"; private static final String METADATA_EXT = ".meta"; private static final String USERDATA_SUFFIX = "user"; private static final Charset UTF_8 = Charset.forName("UTF-8"); private final File filesDir; public MetaDataStore(File file) { this.filesDir = file; } public void writeUserData(String str, UserMetadata userMetadata) { BufferedWriter bufferedWriter; String userDataToJson; File userDataFileForSession = getUserDataFileForSession(str); BufferedWriter bufferedWriter2 = null; try { try { userDataToJson = userDataToJson(userMetadata); bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(userDataFileForSession), UTF_8)); } catch (Exception e) { e = e; } } catch (Throwable th) { th = th; bufferedWriter = bufferedWriter2; } try { bufferedWriter.write(userDataToJson); bufferedWriter.flush(); CommonUtils.closeOrLog(bufferedWriter, "Failed to close user metadata file."); } catch (Exception e2) { e = e2; bufferedWriter2 = bufferedWriter; Logger.getLogger().e("Error serializing user metadata.", e); CommonUtils.closeOrLog(bufferedWriter2, "Failed to close user metadata file."); } catch (Throwable th2) { th = th2; CommonUtils.closeOrLog(bufferedWriter, "Failed to close user metadata file."); throw th; } } public UserMetadata readUserData(String str) { FileInputStream fileInputStream; File userDataFileForSession = getUserDataFileForSession(str); if (!userDataFileForSession.exists()) { return new UserMetadata(); } FileInputStream fileInputStream2 = null; try { try { fileInputStream = new FileInputStream(userDataFileForSession); } catch (Exception e) { e = e; } } catch (Throwable th) { th = th; fileInputStream = fileInputStream2; } try { UserMetadata jsonToUserData = jsonToUserData(CommonUtils.streamToString(fileInputStream)); CommonUtils.closeOrLog(fileInputStream, "Failed to close user metadata file."); return jsonToUserData; } catch (Exception e2) { e = e2; fileInputStream2 = fileInputStream; Logger.getLogger().e("Error deserializing user metadata.", e); CommonUtils.closeOrLog(fileInputStream2, "Failed to close user metadata file."); return new UserMetadata(); } catch (Throwable th2) { th = th2; CommonUtils.closeOrLog(fileInputStream, "Failed to close user metadata file."); throw th; } } public void writeKeyData(String str, Map map) { BufferedWriter bufferedWriter; String keysDataToJson; File keysFileForSession = getKeysFileForSession(str); BufferedWriter bufferedWriter2 = null; try { try { keysDataToJson = keysDataToJson(map); bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(keysFileForSession), UTF_8)); } catch (Exception e) { e = e; } } catch (Throwable th) { th = th; bufferedWriter = bufferedWriter2; } try { bufferedWriter.write(keysDataToJson); bufferedWriter.flush(); CommonUtils.closeOrLog(bufferedWriter, "Failed to close key/value metadata file."); } catch (Exception e2) { e = e2; bufferedWriter2 = bufferedWriter; Logger.getLogger().e("Error serializing key/value metadata.", e); CommonUtils.closeOrLog(bufferedWriter2, "Failed to close key/value metadata file."); } catch (Throwable th2) { th = th2; CommonUtils.closeOrLog(bufferedWriter, "Failed to close key/value metadata file."); throw th; } } public Map readKeyData(String str) { FileInputStream fileInputStream; File keysFileForSession = getKeysFileForSession(str); if (!keysFileForSession.exists()) { return Collections.emptyMap(); } FileInputStream fileInputStream2 = null; try { try { fileInputStream = new FileInputStream(keysFileForSession); } catch (Exception e) { e = e; } } catch (Throwable th) { th = th; fileInputStream = fileInputStream2; } try { Map jsonToKeysData = jsonToKeysData(CommonUtils.streamToString(fileInputStream)); CommonUtils.closeOrLog(fileInputStream, "Failed to close user metadata file."); return jsonToKeysData; } catch (Exception e2) { e = e2; fileInputStream2 = fileInputStream; Logger.getLogger().e("Error deserializing user metadata.", e); CommonUtils.closeOrLog(fileInputStream2, "Failed to close user metadata file."); return Collections.emptyMap(); } catch (Throwable th2) { th = th2; CommonUtils.closeOrLog(fileInputStream, "Failed to close user metadata file."); throw th; } } public File getUserDataFileForSession(String str) { File file = this.filesDir; StringBuilder sb = new StringBuilder(); sb.append(str); sb.append("user.meta"); return new File(file, sb.toString()); } public File getKeysFileForSession(String str) { File file = this.filesDir; StringBuilder sb = new StringBuilder(); sb.append(str); sb.append("keys.meta"); return new File(file, sb.toString()); } private static UserMetadata jsonToUserData(String str) throws JSONException { JSONObject jSONObject = new JSONObject(str); UserMetadata userMetadata = new UserMetadata(); userMetadata.setUserId(valueOrNull(jSONObject, KEY_USER_ID)); return userMetadata; } /* JADX WARN: Type inference failed for: r0v0, types: [com.google.firebase.crashlytics.internal.common.MetaDataStore$1] */ private static String userDataToJson(UserMetadata userMetadata) throws JSONException { return new JSONObject(userMetadata) { // from class: com.google.firebase.crashlytics.internal.common.MetaDataStore.1 final UserMetadata val$userData; { this.val$userData = userMetadata; put(MetaDataStore.KEY_USER_ID, userMetadata.getUserId()); } }.toString(); } private static Map jsonToKeysData(String str) throws JSONException { JSONObject jSONObject = new JSONObject(str); HashMap hashMap = new HashMap(); Iterator keys = jSONObject.keys(); while (keys.hasNext()) { String next = keys.next(); hashMap.put(next, valueOrNull(jSONObject, next)); } return hashMap; } private static String keysDataToJson(Map map) throws JSONException { return new JSONObject(map).toString(); } private static String valueOrNull(JSONObject jSONObject, String str) { if (jSONObject.isNull(str)) { return null; } return jSONObject.optString(str, null); } }