package d;

import java.io.IOException;
import java.nio.ByteBuffer;

/* JADX INFO: compiled from: RealBufferedSink.kt */
/* JADX INFO: loaded from: classes.dex */
public final class t implements g {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f1600c;

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

    public t(y yVar) {
        c.q.d.j.c(yVar, "sink");
        this.f1601d = yVar;
        this.f1599b = new f();
    }

    @Override // d.g
    public f a() {
        return this.f1599b;
    }

    @Override // d.g
    public g b(byte[] bArr) throws IOException {
        c.q.d.j.c(bArr, "source");
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.g0(bArr);
        g();
        return this;
    }

    @Override // d.g
    public g c(byte[] bArr, int i, int i2) throws IOException {
        c.q.d.j.c(bArr, "source");
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.h0(bArr, i, i2);
        g();
        return this;
    }

    @Override // d.y, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws Throwable {
        if (this.f1600c) {
            return;
        }
        Throwable th = null;
        try {
            if (this.f1599b.b0() > 0) {
                this.f1601d.write(this.f1599b, this.f1599b.b0());
            }
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f1601d.close();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        this.f1600c = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // d.g
    public g d(i iVar) throws IOException {
        c.q.d.j.c(iVar, "byteString");
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.f0(iVar);
        g();
        return this;
    }

    @Override // d.g
    public long f(a0 a0Var) throws IOException {
        c.q.d.j.c(a0Var, "source");
        long j = 0;
        while (true) {
            long j2 = a0Var.read(this.f1599b, 8192);
            if (j2 == -1) {
                return j;
            }
            j += j2;
            g();
        }
    }

    @Override // d.g, d.y, java.io.Flushable
    public void flush() throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        if (this.f1599b.b0() > 0) {
            y yVar = this.f1601d;
            f fVar = this.f1599b;
            yVar.write(fVar, fVar.b0());
        }
        this.f1601d.flush();
    }

    @Override // d.g
    public g g() throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        long jG = this.f1599b.G();
        if (jG > 0) {
            this.f1601d.write(this.f1599b, jG);
        }
        return this;
    }

    @Override // d.g
    public g h(long j) throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.l0(j);
        g();
        return this;
    }

    @Override // java.nio.channels.Channel
    public boolean isOpen() {
        return !this.f1600c;
    }

    @Override // d.g
    public g n() throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        long jB0 = this.f1599b.b0();
        if (jB0 > 0) {
            this.f1601d.write(this.f1599b, jB0);
        }
        return this;
    }

    @Override // d.g
    public g o(int i) throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.o0(i);
        g();
        return this;
    }

    @Override // d.g
    public g q(int i) throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.m0(i);
        g();
        return this;
    }

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

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

    @Override // d.g
    public g w(String str) throws IOException {
        c.q.d.j.c(str, "string");
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.r0(str);
        g();
        return this;
    }

    @Override // d.y
    public void write(f fVar, long j) throws IOException {
        c.q.d.j.c(fVar, "source");
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.write(fVar, j);
        g();
    }

    @Override // d.g
    public g x(long j) throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.k0(j);
        g();
        return this;
    }

    @Override // d.g
    public g z(int i) throws IOException {
        if (!(!this.f1600c)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f1599b.j0(i);
        g();
        return this;
    }

    @Override // java.nio.channels.WritableByteChannel
    public int write(ByteBuffer byteBuffer) throws IOException {
        c.q.d.j.c(byteBuffer, "source");
        if (!this.f1600c) {
            int iWrite = this.f1599b.write(byteBuffer);
            g();
            return iWrite;
        }
        throw new IllegalStateException("closed".toString());
    }
}
