package x7;

import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public final class A implements InterfaceC1732h, AutoCloseable {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final F f18721q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final C1731g f18722r;

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

    public A(F f3) {
        P6.g.e(f3, "sink");
        this.f18721q = f3;
        this.f18722r = new C1731g();
    }

    @Override // x7.InterfaceC1732h
    public final InterfaceC1732h S(int i, byte[] bArr) {
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.s0(bArr, 0, i);
        a();
        return this;
    }

    @Override // x7.F
    public final void X(C1731g c1731g, long j8) {
        P6.g.e(c1731g, "source");
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.X(c1731g, j8);
        a();
    }

    @Override // x7.InterfaceC1732h
    public final InterfaceC1732h Y(String str) {
        P6.g.e(str, "string");
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.z0(str);
        a();
        return this;
    }

    public final InterfaceC1732h a() {
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        C1731g c1731g = this.f18722r;
        long jN = c1731g.n();
        if (jN > 0) {
            this.f18721q.X(c1731g, jN);
        }
        return this;
    }

    @Override // x7.InterfaceC1732h
    public final InterfaceC1732h a0(long j8) {
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.v0(j8);
        a();
        return this;
    }

    public final InterfaceC1732h b(int i) {
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.x0(i);
        a();
        return this;
    }

    @Override // x7.F, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws Throwable {
        F f3 = this.f18721q;
        if (this.f18723s) {
            return;
        }
        try {
            C1731g c1731g = this.f18722r;
            long j8 = c1731g.f18765r;
            if (j8 > 0) {
                f3.X(c1731g, j8);
            }
            th = null;
        } catch (Throwable th) {
            th = th;
        }
        try {
            f3.close();
        } catch (Throwable th2) {
            if (th == null) {
                th = th2;
            }
        }
        this.f18723s = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // x7.InterfaceC1732h
    public final C1731g d() {
        return this.f18722r;
    }

    @Override // x7.InterfaceC1732h
    public final InterfaceC1732h d0(int i) {
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.u0(i);
        a();
        return this;
    }

    @Override // x7.F
    public final J f() {
        return this.f18721q.f();
    }

    @Override // x7.F, java.io.Flushable
    public final void flush() {
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        C1731g c1731g = this.f18722r;
        long j8 = c1731g.f18765r;
        F f3 = this.f18721q;
        if (j8 > 0) {
            f3.X(c1731g, j8);
        }
        f3.flush();
    }

    @Override // x7.InterfaceC1732h
    public final InterfaceC1732h h(byte[] bArr) {
        P6.g.e(bArr, "source");
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.s0(bArr, 0, bArr.length);
        a();
        return this;
    }

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

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

    @Override // java.nio.channels.WritableByteChannel
    public final int write(ByteBuffer byteBuffer) {
        P6.g.e(byteBuffer, "source");
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        int iWrite = this.f18722r.write(byteBuffer);
        a();
        return iWrite;
    }

    @Override // x7.InterfaceC1732h
    public final InterfaceC1732h x(C1734j c1734j) {
        P6.g.e(c1734j, "byteString");
        if (this.f18723s) {
            throw new IllegalStateException("closed");
        }
        this.f18722r.r0(c1734j);
        a();
        return this;
    }
}
