package h;

import java.io.IOException;

/* JADX INFO: compiled from: RealBufferedSink.java */
/* JADX INFO: loaded from: classes.dex */
final class m implements d {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final c f4198b = new c();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final r f4199f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    boolean f4200g;

    m(r rVar) {
        if (rVar == null) {
            throw new NullPointerException("sink == null");
        }
        this.f4199f = rVar;
    }

    @Override // h.d
    public d E(f fVar) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.H(fVar);
        K();
        return this;
    }

    @Override // h.d
    public d K() throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        long jG = this.f4198b.g();
        if (jG > 0) {
            this.f4199f.write(this.f4198b, jG);
        }
        return this;
    }

    @Override // h.d
    public d Y(String str) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.W(str);
        K();
        return this;
    }

    @Override // h.d
    public d a0(long j2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.P(j2);
        K();
        return this;
    }

    @Override // h.d
    public long b(s sVar) throws IOException {
        if (sVar == null) {
            throw new IllegalArgumentException("source == null");
        }
        long j2 = 0;
        while (true) {
            long j3 = sVar.read(this.f4198b, 8192L);
            if (j3 == -1) {
                return j2;
            }
            j2 += j3;
            K();
        }
    }

    @Override // h.d
    public c buffer() {
        return this.f4198b;
    }

    @Override // h.d
    public d c(long j2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.Q(j2);
        K();
        return this;
    }

    @Override // h.r, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws Throwable {
        if (this.f4200g) {
            return;
        }
        try {
            if (this.f4198b.f4177f > 0) {
                this.f4199f.write(this.f4198b, this.f4198b.f4177f);
            }
            th = null;
        } catch (Throwable th) {
            th = th;
        }
        try {
            this.f4199f.close();
        } catch (Throwable th2) {
            if (th == null) {
                th = th2;
            }
        }
        this.f4200g = true;
        if (th == null) {
            return;
        }
        u.e(th);
        throw null;
    }

    @Override // h.d, h.r, java.io.Flushable
    public void flush() throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        c cVar = this.f4198b;
        long j2 = cVar.f4177f;
        if (j2 > 0) {
            this.f4199f.write(cVar, j2);
        }
        this.f4199f.flush();
    }

    @Override // h.d
    public d i() throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        long jB = this.f4198b.B();
        if (jB > 0) {
            this.f4199f.write(this.f4198b, jB);
        }
        return this;
    }

    @Override // h.r
    public t timeout() {
        return this.f4199f.timeout();
    }

    public String toString() {
        return "buffer(" + this.f4199f + ")";
    }

    @Override // h.r
    public void write(c cVar, long j2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.write(cVar, j2);
        K();
    }

    @Override // h.d
    public d writeByte(int i2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.N(i2);
        K();
        return this;
    }

    @Override // h.d
    public d writeInt(int i2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.R(i2);
        K();
        return this;
    }

    @Override // h.d
    public d writeLong(long j2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.S(j2);
        K();
        return this;
    }

    @Override // h.d
    public d writeShort(int i2) throws IOException {
        if (this.f4200g) {
            throw new IllegalStateException("closed");
        }
        this.f4198b.T(i2);
        K();
        return this;
    }

    @Override // h.d
    public d write(byte[] bArr) throws IOException {
        if (!this.f4200g) {
            this.f4198b.J(bArr);
            K();
            return this;
        }
        throw new IllegalStateException("closed");
    }

    @Override // h.d
    public d write(byte[] bArr, int i2, int i3) throws IOException {
        if (!this.f4200g) {
            this.f4198b.M(bArr, i2, i3);
            K();
            return this;
        }
        throw new IllegalStateException("closed");
    }
}
