package com.google.android.gms.internal.ads;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public final class ja2 extends OutputStream {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final byte[] f12793f = new byte[0];

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

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int f12794a = 128;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ArrayList f12795b = new ArrayList();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public byte[] f12797d = new byte[128];

    public final synchronized zzian e() {
        try {
            int i10 = this.f12798e;
            byte[] bArr = this.f12797d;
            if (i10 >= bArr.length) {
                this.f12795b.add(new zzial(this.f12797d));
                this.f12797d = f12793f;
            } else if (i10 > 0) {
                this.f12795b.add(new zzial(Arrays.copyOf(bArr, i10)));
            }
            this.f12796c += this.f12798e;
            this.f12798e = 0;
        } catch (Throwable th) {
            throw th;
        }
        return zzian.y(this.f12795b);
    }

    public final void g(int i10) {
        this.f12795b.add(new zzial(this.f12797d));
        int length = this.f12796c + this.f12797d.length;
        this.f12796c = length;
        this.f12797d = new byte[Math.max(this.f12794a, Math.max(i10, length >>> 1))];
        this.f12798e = 0;
    }

    public final String toString() {
        int i10;
        Object[] objArr = new Object[2];
        objArr[0] = Integer.toHexString(System.identityHashCode(this));
        synchronized (this) {
            i10 = this.f12796c + this.f12798e;
        }
        objArr[1] = Integer.valueOf(i10);
        return String.format("<ByteString.Output@%s size=%d>", objArr);
    }

    @Override // java.io.OutputStream
    public final synchronized void write(int i10) {
        try {
            if (this.f12798e == this.f12797d.length) {
                g(1);
            }
            byte[] bArr = this.f12797d;
            int i11 = this.f12798e;
            this.f12798e = i11 + 1;
            bArr[i11] = (byte) i10;
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // java.io.OutputStream
    public final synchronized void write(byte[] bArr, int i10, int i11) {
        byte[] bArr2 = this.f12797d;
        int length = bArr2.length;
        int i12 = this.f12798e;
        int i13 = length - i12;
        if (i11 <= i13) {
            System.arraycopy(bArr, i10, bArr2, i12, i11);
            this.f12798e += i11;
            return;
        }
        System.arraycopy(bArr, i10, bArr2, i12, i13);
        int i14 = i11 - i13;
        g(i14);
        System.arraycopy(bArr, i10 + i13, this.f12797d, 0, i14);
        this.f12798e = i14;
    }
}
