package l;

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 {
    private final OutputStream b;
    private final b0 d;

    public r(OutputStream outputStream, b0 b0Var) {
        j.p0.d.r.g(outputStream, "out");
        j.p0.d.r.g(b0Var, "timeout");
        this.b = outputStream;
        this.d = b0Var;
    }

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

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

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

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

    @Override // l.y
    public void write(f fVar, long j2) throws IOException {
        j.p0.d.r.g(fVar, "source");
        c.b(fVar.K(), 0L, j2);
        while (j2 > 0) {
            this.d.throwIfReached();
            v vVar = fVar.b;
            if (vVar == null) {
                j.p0.d.r.p();
                throw null;
            }
            int iMin = (int) Math.min(j2, vVar.c - vVar.b);
            this.b.write(vVar.a, vVar.b, iMin);
            vVar.b += iMin;
            long j3 = iMin;
            j2 -= j3;
            fVar.I(fVar.K() - j3);
            if (vVar.b == vVar.c) {
                fVar.b = vVar.b();
                w.c.a(vVar);
            }
        }
    }
}
