package l;

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

/* JADX INFO: compiled from: Okio.kt */
/* JADX INFO: loaded from: classes.dex */
final class o implements a0 {
    private final InputStream b;
    private final b0 d;

    public o(InputStream inputStream, b0 b0Var) {
        j.p0.d.r.g(inputStream, "input");
        j.p0.d.r.g(b0Var, "timeout");
        this.b = inputStream;
        this.d = b0Var;
    }

    @Override // l.a0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.b.close();
    }

    @Override // l.a0
    public long read(f fVar, long j2) throws IOException {
        j.p0.d.r.g(fVar, "sink");
        if (j2 == 0) {
            return 0L;
        }
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        try {
            this.d.throwIfReached();
            v vVarP = fVar.P(1);
            int i2 = this.b.read(vVarP.a, vVarP.c, (int) Math.min(j2, 8192 - vVarP.c));
            if (i2 != -1) {
                vVarP.c += i2;
                long j3 = i2;
                fVar.I(fVar.K() + j3);
                return j3;
            }
            if (vVarP.b != vVarP.c) {
                return -1L;
            }
            fVar.b = vVarP.b();
            w.c.a(vVarP);
            return -1L;
        } catch (AssertionError e) {
            if (p.e(e)) {
                throw new IOException(e);
            }
            throw e;
        }
    }

    @Override // l.a0
    public b0 timeout() {
        return this.d;
    }

    public String toString() {
        return "source(" + this.b + ')';
    }
}
