package f;

import java.io.IOException;
import java.util.Objects;

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final s f3102d;

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

    n(s sVar) {
        Objects.requireNonNull(sVar, "sink == null");
        this.f3102d = sVar;
    }

    @Override // f.d
    public long G(t tVar) throws IOException {
        if (tVar == null) {
            throw new IllegalArgumentException("source == null");
        }
        long j = 0;
        while (true) {
            long j2 = tVar.read(this.f3101c, 8192L);
            if (j2 == -1) {
                return j;
            }
            j += j2;
            r();
        }
    }

    @Override // f.d
    public d H(long j) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.H(j);
        return r();
    }

    @Override // f.d
    public d U(f fVar) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.U(fVar);
        return r();
    }

    @Override // f.d
    public c buffer() {
        return this.f3101c;
    }

    @Override // f.s, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws Throwable {
        if (this.f3103e) {
            return;
        }
        Throwable th = null;
        try {
            c cVar = this.f3101c;
            long j = cVar.f3076e;
            if (j > 0) {
                this.f3102d.write(cVar, j);
            }
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f3102d.close();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        this.f3103e = true;
        if (th != null) {
            v.e(th);
        }
    }

    @Override // f.d
    public d d0(long j) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.d0(j);
        return r();
    }

    @Override // f.d
    public d f() throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        long jX = this.f3101c.x();
        if (jX > 0) {
            this.f3102d.write(this.f3101c, jX);
        }
        return this;
    }

    @Override // f.d, f.s, java.io.Flushable
    public void flush() throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        c cVar = this.f3101c;
        long j = cVar.f3076e;
        if (j > 0) {
            this.f3102d.write(cVar, j);
        }
        this.f3102d.flush();
    }

    @Override // f.d
    public d r() throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        long jE = this.f3101c.e();
        if (jE > 0) {
            this.f3102d.write(this.f3101c, jE);
        }
        return this;
    }

    @Override // f.s
    public u timeout() {
        return this.f3102d.timeout();
    }

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

    @Override // f.s
    public void write(c cVar, long j) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.write(cVar, j);
        r();
    }

    @Override // f.d
    public d writeByte(int i) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.writeByte(i);
        return r();
    }

    @Override // f.d
    public d writeInt(int i) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.writeInt(i);
        return r();
    }

    @Override // f.d
    public d writeLong(long j) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.writeLong(j);
        return r();
    }

    @Override // f.d
    public d writeShort(int i) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.writeShort(i);
        return r();
    }

    @Override // f.d
    public d z(String str) throws IOException {
        if (this.f3103e) {
            throw new IllegalStateException("closed");
        }
        this.f3101c.z(str);
        return r();
    }

    @Override // f.d
    public d write(byte[] bArr) throws IOException {
        if (!this.f3103e) {
            this.f3101c.write(bArr);
            return r();
        }
        throw new IllegalStateException("closed");
    }

    @Override // f.d
    public d write(byte[] bArr, int i, int i2) throws IOException {
        if (!this.f3103e) {
            this.f3101c.write(bArr, i, i2);
            return r();
        }
        throw new IllegalStateException("closed");
    }
}
