package androidx.datastore.preferences.protobuf;

import java.lang.reflect.Field;
import java.nio.Buffer;
import java.nio.ByteOrder;
import java.security.AccessController;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.misc.Unsafe;

/* JADX INFO: loaded from: classes.dex */
public abstract class q0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Unsafe f11434a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Class f11435b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final p0 f11436c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final boolean f11437d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final boolean f11438e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final long f11439f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final boolean f11440g;

    static {
        Unsafe unsafeJ = j();
        f11434a = unsafeJ;
        f11435b = AbstractC1014c.f11362a;
        boolean zF = f(Long.TYPE);
        boolean zF2 = f(Integer.TYPE);
        p0 o0Var = null;
        if (unsafeJ != null) {
            if (!AbstractC1014c.a()) {
                o0Var = new o0(unsafeJ);
            } else if (zF) {
                o0Var = new n0(unsafeJ, 1);
            } else if (zF2) {
                o0Var = new n0(unsafeJ, 0);
            }
        }
        f11436c = o0Var;
        f11437d = o0Var == null ? false : o0Var.s();
        f11438e = o0Var == null ? false : o0Var.r();
        f11439f = c(byte[].class);
        c(boolean[].class);
        d(boolean[].class);
        c(int[].class);
        d(int[].class);
        c(long[].class);
        d(long[].class);
        c(float[].class);
        d(float[].class);
        c(double[].class);
        d(double[].class);
        c(Object[].class);
        d(Object[].class);
        Field fieldE = e();
        if (fieldE != null && o0Var != null) {
            o0Var.j(fieldE);
        }
        f11440g = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
    }

    public static void a(Throwable th) {
        Logger.getLogger(q0.class.getName()).log(Level.WARNING, "platform method missing - proto runtime falling back to safer methods: " + th);
    }

    public static Object b(Class cls) {
        try {
            return f11434a.allocateInstance(cls);
        } catch (InstantiationException e6) {
            throw new IllegalStateException(e6);
        }
    }

    public static int c(Class cls) {
        if (f11438e) {
            return f11436c.a(cls);
        }
        return -1;
    }

    public static void d(Class cls) {
        if (f11438e) {
            f11436c.b(cls);
        }
    }

    public static Field e() {
        Field declaredField;
        Field declaredField2;
        if (AbstractC1014c.a()) {
            try {
                declaredField2 = Buffer.class.getDeclaredField("effectiveDirectAddress");
            } catch (Throwable unused) {
                declaredField2 = null;
            }
            if (declaredField2 != null) {
                return declaredField2;
            }
        }
        try {
            declaredField = Buffer.class.getDeclaredField("address");
        } catch (Throwable unused2) {
            declaredField = null;
        }
        if (declaredField == null || declaredField.getType() != Long.TYPE) {
            return null;
        }
        return declaredField;
    }

    public static boolean f(Class cls) {
        if (!AbstractC1014c.a()) {
            return false;
        }
        try {
            Class cls2 = f11435b;
            Class cls3 = Boolean.TYPE;
            cls2.getMethod("peekLong", cls, cls3);
            cls2.getMethod("pokeLong", cls, Long.TYPE, cls3);
            Class cls4 = Integer.TYPE;
            cls2.getMethod("pokeInt", cls, cls4, cls3);
            cls2.getMethod("peekInt", cls, cls3);
            cls2.getMethod("pokeByte", cls, Byte.TYPE);
            cls2.getMethod("peekByte", cls);
            cls2.getMethod("pokeByteArray", cls, byte[].class, cls4, cls4);
            cls2.getMethod("peekByteArray", cls, byte[].class, cls4, cls4);
            return true;
        } catch (Throwable unused) {
            return false;
        }
    }

    public static byte g(long j4, byte[] bArr) {
        return f11436c.d(f11439f + j4, bArr);
    }

    public static byte h(long j4, Object obj) {
        return (byte) ((f11436c.g((-4) & j4, obj) >>> ((int) (((~j4) & 3) << 3))) & 255);
    }

    public static byte i(long j4, Object obj) {
        return (byte) ((f11436c.g((-4) & j4, obj) >>> ((int) ((j4 & 3) << 3))) & 255);
    }

    public static Unsafe j() {
        try {
            return (Unsafe) AccessController.doPrivileged(new m0());
        } catch (Throwable unused) {
            return null;
        }
    }

    public static void k(byte[] bArr, long j4, byte b5) {
        f11436c.l(bArr, f11439f + j4, b5);
    }

    public static void l(Object obj, long j4, byte b5) {
        long j6 = (-4) & j4;
        int iG = f11436c.g(j6, obj);
        int i6 = ((~((int) j4)) & 3) << 3;
        n(((255 & b5) << i6) | (iG & (~(255 << i6))), j6, obj);
    }

    public static void m(Object obj, long j4, byte b5) {
        long j6 = (-4) & j4;
        int i6 = (((int) j4) & 3) << 3;
        n(((255 & b5) << i6) | (f11436c.g(j6, obj) & (~(255 << i6))), j6, obj);
    }

    public static void n(int i6, long j4, Object obj) {
        f11436c.o(i6, j4, obj);
    }

    public static void o(Object obj, long j4, long j6) {
        f11436c.p(obj, j4, j6);
    }

    public static void p(Object obj, long j4, Object obj2) {
        f11436c.q(obj, j4, obj2);
    }
}
