package i;

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 w implements g {
    public final f b;

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

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

    public w(b0 b0Var) {
        h.x.d.k.d(b0Var, "sink");
        this.f1050f = b0Var;
        this.b = new f();
    }

    @Override // i.g
    public g G(i iVar) throws IOException {
        h.x.d.k.d(iVar, "byteString");
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.Q(iVar);
        L();
        return this;
    }

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

    @Override // i.g
    public g Z(String str) throws IOException {
        h.x.d.k.d(str, "string");
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.i0(str);
        L();
        return this;
    }

    @Override // i.g
    public g b0(long j2) throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.V(j2);
        L();
        return this;
    }

    @Override // i.g
    public f c() {
        return this.b;
    }

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

    @Override // i.g
    public long e(d0 d0Var) throws IOException {
        h.x.d.k.d(d0Var, "source");
        long j2 = 0;
        while (true) {
            long j3 = d0Var.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START);
            if (j3 == -1) {
                return j2;
            }
            j2 += j3;
            L();
        }
    }

    @Override // i.g
    public g f(long j2) throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.W(j2);
        L();
        return this;
    }

    @Override // i.g, i.b0, java.io.Flushable
    public void flush() throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (this.b.J() > 0) {
            b0 b0Var = this.f1050f;
            f fVar = this.b;
            b0Var.write(fVar, fVar.J());
        }
        this.f1050f.flush();
    }

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

    @Override // i.g
    public g k() throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        long J = this.b.J();
        if (J > 0) {
            this.f1050f.write(this.b, J);
        }
        return this;
    }

    @Override // i.b0
    public e0 timeout() {
        return this.f1050f.timeout();
    }

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

    @Override // java.nio.channels.WritableByteChannel
    public int write(ByteBuffer byteBuffer) throws IOException {
        h.x.d.k.d(byteBuffer, "source");
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        int iWrite = this.b.write(byteBuffer);
        L();
        return iWrite;
    }

    @Override // i.g
    public g writeByte(int i2) throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.U(i2);
        L();
        return this;
    }

    @Override // i.g
    public g writeInt(int i2) throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.X(i2);
        L();
        return this;
    }

    @Override // i.g
    public g writeShort(int i2) throws IOException {
        if (!(!this.f1049e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.b.c0(i2);
        L();
        return this;
    }

    @Override // i.b0
    public void write(f fVar, long j2) throws IOException {
        h.x.d.k.d(fVar, "source");
        if (!this.f1049e) {
            this.b.write(fVar, j2);
            L();
            return;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // i.g
    public g write(byte[] bArr) throws IOException {
        h.x.d.k.d(bArr, "source");
        if (!this.f1049e) {
            this.b.S(bArr);
            L();
            return this;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // i.g
    public g write(byte[] bArr, int i2, int i3) throws IOException {
        h.x.d.k.d(bArr, "source");
        if (!this.f1049e) {
            this.b.T(bArr, i2, i3);
            L();
            return this;
        }
        throw new IllegalStateException("closed".toString());
    }
}
