package com.google.android.gms.internal.atv_ads_framework;

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 libcore.io.Memory;
import sun.misc.Unsafe;

/* JADX INFO: loaded from: classes2.dex */
public abstract class n1 {

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

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

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

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

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

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

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

    /* JADX WARN: Removed duplicated region for block: B:11:0x004b  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x007d  */
    /* JADX WARN: Removed duplicated region for block: B:4:0x001c  */
    static {
        m1 l1Var;
        Field declaredField;
        boolean z10;
        boolean z11;
        Field declaredField2;
        m1 m1Var;
        Unsafe unsafeJ = j();
        f19957a = unsafeJ;
        int i10 = r.f19972a;
        f19958b = Memory.class;
        Class<?> cls = Long.TYPE;
        boolean zP = p(cls);
        boolean zP2 = p(Integer.TYPE);
        if (unsafeJ != null) {
            l1Var = zP ? new l1(unsafeJ) : zP2 ? new k1(unsafeJ) : null;
        }
        f19959c = l1Var;
        if (l1Var == null) {
            z10 = false;
        } else {
            try {
                Class<?> cls2 = l1Var.f19954a.getClass();
                cls2.getMethod("objectFieldOffset", Field.class);
                cls2.getMethod("getLong", Object.class, cls);
                try {
                    declaredField = Buffer.class.getDeclaredField("effectiveDirectAddress");
                } catch (Throwable unused) {
                    declaredField = null;
                }
                if (declaredField == null) {
                    try {
                        declaredField = Buffer.class.getDeclaredField("address");
                    } catch (Throwable unused2) {
                        declaredField = null;
                    }
                    if (declaredField != null) {
                        if (declaredField.getType() != Long.TYPE) {
                            declaredField = null;
                        }
                    }
                }
                if (declaredField != null) {
                    z10 = true;
                }
            } catch (Throwable th) {
                Logger.getLogger(n1.class.getName()).logp(Level.WARNING, "com.google.protobuf.UnsafeUtil", "logMissingMethod", "platform method missing - proto runtime falling back to safer methods: ".concat(th.toString()));
            }
        }
        f19960d = z10;
        m1 m1Var2 = f19959c;
        if (m1Var2 == null) {
            z11 = false;
        } else {
            try {
                Class<?> cls3 = m1Var2.f19954a.getClass();
                cls3.getMethod("objectFieldOffset", Field.class);
                cls3.getMethod("arrayBaseOffset", Class.class);
                cls3.getMethod("arrayIndexScale", Class.class);
                Class<?> cls4 = Long.TYPE;
                cls3.getMethod("getInt", Object.class, cls4);
                cls3.getMethod("putInt", Object.class, cls4, Integer.TYPE);
                cls3.getMethod("getLong", Object.class, cls4);
                cls3.getMethod("putLong", Object.class, cls4, cls4);
                cls3.getMethod("getObject", Object.class, cls4);
                cls3.getMethod("putObject", Object.class, cls4, Object.class);
                z11 = true;
            } catch (Throwable th2) {
                Logger.getLogger(n1.class.getName()).logp(Level.WARNING, "com.google.protobuf.UnsafeUtil", "logMissingMethod", "platform method missing - proto runtime falling back to safer methods: ".concat(th2.toString()));
                z11 = false;
            }
        }
        f19961e = z11;
        f19962f = r(byte[].class);
        r(boolean[].class);
        a(boolean[].class);
        r(int[].class);
        a(int[].class);
        r(long[].class);
        a(long[].class);
        r(float[].class);
        a(float[].class);
        r(double[].class);
        a(double[].class);
        r(Object[].class);
        a(Object[].class);
        int i11 = r.f19972a;
        try {
            declaredField2 = Buffer.class.getDeclaredField("effectiveDirectAddress");
        } catch (Throwable unused3) {
            declaredField2 = null;
        }
        if (declaredField2 == null) {
            try {
                declaredField2 = Buffer.class.getDeclaredField("address");
            } catch (Throwable unused4) {
                declaredField2 = null;
            }
            if (declaredField2 == null || declaredField2.getType() != Long.TYPE) {
                declaredField2 = null;
            }
        }
        if (declaredField2 != null && (m1Var = f19959c) != null) {
            m1Var.f19954a.objectFieldOffset(declaredField2);
        }
        f19963g = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
    }

    public static void a(Class cls) {
        if (f19961e) {
            f19959c.f19954a.arrayIndexScale(cls);
        }
    }

    public static void b(Object obj, long j10, byte b10) {
        m1 m1Var = f19959c;
        long j11 = (-4) & j10;
        int i10 = m1Var.f19954a.getInt(obj, j11);
        int i11 = ((~((int) j10)) & 3) << 3;
        m1Var.f19954a.putInt(obj, j11, ((255 & b10) << i11) | (i10 & (~(255 << i11))));
    }

    public static void c(Object obj, long j10, byte b10) {
        m1 m1Var = f19959c;
        long j11 = (-4) & j10;
        int i10 = (((int) j10) & 3) << 3;
        m1Var.f19954a.putInt(obj, j11, ((255 & b10) << i10) | (m1Var.f19954a.getInt(obj, j11) & (~(255 << i10))));
    }

    public static double d(Object obj, long j10) {
        return f19959c.a(obj, j10);
    }

    public static float e(Object obj, long j10) {
        return f19959c.b(obj, j10);
    }

    public static int f(Object obj, long j10) {
        return f19959c.f19954a.getInt(obj, j10);
    }

    public static long g(Object obj, long j10) {
        return f19959c.f19954a.getLong(obj, j10);
    }

    public static Object h(Class cls) {
        try {
            return f19957a.allocateInstance(cls);
        } catch (InstantiationException e10) {
            throw new IllegalStateException(e10);
        }
    }

    public static Object i(Object obj, long j10) {
        return f19959c.f19954a.getObject(obj, j10);
    }

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

    public static void k(Object obj, long j10, int i10) {
        f19959c.f19954a.putInt(obj, j10, i10);
    }

    public static void l(Object obj, long j10, long j11) {
        f19959c.f19954a.putLong(obj, j10, j11);
    }

    public static void m(Object obj, long j10, Object obj2) {
        f19959c.f19954a.putObject(obj, j10, obj2);
    }

    public static /* bridge */ /* synthetic */ boolean n(Object obj, long j10) {
        return ((byte) ((f19959c.f19954a.getInt(obj, (-4) & j10) >>> ((int) (((~j10) & 3) << 3))) & 255)) != 0;
    }

    public static /* bridge */ /* synthetic */ boolean o(Object obj, long j10) {
        return ((byte) ((f19959c.f19954a.getInt(obj, (-4) & j10) >>> ((int) ((j10 & 3) << 3))) & 255)) != 0;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static boolean p(Class cls) {
        int i10 = r.f19972a;
        try {
            Class cls2 = f19958b;
            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 boolean q(Object obj, long j10) {
        return f19959c.g(obj, j10);
    }

    public static int r(Class cls) {
        if (f19961e) {
            return f19959c.f19954a.arrayBaseOffset(cls);
        }
        return -1;
    }
}
