导航菜单

页面标题

页面副标题

Vi App v10.18.0 - LocalIdManager.java 源代码

正在查看: Vi App v10.18.0 应用的 LocalIdManager.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.parse;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import org.json.JSONException;
import org.json.JSONObject;

class LocalIdManager {
    private final File diskPath;
    private final Random random = new Random();

    public static class MapEntry {
        public String objectId;
        public int retainCount;

        private MapEntry() {
        }
    }

    public LocalIdManager(File file) {
        this.diskPath = new File(file, "LocalId");
    }

    private synchronized MapEntry getMapEntry(String str) {
        MapEntry mapEntry;
        if (!isLocalId(str)) {
            throw new IllegalStateException("Tried to get invalid local id: \"" + str + "\".");
        }
        try {
            JSONObject readFileToJSONObject = ParseFileUtils.readFileToJSONObject(new File(this.diskPath, str));
            mapEntry = new MapEntry();
            mapEntry.retainCount = readFileToJSONObject.optInt("retainCount", 0);
            mapEntry.objectId = readFileToJSONObject.optString("objectId", null);
        } catch (IOException | JSONException unused) {
            return new MapEntry();
        }
        return mapEntry;
    }

    private boolean isLocalId(String str) {
        if (!str.startsWith("local_")) {
            return false;
        }
        for (int i = 6; i < str.length(); i++) {
            char charAt = str.charAt(i);
            if ((charAt < '0' || charAt > '9') && (charAt < 'a' || charAt > 'f')) {
                return false;
            }
        }
        return true;
    }

    private synchronized void putMapEntry(String str, MapEntry mapEntry) {
        if (!isLocalId(str)) {
            throw new IllegalStateException("Tried to get invalid local id: \"" + str + "\".");
        }
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("retainCount", mapEntry.retainCount);
            String str2 = mapEntry.objectId;
            if (str2 != null) {
                jSONObject.put("objectId", str2);
            }
            File file = new File(this.diskPath, str);
            if (!this.diskPath.exists()) {
                this.diskPath.mkdirs();
            }
            try {
                ParseFileUtils.writeJSONObjectToFile(file, jSONObject);
            } catch (IOException unused) {
            }
        } catch (JSONException e) {
            throw new IllegalStateException("Error creating local id map entry.", e);
        }
    }

    private synchronized void removeMapEntry(String str) {
        if (!isLocalId(str)) {
            throw new IllegalStateException("Tried to get invalid local id: \"" + str + "\".");
        }
        ParseFileUtils.deleteQuietly(new File(this.diskPath, str));
    }

    public synchronized String createLocalId() {
        String str;
        str = "local_" + Long.toHexString(this.random.nextLong());
        if (!isLocalId(str)) {
            throw new IllegalStateException("Generated an invalid local id: \"" + str + "\". This should never happen. Open a bug at https://github.com/parse-community/parse-server");
        }
        return str;
    }

    public synchronized String getObjectId(String str) {
        return getMapEntry(str).objectId;
    }

    public synchronized void releaseLocalIdOnDisk(String str) {
        MapEntry mapEntry = getMapEntry(str);
        int i = mapEntry.retainCount - 1;
        mapEntry.retainCount = i;
        if (i > 0) {
            putMapEntry(str, mapEntry);
        } else {
            removeMapEntry(str);
        }
    }

    public synchronized void retainLocalIdOnDisk(String str) {
        MapEntry mapEntry = getMapEntry(str);
        mapEntry.retainCount++;
        putMapEntry(str, mapEntry);
    }

    public synchronized void setObjectId(String str, String str2) {
        MapEntry mapEntry = getMapEntry(str);
        if (mapEntry.retainCount > 0) {
            if (mapEntry.objectId != null) {
                throw new IllegalStateException("Tried to set an objectId for a localId that already has one.");
            }
            mapEntry.objectId = str2;
            putMapEntry(str, mapEntry);
        }
    }
}