package h;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class r implements g {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final w f6568c;

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

    public r(w wVar) {
        Objects.requireNonNull(wVar, "sink == null");
        this.f6568c = wVar;
    }

    @Override // h.g
    public g C(String str) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.e0(str);
        a();
        return this;
    }

    @Override // h.g
    public g D(long j2) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.D(j2);
        a();
        return this;
    }

    public g a() {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        long jL = this.f6567b.L();
        if (jL > 0) {
            this.f6568c.h(this.f6567b, jL);
        }
        return this;
    }

    @Override // h.g
    public f b() {
        return this.f6567b;
    }

    @Override // h.w, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws Throwable {
        if (this.f6569d) {
            return;
        }
        Throwable th = null;
        try {
            f fVar = this.f6567b;
            long j2 = fVar.f6543d;
            if (j2 > 0) {
                this.f6568c.h(fVar, j2);
            }
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f6568c.close();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        this.f6569d = true;
        if (th == null) {
            return;
        }
        Charset charset = z.f6589a;
        throw th;
    }

    @Override // h.g
    public g d(int i2) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.c0(i2);
        a();
        return this;
    }

    @Override // h.w
    public y e() {
        return this.f6568c.e();
    }

    @Override // h.g
    public g f(byte[] bArr) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.X(bArr);
        a();
        return this;
    }

    @Override // h.g, h.w, java.io.Flushable
    public void flush() {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        f fVar = this.f6567b;
        long j2 = fVar.f6543d;
        if (j2 > 0) {
            this.f6568c.h(fVar, j2);
        }
        this.f6568c.flush();
    }

    @Override // h.g
    public g g(byte[] bArr, int i2, int i3) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.Y(bArr, i2, i3);
        a();
        return this;
    }

    @Override // h.w
    public void h(f fVar, long j2) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.h(fVar, j2);
        a();
    }

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

    @Override // h.g
    public g j(long j2) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.j(j2);
        return a();
    }

    @Override // h.g
    public g m(int i2) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.d0(i2);
        a();
        return this;
    }

    @Override // h.g
    public g r(int i2) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.Z(i2);
        a();
        return this;
    }

    @Override // h.g
    public g t(i iVar) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        this.f6567b.W(iVar);
        a();
        return this;
    }

    public String toString() {
        StringBuilder sbC = a.b.c.a.a.C("buffer(");
        sbC.append(this.f6568c);
        sbC.append(")");
        return sbC.toString();
    }

    @Override // java.nio.channels.WritableByteChannel
    public int write(ByteBuffer byteBuffer) {
        if (this.f6569d) {
            throw new IllegalStateException("closed");
        }
        int iWrite = this.f6567b.write(byteBuffer);
        a();
        return iWrite;
    }
}
