package io.netty.channel;

import io.netty.buffer.ByteBufUtil;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.MacAddressUtil;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import io.netty.util.internal.ThreadLocalRandom;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public final class DefaultChannelId implements ChannelId {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final byte[] MACHINE_ID;
    private static final int MACHINE_ID_LEN = 8;
    private static final int MAX_PROCESS_ID = 4194304;
    private static final int PROCESS_ID;
    private static final int PROCESS_ID_LEN = 4;
    private static final int RANDOM_LEN = 4;
    private static final int SEQUENCE_LEN = 4;
    private static final int TIMESTAMP_LEN = 8;
    private static final long serialVersionUID = 3884076183504074063L;
    private final byte[] data = new byte[28];
    private int hashCode;
    private transient String longValue;
    private transient String shortValue;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) DefaultChannelId.class);
    private static final Pattern MACHINE_ID_PATTERN = Pattern.compile("^(?:[0-9a-fA-F][:-]?){6,8}$");
    private static final AtomicInteger nextSequence = new AtomicInteger();

    static {
        int i2;
        String str = SystemPropertyUtil.get("io.netty.processId");
        int iDefaultProcessId = -1;
        if (str != null) {
            try {
                i2 = Integer.parseInt(str);
            } catch (NumberFormatException unused) {
                i2 = -1;
            }
            if (i2 < 0 || i2 > MAX_PROCESS_ID) {
                logger.warn("-Dio.netty.processId: {} (malformed)", str);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("-Dio.netty.processId: {} (user-set)", Integer.valueOf(i2));
                }
                iDefaultProcessId = i2;
            }
        }
        if (iDefaultProcessId < 0) {
            iDefaultProcessId = defaultProcessId();
            if (logger.isDebugEnabled()) {
                logger.debug("-Dio.netty.processId: {} (auto-detected)", Integer.valueOf(iDefaultProcessId));
            }
        }
        PROCESS_ID = iDefaultProcessId;
        byte[] bArrDefaultMachineId = null;
        String str2 = SystemPropertyUtil.get("io.netty.machineId");
        if (str2 != null) {
            if (MACHINE_ID_PATTERN.matcher(str2).matches()) {
                bArrDefaultMachineId = parseMachineId(str2);
                logger.debug("-Dio.netty.machineId: {} (user-set)", str2);
            } else {
                logger.warn("-Dio.netty.machineId: {} (malformed)", str2);
            }
        }
        if (bArrDefaultMachineId == null) {
            bArrDefaultMachineId = defaultMachineId();
            if (logger.isDebugEnabled()) {
                logger.debug("-Dio.netty.machineId: {} (auto-detected)", MacAddressUtil.formatAddress(bArrDefaultMachineId));
            }
        }
        MACHINE_ID = bArrDefaultMachineId;
    }

    private DefaultChannelId() {
    }

    private int appendHexDumpField(StringBuilder sb, int i2, int i3) {
        sb.append(ByteBufUtil.hexDump(this.data, i2, i3));
        sb.append('-');
        return i2 + i3;
    }

    private static byte[] defaultMachineId() {
        byte[] bArrBestAvailableMac = MacAddressUtil.bestAvailableMac();
        if (bArrBestAvailableMac != null) {
            return bArrBestAvailableMac;
        }
        byte[] bArr = new byte[8];
        ThreadLocalRandom.current().nextBytes(bArr);
        logger.warn("Failed to find a usable hardware address from the network interfaces; using random bytes: {}", MacAddressUtil.formatAddress(bArr));
        return bArr;
    }

    private static int defaultProcessId() {
        String string;
        int i2;
        ClassLoader systemClassLoader = PlatformDependent.getSystemClassLoader();
        try {
            Class<?> cls = Class.forName("java.lang.management.ManagementFactory", true, systemClassLoader);
            Class<?> cls2 = Class.forName("java.lang.management.RuntimeMXBean", true, systemClassLoader);
            string = (String) cls2.getDeclaredMethod("getName", EmptyArrays.EMPTY_CLASSES).invoke(cls.getMethod("getRuntimeMXBean", EmptyArrays.EMPTY_CLASSES).invoke(null, EmptyArrays.EMPTY_OBJECTS), EmptyArrays.EMPTY_OBJECTS);
        } catch (Exception e2) {
            logger.debug("Could not invoke ManagementFactory.getRuntimeMXBean().getName(); Android?", (Throwable) e2);
            try {
                string = Class.forName("android.os.Process", true, systemClassLoader).getMethod("myPid", EmptyArrays.EMPTY_CLASSES).invoke(null, EmptyArrays.EMPTY_OBJECTS).toString();
            } catch (Exception e3) {
                logger.debug("Could not invoke Process.myPid(); not Android?", (Throwable) e3);
                string = "";
            }
        }
        int iIndexOf = string.indexOf(64);
        if (iIndexOf >= 0) {
            string = string.substring(0, iIndexOf);
        }
        try {
            i2 = Integer.parseInt(string);
        } catch (NumberFormatException unused) {
            i2 = -1;
        }
        if (i2 >= 0 && i2 <= MAX_PROCESS_ID) {
            return i2;
        }
        int iNextInt = ThreadLocalRandom.current().nextInt(4194305);
        logger.warn("Failed to find the current process ID from '{}'; using a random value: {}", string, Integer.valueOf(iNextInt));
        return iNextInt;
    }

    private void init() {
        System.arraycopy(MACHINE_ID, 0, this.data, 0, 8);
        int iWriteLong = writeLong(writeInt(writeInt(8, PROCESS_ID), nextSequence.getAndIncrement()), Long.reverse(System.nanoTime()) ^ System.currentTimeMillis());
        int iNextInt = ThreadLocalRandom.current().nextInt();
        this.hashCode = iNextInt;
        writeInt(iWriteLong, iNextInt);
    }

    public static DefaultChannelId newInstance() {
        DefaultChannelId defaultChannelId = new DefaultChannelId();
        defaultChannelId.init();
        return defaultChannelId;
    }

    private String newLongValue() {
        StringBuilder sb = new StringBuilder((this.data.length * 2) + 5);
        appendHexDumpField(sb, appendHexDumpField(sb, appendHexDumpField(sb, appendHexDumpField(sb, appendHexDumpField(sb, 0, 8), 4), 4), 8), 4);
        return sb.substring(0, sb.length() - 1);
    }

    private static byte[] parseMachineId(String str) {
        String strReplaceAll = str.replaceAll("[:-]", "");
        byte[] bArr = new byte[8];
        int i2 = 0;
        while (i2 < strReplaceAll.length()) {
            int i3 = i2 + 2;
            bArr[i2] = (byte) Integer.parseInt(strReplaceAll.substring(i2, i3), 16);
            i2 = i3;
        }
        return bArr;
    }

    private int writeInt(int i2, int i3) {
        byte[] bArr = this.data;
        int i4 = i2 + 1;
        bArr[i2] = (byte) (i3 >>> 24);
        int i5 = i4 + 1;
        bArr[i4] = (byte) (i3 >>> 16);
        int i6 = i5 + 1;
        bArr[i5] = (byte) (i3 >>> 8);
        int i7 = i6 + 1;
        bArr[i6] = (byte) i3;
        return i7;
    }

    private int writeLong(int i2, long j2) {
        byte[] bArr = this.data;
        int i3 = i2 + 1;
        bArr[i2] = (byte) (j2 >>> 56);
        int i4 = i3 + 1;
        bArr[i3] = (byte) (j2 >>> 48);
        int i5 = i4 + 1;
        bArr[i4] = (byte) (j2 >>> 40);
        int i6 = i5 + 1;
        bArr[i5] = (byte) (j2 >>> 32);
        int i7 = i6 + 1;
        bArr[i6] = (byte) (j2 >>> 24);
        int i8 = i7 + 1;
        bArr[i7] = (byte) (j2 >>> 16);
        int i9 = i8 + 1;
        bArr[i8] = (byte) (j2 >>> 8);
        int i10 = i9 + 1;
        bArr[i9] = (byte) j2;
        return i10;
    }

    @Override // io.netty.channel.ChannelId
    public String asLongText() {
        String str = this.longValue;
        if (str != null) {
            return str;
        }
        String strNewLongValue = newLongValue();
        this.longValue = strNewLongValue;
        return strNewLongValue;
    }

    @Override // io.netty.channel.ChannelId
    public String asShortText() {
        String str = this.shortValue;
        if (str != null) {
            return str;
        }
        String strHexDump = ByteBufUtil.hexDump(this.data, 24, 4);
        this.shortValue = strHexDump;
        return strHexDump;
    }

    @Override // java.lang.Comparable
    public int compareTo(ChannelId channelId) {
        return 0;
    }

    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof DefaultChannelId) {
            return Arrays.equals(this.data, ((DefaultChannelId) obj).data);
        }
        return false;
    }

    public int hashCode() {
        return this.hashCode;
    }

    public String toString() {
        return asShortText();
    }
}
