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

import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;

/* JADX INFO: loaded from: classes2.dex */
public final class lb2 extends InputStream {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Iterator f13609a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public ByteBuffer f13610b;

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public byte[] f13615g;

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

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

    public final boolean e() {
        ByteBuffer byteBuffer;
        do {
            this.f13612d++;
            Iterator it = this.f13609a;
            if (!it.hasNext()) {
                return false;
            }
            byteBuffer = (ByteBuffer) it.next();
            this.f13610b = byteBuffer;
        } while (!byteBuffer.hasRemaining());
        this.f13613e = this.f13610b.position();
        if (this.f13610b.hasArray()) {
            this.f13614f = true;
            this.f13615g = this.f13610b.array();
            this.f13616h = this.f13610b.arrayOffset();
        } else {
            this.f13614f = false;
            this.f13617i = sc2.s(this.f13610b);
            this.f13615g = null;
        }
        return true;
    }

    public final void g(int i10) {
        int i11 = this.f13613e + i10;
        this.f13613e = i11;
        if (i11 == this.f13610b.limit()) {
            e();
        }
    }

    @Override // java.io.InputStream
    public final int read() {
        if (this.f13612d == this.f13611c) {
            return -1;
        }
        if (this.f13614f) {
            int i10 = this.f13615g[this.f13613e + this.f13616h] & 255;
            g(1);
            return i10;
        }
        int iF0 = sc2.f16407c.F0(((long) this.f13613e) + this.f13617i) & 255;
        g(1);
        return iF0;
    }

    @Override // java.io.InputStream
    public final int read(byte[] bArr, int i10, int i11) {
        if (this.f13612d == this.f13611c) {
            return -1;
        }
        int iLimit = this.f13610b.limit();
        int i12 = this.f13613e;
        int i13 = iLimit - i12;
        if (i11 > i13) {
            i11 = i13;
        }
        if (this.f13614f) {
            System.arraycopy(this.f13615g, i12 + this.f13616h, bArr, i10, i11);
            g(i11);
        } else {
            int iPosition = this.f13610b.position();
            this.f13610b.position(this.f13613e);
            this.f13610b.get(bArr, i10, i11);
            this.f13610b.position(iPosition);
            g(i11);
        }
        return i11;
    }
}
