package g2;

import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: compiled from: ConstructedOctetStream.java */
/* JADX INFO: loaded from: classes.dex */
class k0 extends InputStream {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final w f4716e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private boolean f4717f = true;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private InputStream f4718g;

    k0(w wVar) {
        this.f4716e = wVar;
    }

    private p a() throws IOException {
        d dVarB = this.f4716e.b();
        if (dVarB == null) {
            return null;
        }
        if (dVarB instanceof p) {
            return (p) dVarB;
        }
        throw new IOException("unknown object encountered: " + dVarB.getClass());
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i3, int i4) throws IOException {
        p pVarA;
        int i5 = 0;
        if (this.f4718g == null) {
            if (!this.f4717f || (pVarA = a()) == null) {
                return -1;
            }
            this.f4717f = false;
            this.f4718g = pVarA.b();
        }
        while (true) {
            int i6 = this.f4718g.read(bArr, i3 + i5, i4 - i5);
            if (i6 >= 0) {
                i5 += i6;
                if (i5 == i4) {
                    return i5;
                }
            } else {
                p pVarA2 = a();
                if (pVarA2 == null) {
                    this.f4718g = null;
                    if (i5 < 1) {
                        return -1;
                    }
                    return i5;
                }
                this.f4718g = pVarA2.b();
            }
        }
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        p pVarA;
        if (this.f4718g == null) {
            if (!this.f4717f || (pVarA = a()) == null) {
                return -1;
            }
            this.f4717f = false;
            this.f4718g = pVarA.b();
        }
        while (true) {
            int i3 = this.f4718g.read();
            if (i3 >= 0) {
                return i3;
            }
            p pVarA2 = a();
            if (pVarA2 == null) {
                this.f4718g = null;
                return -1;
            }
            this.f4718g = pVarA2.b();
        }
    }
}
