package p5;

import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes3.dex */
public final class I implements InterfaceC2741k {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final N f23055a;

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

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

    public I(N sink) {
        kotlin.jvm.internal.o.h(sink, "sink");
        this.f23055a = sink;
        this.f23056b = new C2740j();
    }

    public final InterfaceC2741k a() {
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        C2740j c2740j = this.f23056b;
        long jB = c2740j.b();
        if (jB > 0) {
            this.f23055a.write(c2740j, jB);
        }
        return this;
    }

    public final InterfaceC2741k b(int i6) {
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.T(i6);
        a();
        return this;
    }

    public final InterfaceC2741k c(int i6) {
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        C2740j c2740j = this.f23056b;
        K kM = c2740j.M(2);
        int i7 = kM.f23063c;
        byte[] bArr = kM.f23061a;
        bArr[i7] = (byte) ((i6 >>> 8) & 255);
        bArr[i7 + 1] = (byte) (i6 & 255);
        kM.f23063c = i7 + 2;
        c2740j.f23100b += 2;
        a();
        return this;
    }

    @Override // p5.N, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws Throwable {
        N n6 = this.f23055a;
        if (this.f23057c) {
            return;
        }
        try {
            C2740j c2740j = this.f23056b;
            long j4 = c2740j.f23100b;
            if (j4 > 0) {
                n6.write(c2740j, j4);
            }
            th = null;
        } catch (Throwable th) {
            th = th;
        }
        try {
            n6.close();
        } catch (Throwable th2) {
            if (th == null) {
                th = th2;
            }
        }
        this.f23057c = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // p5.InterfaceC2741k, p5.N, java.io.Flushable
    public final void flush() {
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        C2740j c2740j = this.f23056b;
        long j4 = c2740j.f23100b;
        N n6 = this.f23055a;
        if (j4 > 0) {
            n6.write(c2740j, j4);
        }
        n6.flush();
    }

    @Override // p5.InterfaceC2741k
    public final InterfaceC2741k h(int i6) {
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.Q(i6);
        a();
        return this;
    }

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

    @Override // p5.InterfaceC2741k
    public final InterfaceC2741k j(C2743m byteString) {
        kotlin.jvm.internal.o.h(byteString, "byteString");
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.N(byteString);
        a();
        return this;
    }

    @Override // p5.InterfaceC2741k
    public final InterfaceC2741k l(byte[] source, int i6) {
        kotlin.jvm.internal.o.h(source, "source");
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.P(source, 0, i6);
        a();
        return this;
    }

    @Override // p5.InterfaceC2741k
    public final InterfaceC2741k n(String string) {
        kotlin.jvm.internal.o.h(string, "string");
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.W(string);
        a();
        return this;
    }

    @Override // p5.InterfaceC2741k
    public final long q(P source) {
        kotlin.jvm.internal.o.h(source, "source");
        long j4 = 0;
        while (true) {
            long j6 = source.read(this.f23056b, 8192L);
            if (j6 == -1) {
                return j4;
            }
            j4 += j6;
            a();
        }
    }

    @Override // p5.N
    public final S timeout() {
        return this.f23055a.timeout();
    }

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

    @Override // p5.InterfaceC2741k
    public final InterfaceC2741k u(byte[] source) {
        kotlin.jvm.internal.o.h(source, "source");
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.O(source);
        a();
        return this;
    }

    @Override // p5.InterfaceC2741k
    public final InterfaceC2741k w(long j4) {
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        this.f23056b.R(j4);
        a();
        return this;
    }

    @Override // java.nio.channels.WritableByteChannel
    public final int write(ByteBuffer source) {
        kotlin.jvm.internal.o.h(source, "source");
        if (this.f23057c) {
            throw new IllegalStateException("closed");
        }
        int iWrite = this.f23056b.write(source);
        a();
        return iWrite;
    }

    @Override // p5.N
    public final void write(C2740j source, long j4) {
        kotlin.jvm.internal.o.h(source, "source");
        if (!this.f23057c) {
            this.f23056b.write(source, j4);
            a();
            return;
        }
        throw new IllegalStateException("closed");
    }
}
