package org.snmp4j.smi;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import org.snmp4j.asn1.BER;
import org.snmp4j.asn1.BERInputStream;

/* JADX INFO: loaded from: classes.dex */
public class Counter64 extends AbstractVariable implements AssignableFromLong, AssignableFromString {
    private static final long serialVersionUID = 8741539680564150071L;
    private long value = 0;

    public Counter64() {
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public Object clone() {
        return new Counter64(this.value);
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.asn1.BERSerializable
    public void decodeBER(BERInputStream bERInputStream) throws IOException {
        BER.MutableByte mutableByte = new BER.MutableByte();
        long jDecodeUnsignedInt64 = BER.decodeUnsignedInt64(bERInputStream, mutableByte);
        if (mutableByte.getValue() == 70) {
            setValue(jDecodeUnsignedInt64);
            return;
        }
        throw new IOException("Wrong type encountered when decoding Counter64: " + ((int) mutableByte.getValue()));
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.asn1.BERSerializable
    public void encodeBER(OutputStream outputStream) throws IOException {
        BER.encodeUnsignedInt64(outputStream, (byte) 70, this.value);
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public boolean equals(Object obj) {
        return (obj instanceof Counter64) && ((Counter64) obj).value == this.value;
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public void fromSubIndex(OID oid, boolean z) {
        throw new UnsupportedOperationException();
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.asn1.BERSerializable
    public int getBERLength() {
        long j2 = this.value;
        if (j2 < 0) {
            return 11;
        }
        return j2 < 2147483648L ? j2 < 32768 ? j2 < 128 ? 3 : 4 : j2 < 8388608 ? 5 : 6 : j2 < 140737488355328L ? j2 < 549755813888L ? 7 : 8 : j2 < 36028797018963968L ? 9 : 10;
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public int getSyntax() {
        return 70;
    }

    public long getValue() {
        return this.value;
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public int hashCode() {
        return (int) this.value;
    }

    public void increment() {
        this.value++;
    }

    @Override // org.snmp4j.smi.AssignableFromString
    public void setValue(String str) {
        this.value = Long.parseLong(str);
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public final int toInt() {
        return (int) getValue();
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public final long toLong() {
        return getValue();
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public String toString() {
        long j2 = this.value;
        if (j2 > 0 && j2 < Long.MAX_VALUE) {
            return Long.toString(j2);
        }
        byte[] bArr = new byte[8];
        for (int i2 = 0; i2 < 8; i2++) {
            bArr[i2] = (byte) ((this.value >> ((7 - i2) * 8)) & 255);
        }
        return new BigInteger(1, bArr).toString();
    }

    @Override // org.snmp4j.smi.AbstractVariable, org.snmp4j.smi.Variable
    public OID toSubIndex(boolean z) {
        throw new UnsupportedOperationException();
    }

    @Override // org.snmp4j.smi.AbstractVariable, java.lang.Comparable
    public int compareTo(Variable variable) {
        long j2 = ((Counter64) variable).value;
        for (int i2 = 63; i2 >= 0; i2--) {
            long j3 = this.value;
            if (((j3 >> i2) & 1) != ((j2 >> i2) & 1)) {
                return ((j3 >> i2) & 1) != 0 ? 1 : -1;
            }
        }
        return 0;
    }

    public long increment(long j2) {
        if (j2 >= 0) {
            long j3 = this.value + j2;
            this.value = j3;
            return j3;
        }
        throw new IllegalArgumentException("Counter64 allows only positive increments: " + j2);
    }

    @Override // org.snmp4j.smi.AssignableFromLong
    public void setValue(long j2) {
        this.value = j2;
    }

    public Counter64(long j2) {
        setValue(j2);
    }
}
