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

import androidx.media3.common.C;
import java.io.EOFException;
import java.io.InterruptedIOException;
import java.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public final class h2 implements p2 {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final long f11989c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public long f11990d;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public byte[] f11991e = new byte[C.DEFAULT_BUFFER_SEGMENT_SIZE];

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final byte[] f11987a = new byte[4096];

    static {
        n7.a("media3.extractor");
    }

    public h2(sv1 sv1Var, long j10, long j11) {
        this.f11988b = sv1Var;
        this.f11990d = j10;
        this.f11989c = j11;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final void a(int i10) throws EOFException, InterruptedIOException {
        l(i10, false);
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final void c(byte[] bArr, int i10, int i11) throws EOFException, InterruptedIOException {
        i(bArr, i10, i11, false);
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws EOFException, InterruptedIOException {
        int i12 = this.f11993g;
        int iO = 0;
        if (i12 != 0) {
            int iMin = Math.min(i12, i11);
            System.arraycopy(this.f11991e, 0, bArr, i10, iMin);
            n(iMin);
            iO = iMin;
        }
        if (iO == 0) {
            iO = o(bArr, i10, i11, 0, true);
        }
        if (iO != -1) {
            this.f11990d += (long) iO;
        }
        return iO;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final void e(int i10) throws EOFException, InterruptedIOException {
        f(i10, false);
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final boolean f(int i10, boolean z10) throws EOFException, InterruptedIOException {
        int iMin = Math.min(this.f11993g, i10);
        n(iMin);
        int iO = iMin;
        while (iO < i10 && iO != -1) {
            iO = o(this.f11987a, -iO, Math.min(i10, iO + 4096), iO, z10);
        }
        if (iO != -1) {
            this.f11990d += (long) iO;
        }
        return iO != -1;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final void h(int i10, int i11, byte[] bArr) {
        j(bArr, i10, i11, false);
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final boolean i(byte[] bArr, int i10, int i11, boolean z10) throws EOFException, InterruptedIOException {
        int iMin;
        int i12 = this.f11993g;
        if (i12 == 0) {
            iMin = 0;
        } else {
            iMin = Math.min(i12, i11);
            System.arraycopy(this.f11991e, 0, bArr, i10, iMin);
            n(iMin);
        }
        int iO = iMin;
        while (iO < i11 && iO != -1) {
            iO = o(bArr, i10, i11, iO, z10);
        }
        if (iO != -1) {
            this.f11990d += (long) iO;
        }
        return iO != -1;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final boolean j(byte[] bArr, int i10, int i11, boolean z10) {
        if (!l(i11, z10)) {
            return false;
        }
        System.arraycopy(this.f11991e, this.f11992f - i11, bArr, i10, i11);
        return true;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final int k(int i10, int i11, byte[] bArr) throws EOFException, InterruptedIOException {
        int iMin;
        m(i11);
        int i12 = this.f11993g;
        int i13 = this.f11992f;
        int i14 = i12 - i13;
        if (i14 == 0) {
            iMin = o(this.f11991e, i13, i11, 0, true);
            if (iMin == -1) {
                return -1;
            }
            this.f11993g += iMin;
        } else {
            iMin = Math.min(i11, i14);
        }
        System.arraycopy(this.f11991e, this.f11992f, bArr, i10, iMin);
        this.f11992f += iMin;
        return iMin;
    }

    public final boolean l(int i10, boolean z10) throws EOFException, InterruptedIOException {
        m(i10);
        int iO = this.f11993g - this.f11992f;
        while (iO < i10) {
            iO = o(this.f11991e, this.f11992f, i10, iO, z10);
            if (iO == -1) {
                return false;
            }
            this.f11993g = this.f11992f + iO;
        }
        this.f11992f += i10;
        return true;
    }

    public final void m(int i10) {
        int i11 = this.f11992f + i10;
        int length = this.f11991e.length;
        if (i11 > length) {
            String str = bb1.f9474a;
            this.f11991e = Arrays.copyOf(this.f11991e, Math.max(C.DEFAULT_BUFFER_SEGMENT_SIZE + i11, Math.min(length + length, i11 + 524288)));
        }
    }

    public final void n(int i10) {
        int i11 = this.f11993g - i10;
        this.f11993g = i11;
        this.f11992f = 0;
        byte[] bArr = this.f11991e;
        byte[] bArr2 = i11 < bArr.length + (-524288) ? new byte[C.DEFAULT_BUFFER_SEGMENT_SIZE + i11] : bArr;
        System.arraycopy(bArr, i10, bArr2, 0, i11);
        this.f11991e = bArr2;
    }

    public final int o(byte[] bArr, int i10, int i11, int i12, boolean z10) throws EOFException, InterruptedIOException {
        if (Thread.interrupted()) {
            throw new InterruptedIOException();
        }
        int iD = this.f11988b.d(bArr, i10 + i12, i11 - i12);
        if (iD != -1) {
            return i12 + iD;
        }
        if (i12 == 0 && z10) {
            return -1;
        }
        throw new EOFException();
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final int zzd() throws EOFException, InterruptedIOException {
        int iMin = Math.min(this.f11993g, 1);
        n(iMin);
        if (iMin == 0) {
            iMin = o(this.f11987a, 0, Math.min(1, 4096), 0, true);
        }
        if (iMin != -1) {
            this.f11990d += (long) iMin;
        }
        return iMin;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final void zzl() {
        this.f11992f = 0;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final long zzm() {
        return this.f11990d + ((long) this.f11992f);
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final long zzn() {
        return this.f11990d;
    }

    @Override // com.google.android.gms.internal.ads.p2
    public final long zzo() {
        return this.f11989c;
    }
}
