package l;

import com.cvte.vman.instance.InvokeCmd;
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 {
    public final f b;
    public boolean d;
    public final y e;

    public t(y yVar) {
        j.p0.d.r.g(yVar, "sink");
        this.e = yVar;
        this.b = new f();
    }

    @Override // l.g
    public g F(int i2) throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.X(i2);
        Q();
        return this;
    }

    @Override // l.g
    public g J(byte[] bArr) throws IOException {
        j.p0.d.r.g(bArr, "source");
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.V(bArr);
        Q();
        return this;
    }

    @Override // l.g
    public g L(i iVar) throws IOException {
        j.p0.d.r.g(iVar, "byteString");
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.S(iVar);
        Q();
        return this;
    }

    @Override // l.g
    public g Q() throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        long jI = this.b.i();
        if (jI > 0) {
            this.e.write(this.b, jI);
        }
        return this;
    }

    @Override // l.g
    public f a() {
        return this.b;
    }

    @Override // l.g
    public g b(byte[] bArr, int i2, int i3) throws IOException {
        j.p0.d.r.g(bArr, "source");
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.W(bArr, i2, i3);
        Q();
        return this;
    }

    @Override // l.y, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws Throwable {
        if (this.d) {
            return;
        }
        Throwable th = null;
        try {
            if (this.b.K() > 0) {
                this.e.write(this.b, this.b.K());
            }
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.e.close();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        this.d = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // l.g
    public long d(a0 a0Var) throws IOException {
        j.p0.d.r.g(a0Var, "source");
        long j2 = 0;
        while (true) {
            long j3 = a0Var.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START);
            if (j3 == -1) {
                return j2;
            }
            j2 += j3;
            Q();
        }
    }

    @Override // l.g
    public g e(long j2) throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.Z(j2);
        Q();
        return this;
    }

    @Override // l.g
    public g f0(String str) throws IOException {
        j.p0.d.r.g(str, "string");
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.j0(str);
        Q();
        return this;
    }

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

    @Override // l.g
    public g h0(long j2) throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.Y(j2);
        Q();
        return this;
    }

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

    @Override // l.g
    public g m() throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        long jK = this.b.K();
        if (jK > 0) {
            this.e.write(this.b, jK);
        }
        return this;
    }

    @Override // l.g
    public g o(int i2) throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.d0(i2);
        Q();
        return this;
    }

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

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

    @Override // l.g
    public g u(int i2) throws IOException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.b0(i2);
        Q();
        return this;
    }

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

    @Override // l.y
    public void write(f fVar, long j2) throws IOException {
        j.p0.d.r.g(fVar, "source");
        if (!this.d) {
            this.b.write(fVar, j2);
            Q();
            return;
        }
        throw new IllegalStateException("closed".toString());
    }
}
