package d;

import java.io.IOException;
import java.io.OutputStream;

/* JADX INFO: compiled from: Okio.kt */
/* JADX INFO: loaded from: classes.dex */
final class r implements y {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final OutputStream f1592b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final b0 f1593c;

    public r(OutputStream outputStream, b0 b0Var) {
        c.q.d.j.c(outputStream, "out");
        c.q.d.j.c(b0Var, "timeout");
        this.f1592b = outputStream;
        this.f1593c = b0Var;
    }

    @Override // d.y, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.f1592b.close();
    }

    @Override // d.y, java.io.Flushable
    public void flush() throws IOException {
        this.f1592b.flush();
    }

    @Override // d.y
    public b0 timeout() {
        return this.f1593c;
    }

    public String toString() {
        return "sink(" + this.f1592b + ')';
    }

    @Override // d.y
    public void write(f fVar, long j) throws IOException {
        c.q.d.j.c(fVar, "source");
        c.b(fVar.b0(), 0L, j);
        while (j > 0) {
            this.f1593c.throwIfReached();
            v vVar = fVar.f1563b;
            if (vVar == null) {
                c.q.d.j.g();
                throw null;
            }
            int iMin = (int) Math.min(j, vVar.f1608c - vVar.f1607b);
            this.f1592b.write(vVar.f1606a, vVar.f1607b, iMin);
            vVar.f1607b += iMin;
            long j2 = iMin;
            j -= j2;
            fVar.a0(fVar.b0() - j2);
            if (vVar.f1607b == vVar.f1608c) {
                fVar.f1563b = vVar.b();
                w.a(vVar);
            }
        }
    }
}
