package kotlin.reflect.w.internal.y0.i;

import com.cvte.vman.instance.InvokeCmd;
import io.netty.handler.codec.dns.DnsRecord;
import java.io.IOException;
import java.io.OutputStream;

/* JADX INFO: loaded from: classes2.dex */
public final class e {
    public final byte[] a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final int f8250b;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final OutputStream f8253e;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f8252d = 0;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f8251c = 0;

    public static class a extends IOException {
        public a() {
            super("CodedOutputStream was writing to a flat byte array and ran out of space.");
        }
    }

    public e(OutputStream outputStream, byte[] bArr) {
        this.f8253e = outputStream;
        this.a = bArr;
        this.f8250b = bArr.length;
    }

    public static int a(c cVar) {
        return cVar.size() + g(cVar.size());
    }

    public static int b(int i2, int i3) {
        return i(i2) + d(i3);
    }

    public static int c(int i2, int i3) {
        return d(i3) + i(i2);
    }

    public static int d(int i2) {
        if (i2 >= 0) {
            return g(i2);
        }
        return 10;
    }

    public static int e(int i2, p pVar) {
        int i3 = i(i2);
        int iF = pVar.f();
        return i3 + g(iF) + iF;
    }

    public static int f(p pVar) {
        int iF = pVar.f();
        return g(iF) + iF;
    }

    public static int g(int i2) {
        if ((i2 & (-128)) == 0) {
            return 1;
        }
        if ((i2 & (-16384)) == 0) {
            return 2;
        }
        if (((-2097152) & i2) == 0) {
            return 3;
        }
        return (i2 & (-268435456)) == 0 ? 4 : 5;
    }

    public static int h(long j2) {
        if (((-128) & j2) == 0) {
            return 1;
        }
        if (((-16384) & j2) == 0) {
            return 2;
        }
        if (((-2097152) & j2) == 0) {
            return 3;
        }
        if (((-268435456) & j2) == 0) {
            return 4;
        }
        if (((-34359738368L) & j2) == 0) {
            return 5;
        }
        if (((-4398046511104L) & j2) == 0) {
            return 6;
        }
        if (((-562949953421312L) & j2) == 0) {
            return 7;
        }
        if (((-72057594037927936L) & j2) == 0) {
            return 8;
        }
        return (j2 & Long.MIN_VALUE) == 0 ? 9 : 10;
    }

    public static int i(int i2) {
        return g((i2 << 3) | 0);
    }

    public static e k(OutputStream outputStream, int i2) {
        return new e(outputStream, new byte[i2]);
    }

    public void A(int i2, int i3) {
        y((i2 << 3) | i3);
    }

    public void j() throws IOException {
        if (this.f8253e != null) {
            l();
        }
    }

    public final void l() throws IOException {
        OutputStream outputStream = this.f8253e;
        if (outputStream == null) {
            throw new a();
        }
        outputStream.write(this.a, 0, this.f8251c);
        this.f8251c = 0;
    }

    public void m(c cVar) {
        y(cVar.size());
        u(cVar);
    }

    public void n(int i2, int i3) {
        y((i2 << 3) | 0);
        if (i3 >= 0) {
            y(i3);
        } else {
            z(i3);
        }
    }

    public void o(int i2) {
        if (i2 >= 0) {
            y(i2);
        } else {
            z(i2);
        }
    }

    public void p(int i2, int i3) {
        y((i2 << 3) | 0);
        if (i3 >= 0) {
            y(i3);
        } else {
            z(i3);
        }
    }

    public void q(int i2) {
        if (i2 >= 0) {
            y(i2);
        } else {
            z(i2);
        }
    }

    public void r(int i2, p pVar) {
        y((i2 << 3) | 2);
        y(pVar.f());
        pVar.d(this);
    }

    public void s(p pVar) {
        y(pVar.f());
        pVar.d(this);
    }

    public void t(int i2) {
        byte b2 = (byte) i2;
        if (this.f8251c == this.f8250b) {
            l();
        }
        byte[] bArr = this.a;
        int i3 = this.f8251c;
        this.f8251c = i3 + 1;
        bArr[i3] = b2;
        this.f8252d++;
    }

    public void u(c cVar) throws IOException {
        int size = cVar.size();
        int i2 = this.f8250b;
        int i3 = this.f8251c;
        int i4 = i2 - i3;
        if (i4 >= size) {
            cVar.d(this.a, 0, i3, size);
            this.f8251c += size;
        } else {
            cVar.d(this.a, 0, i3, i4);
            int i5 = 0 + i4;
            size -= i4;
            this.f8251c = this.f8250b;
            this.f8252d += i4;
            l();
            if (size <= this.f8250b) {
                cVar.d(this.a, i5, 0, size);
                this.f8251c = size;
            } else {
                OutputStream outputStream = this.f8253e;
                if (i5 < 0) {
                    StringBuilder sb = new StringBuilder(30);
                    sb.append("Source offset < 0: ");
                    sb.append(i5);
                    throw new IndexOutOfBoundsException(sb.toString());
                }
                if (size < 0) {
                    StringBuilder sb2 = new StringBuilder(23);
                    sb2.append("Length < 0: ");
                    sb2.append(size);
                    throw new IndexOutOfBoundsException(sb2.toString());
                }
                int i6 = i5 + size;
                if (i6 > cVar.size()) {
                    StringBuilder sb3 = new StringBuilder(39);
                    sb3.append("Source end offset exceeded: ");
                    sb3.append(i6);
                    throw new IndexOutOfBoundsException(sb3.toString());
                }
                if (size > 0) {
                    cVar.v(outputStream, i5, size);
                }
            }
        }
        this.f8252d += size;
    }

    public void v(byte[] bArr) {
        int length = bArr.length;
        int i2 = this.f8250b;
        int i3 = this.f8251c;
        int i4 = i2 - i3;
        if (i4 >= length) {
            System.arraycopy(bArr, 0, this.a, i3, length);
            this.f8251c += length;
        } else {
            System.arraycopy(bArr, 0, this.a, i3, i4);
            int i5 = i4 + 0;
            length -= i4;
            this.f8251c = this.f8250b;
            this.f8252d += i4;
            l();
            if (length <= this.f8250b) {
                System.arraycopy(bArr, i5, this.a, 0, length);
                this.f8251c = length;
            } else {
                this.f8253e.write(bArr, i5, length);
            }
        }
        this.f8252d += length;
    }

    public void w(int i2) {
        t(i2 & DnsRecord.CLASS_ANY);
        t((i2 >> 8) & DnsRecord.CLASS_ANY);
        t((i2 >> 16) & DnsRecord.CLASS_ANY);
        t((i2 >> 24) & DnsRecord.CLASS_ANY);
    }

    public void x(long j2) {
        t(((int) j2) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 8)) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 16)) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 24)) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 32)) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 40)) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 48)) & DnsRecord.CLASS_ANY);
        t(((int) (j2 >> 56)) & DnsRecord.CLASS_ANY);
    }

    public void y(int i2) {
        while ((i2 & (-128)) != 0) {
            t((i2 & InvokeCmd.CMD_SYSTEMCTRL_IS_PRODUCTION_MODE) | 128);
            i2 >>>= 7;
        }
        t(i2);
    }

    public void z(long j2) {
        while (((-128) & j2) != 0) {
            t((((int) j2) & InvokeCmd.CMD_SYSTEMCTRL_IS_PRODUCTION_MODE) | 128);
            j2 >>>= 7;
        }
        t((int) j2);
    }
}
