package io.netty.util.internal;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import sun.misc.Unsafe;

/* JADX INFO: loaded from: classes.dex */
public final class CleanerJava9 implements Cleaner {
    private static final Method INVOKE_CLEANER;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) CleanerJava9.class);

    static {
        Throwable unsupportedOperationException;
        Object obj;
        Method method = null;
        if (PlatformDependent0.hasUnsafe()) {
            ByteBuffer byteBufferAllocateDirect = ByteBuffer.allocateDirect(1);
            try {
                Unsafe unsafe = PlatformDependent0.UNSAFE;
                Method declaredMethod = unsafe.getClass().getDeclaredMethod("invokeCleaner", ByteBuffer.class);
                declaredMethod.invoke(unsafe, byteBufferAllocateDirect);
                obj = declaredMethod;
            } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e2) {
                obj = e2;
            }
            if (obj instanceof Throwable) {
                unsupportedOperationException = (Throwable) obj;
            } else {
                Method method2 = (Method) obj;
                unsupportedOperationException = null;
                method = method2;
            }
        } else {
            unsupportedOperationException = new UnsupportedOperationException("sun.misc.Unsafe unavailable");
        }
        InternalLogger internalLogger = logger;
        if (unsupportedOperationException == null) {
            internalLogger.debug("java.nio.ByteBuffer.cleaner(): available");
        } else {
            internalLogger.debug("java.nio.ByteBuffer.cleaner(): unavailable", unsupportedOperationException);
        }
        INVOKE_CLEANER = method;
    }

    public static boolean isSupported() {
        return INVOKE_CLEANER != null;
    }

    @Override // io.netty.util.internal.Cleaner
    public void freeDirectBuffer(ByteBuffer byteBuffer) {
        try {
            INVOKE_CLEANER.invoke(PlatformDependent0.UNSAFE, byteBuffer);
        } catch (Throwable th) {
            PlatformDependent0.throwException(th);
        }
    }
}
