package j;

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 u implements f {

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

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

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

    public u(z zVar) {
        g.h0.c.l.f(zVar, "sink");
        this.f4644f = zVar;
        this.f4642b = new d();
    }

    @Override // j.f
    public d buffer() {
        return this.f4642b;
    }

    @Override // j.z, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws Throwable {
        if (this.f4643e) {
            return;
        }
        Throwable th = null;
        try {
            d dVar = this.f4642b;
            long j2 = dVar.f4601e;
            if (j2 > 0) {
                this.f4644f.write(dVar, j2);
            }
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f4644f.close();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        this.f4643e = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // j.f
    public f emit() throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        d dVar = this.f4642b;
        long j2 = dVar.f4601e;
        if (j2 > 0) {
            this.f4644f.write(dVar, j2);
        }
        return this;
    }

    @Override // j.f
    public f emitCompleteSegments() throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        long jB = this.f4642b.b();
        if (jB > 0) {
            this.f4644f.write(this.f4642b, jB);
        }
        return this;
    }

    @Override // j.f, j.z, java.io.Flushable
    public void flush() throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        d dVar = this.f4642b;
        long j2 = dVar.f4601e;
        if (j2 > 0) {
            this.f4644f.write(dVar, j2);
        }
        this.f4644f.flush();
    }

    @Override // j.f
    public d getBuffer() {
        return this.f4642b;
    }

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

    @Override // j.z
    public c0 timeout() {
        return this.f4644f.timeout();
    }

    public String toString() {
        StringBuilder sbB = d.b.a.a.a.B("buffer(");
        sbB.append(this.f4644f);
        sbB.append(')');
        return sbB.toString();
    }

    @Override // java.nio.channels.WritableByteChannel
    public int write(ByteBuffer byteBuffer) throws IOException {
        g.h0.c.l.f(byteBuffer, "source");
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        int iWrite = this.f4642b.write(byteBuffer);
        emitCompleteSegments();
        return iWrite;
    }

    @Override // j.f
    public long writeAll(b0 b0Var) throws IOException {
        g.h0.c.l.f(b0Var, "source");
        long j2 = 0;
        while (true) {
            long j3 = ((p) b0Var).read(this.f4642b, InvokeCmd.CMD_TOUCH_CTRL_START);
            if (j3 == -1) {
                return j2;
            }
            j2 += j3;
            emitCompleteSegments();
        }
    }

    @Override // j.f
    public f writeByte(int i2) throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f4642b.D(i2);
        emitCompleteSegments();
        return this;
    }

    @Override // j.f
    public f writeDecimalLong(long j2) throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f4642b.writeDecimalLong(j2);
        emitCompleteSegments();
        return this;
    }

    @Override // j.f
    public f writeHexadecimalUnsignedLong(long j2) throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f4642b.writeHexadecimalUnsignedLong(j2);
        emitCompleteSegments();
        return this;
    }

    @Override // j.f
    public f writeInt(int i2) throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f4642b.J(i2);
        emitCompleteSegments();
        return this;
    }

    @Override // j.f
    public f writeShort(int i2) throws IOException {
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f4642b.N(i2);
        emitCompleteSegments();
        return this;
    }

    @Override // j.f
    public f writeUtf8(String str) throws IOException {
        g.h0.c.l.f(str, "string");
        if (!(!this.f4643e)) {
            throw new IllegalStateException("closed".toString());
        }
        this.f4642b.P(str);
        emitCompleteSegments();
        return this;
    }

    @Override // j.z
    public void write(d dVar, long j2) throws IOException {
        g.h0.c.l.f(dVar, "source");
        if (!this.f4643e) {
            this.f4642b.write(dVar, j2);
            emitCompleteSegments();
            return;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // j.f
    public f write(h hVar) throws IOException {
        g.h0.c.l.f(hVar, "byteString");
        if (!this.f4643e) {
            this.f4642b.y(hVar);
            emitCompleteSegments();
            return this;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // j.f
    public f write(byte[] bArr) throws IOException {
        g.h0.c.l.f(bArr, "source");
        if (!this.f4643e) {
            this.f4642b.z(bArr);
            emitCompleteSegments();
            return this;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // j.f
    public f write(byte[] bArr, int i2, int i3) throws IOException {
        g.h0.c.l.f(bArr, "source");
        if (!this.f4643e) {
            this.f4642b.A(bArr, i2, i3);
            emitCompleteSegments();
            return this;
        }
        throw new IllegalStateException("closed".toString());
    }
}
