package com.google.protobuf;

import java.io.IOException;
import java.io.OutputStream;

/* JADX INFO: renamed from: com.google.protobuf.u, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C1333u extends AbstractC1335v {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final byte[] f14357f;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f14359h;

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

    public C1333u(OutputStream outputStream, int i6) {
        if (i6 < 0) {
            throw new IllegalArgumentException("bufferSize must be >= 0");
        }
        int iMax = Math.max(i6, 20);
        this.f14357f = new byte[iMax];
        this.f14358g = iMax;
        if (outputStream == null) {
            throw new NullPointerException("out");
        }
        this.f14360i = outputStream;
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void H(byte b5) {
        if (this.f14359h == this.f14358g) {
            g0();
        }
        int i6 = this.f14359h;
        this.f14359h = i6 + 1;
        this.f14357f[i6] = b5;
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void I(int i6, boolean z6) {
        h0(11);
        d0(i6, 0);
        byte b5 = z6 ? (byte) 1 : (byte) 0;
        int i7 = this.f14359h;
        this.f14359h = i7 + 1;
        this.f14357f[i7] = b5;
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void J(byte[] bArr, int i6) throws IOException {
        Y(i6);
        i0(bArr, 0, i6);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void K(int i6, AbstractC1320n abstractC1320n) {
        W(i6, 2);
        L(abstractC1320n);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void L(AbstractC1320n abstractC1320n) {
        Y(abstractC1320n.size());
        abstractC1320n.A(this);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void M(int i6, int i7) {
        h0(14);
        d0(i6, 5);
        b0(i7);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void N(int i6) {
        h0(4);
        b0(i6);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void O(int i6, long j4) {
        h0(18);
        d0(i6, 1);
        c0(j4);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void P(long j4) {
        h0(8);
        c0(j4);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void Q(int i6, int i7) {
        h0(20);
        d0(i6, 0);
        if (i7 >= 0) {
            e0(i7);
        } else {
            f0(i7);
        }
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void R(int i6) {
        if (i6 >= 0) {
            Y(i6);
        } else {
            a0(i6);
        }
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void S(int i6, InterfaceC1332t0 interfaceC1332t0, J0 j02) {
        W(i6, 2);
        Y(((AbstractC1298c) interfaceC1332t0).getSerializedSize(j02));
        j02.e(interfaceC1332t0, this.f14363c);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void T(InterfaceC1332t0 interfaceC1332t0) {
        Y(interfaceC1332t0.getSerializedSize());
        interfaceC1332t0.writeTo(this);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void U(int i6, String str) throws IOException {
        W(i6, 2);
        V(str);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void V(String str) throws IOException {
        try {
            int length = str.length() * 3;
            int iE = AbstractC1335v.E(length);
            int i6 = iE + length;
            int i7 = this.f14358g;
            if (i6 > i7) {
                byte[] bArr = new byte[length];
                int iQ = f1.f14272a.q(str, bArr, 0, length);
                Y(iQ);
                i0(bArr, 0, iQ);
                return;
            }
            if (i6 > i7 - this.f14359h) {
                g0();
            }
            int iE2 = AbstractC1335v.E(str.length());
            int i8 = this.f14359h;
            byte[] bArr2 = this.f14357f;
            try {
                try {
                    if (iE2 == iE) {
                        int i9 = i8 + iE2;
                        this.f14359h = i9;
                        int iQ2 = f1.f14272a.q(str, bArr2, i9, i7 - i9);
                        this.f14359h = i8;
                        e0((iQ2 - i8) - iE2);
                        this.f14359h = iQ2;
                    } else {
                        int iC = f1.c(str);
                        e0(iC);
                        this.f14359h = f1.f14272a.q(str, bArr2, this.f14359h, iC);
                    }
                } catch (e1 e6) {
                    this.f14359h = i8;
                    throw e6;
                }
            } catch (ArrayIndexOutOfBoundsException e7) {
                throw new H4.c(e7);
            }
        } catch (e1 e8) {
            G(str, e8);
        }
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void W(int i6, int i7) {
        Y((i6 << 3) | i7);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void X(int i6, int i7) {
        h0(20);
        d0(i6, 0);
        e0(i7);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void Y(int i6) {
        h0(5);
        e0(i6);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void Z(int i6, long j4) {
        h0(20);
        d0(i6, 0);
        f0(j4);
    }

    @Override // com.google.protobuf.AbstractC1335v
    public final void a0(long j4) {
        h0(10);
        f0(j4);
    }

    public final void b0(int i6) {
        int i7 = this.f14359h;
        int i8 = i7 + 1;
        this.f14359h = i8;
        byte[] bArr = this.f14357f;
        bArr[i7] = (byte) (i6 & 255);
        int i9 = i7 + 2;
        this.f14359h = i9;
        bArr[i8] = (byte) ((i6 >> 8) & 255);
        int i10 = i7 + 3;
        this.f14359h = i10;
        bArr[i9] = (byte) ((i6 >> 16) & 255);
        this.f14359h = i7 + 4;
        bArr[i10] = (byte) ((i6 >> 24) & 255);
    }

    public final void c0(long j4) {
        int i6 = this.f14359h;
        int i7 = i6 + 1;
        this.f14359h = i7;
        byte[] bArr = this.f14357f;
        bArr[i6] = (byte) (j4 & 255);
        int i8 = i6 + 2;
        this.f14359h = i8;
        bArr[i7] = (byte) ((j4 >> 8) & 255);
        int i9 = i6 + 3;
        this.f14359h = i9;
        bArr[i8] = (byte) ((j4 >> 16) & 255);
        int i10 = i6 + 4;
        this.f14359h = i10;
        bArr[i9] = (byte) (255 & (j4 >> 24));
        int i11 = i6 + 5;
        this.f14359h = i11;
        bArr[i10] = (byte) (((int) (j4 >> 32)) & 255);
        int i12 = i6 + 6;
        this.f14359h = i12;
        bArr[i11] = (byte) (((int) (j4 >> 40)) & 255);
        int i13 = i6 + 7;
        this.f14359h = i13;
        bArr[i12] = (byte) (((int) (j4 >> 48)) & 255);
        this.f14359h = i6 + 8;
        bArr[i13] = (byte) (((int) (j4 >> 56)) & 255);
    }

    public final void d0(int i6, int i7) {
        e0((i6 << 3) | i7);
    }

    public final void e0(int i6) {
        boolean z6 = AbstractC1335v.f14362e;
        byte[] bArr = this.f14357f;
        if (z6) {
            while ((i6 & (-128)) != 0) {
                int i7 = this.f14359h;
                this.f14359h = i7 + 1;
                c1.k(bArr, i7, (byte) ((i6 & 127) | 128));
                i6 >>>= 7;
            }
            int i8 = this.f14359h;
            this.f14359h = i8 + 1;
            c1.k(bArr, i8, (byte) i6);
            return;
        }
        while ((i6 & (-128)) != 0) {
            int i9 = this.f14359h;
            this.f14359h = i9 + 1;
            bArr[i9] = (byte) ((i6 & 127) | 128);
            i6 >>>= 7;
        }
        int i10 = this.f14359h;
        this.f14359h = i10 + 1;
        bArr[i10] = (byte) i6;
    }

    public final void f0(long j4) {
        boolean z6 = AbstractC1335v.f14362e;
        byte[] bArr = this.f14357f;
        if (z6) {
            while ((j4 & (-128)) != 0) {
                int i6 = this.f14359h;
                this.f14359h = i6 + 1;
                c1.k(bArr, i6, (byte) ((((int) j4) & 127) | 128));
                j4 >>>= 7;
            }
            int i7 = this.f14359h;
            this.f14359h = i7 + 1;
            c1.k(bArr, i7, (byte) j4);
            return;
        }
        while ((j4 & (-128)) != 0) {
            int i8 = this.f14359h;
            this.f14359h = i8 + 1;
            bArr[i8] = (byte) ((((int) j4) & 127) | 128);
            j4 >>>= 7;
        }
        int i9 = this.f14359h;
        this.f14359h = i9 + 1;
        bArr[i9] = (byte) j4;
    }

    public final void g0() {
        this.f14360i.write(this.f14357f, 0, this.f14359h);
        this.f14359h = 0;
    }

    public final void h0(int i6) {
        if (this.f14358g - this.f14359h < i6) {
            g0();
        }
    }

    public final void i0(byte[] bArr, int i6, int i7) throws IOException {
        int i8 = this.f14359h;
        int i9 = this.f14358g;
        int i10 = i9 - i8;
        byte[] bArr2 = this.f14357f;
        if (i10 >= i7) {
            System.arraycopy(bArr, i6, bArr2, i8, i7);
            this.f14359h += i7;
            return;
        }
        System.arraycopy(bArr, i6, bArr2, i8, i10);
        int i11 = i6 + i10;
        int i12 = i7 - i10;
        this.f14359h = i9;
        g0();
        if (i12 > i9) {
            this.f14360i.write(bArr, i11, i12);
        } else {
            System.arraycopy(bArr, i11, bArr2, 0, i12);
            this.f14359h = i12;
        }
    }

    @Override // com.google.protobuf.O0
    public final void y(byte[] bArr, int i6, int i7) throws IOException {
        i0(bArr, i6, i7);
    }
}
