package p5;

import java.io.EOFException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/* JADX INFO: loaded from: classes3.dex */
public final class J implements InterfaceC2742l {

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

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

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

    public J(P source) {
        kotlin.jvm.internal.o.h(source, "source");
        this.f23058a = source;
        this.f23059b = new C2740j();
    }

    public final void A(long j4) {
        if (!p(j4)) {
            throw new EOFException();
        }
    }

    public final void B(long j4) {
        if (this.f23060c) {
            throw new IllegalStateException("closed");
        }
        while (j4 > 0) {
            C2740j c2740j = this.f23059b;
            if (c2740j.f23100b == 0 && this.f23058a.read(c2740j, 8192L) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j4, c2740j.f23100b);
            c2740j.J(jMin);
            j4 -= jMin;
        }
    }

    public final boolean a() {
        if (this.f23060c) {
            throw new IllegalStateException("closed");
        }
        C2740j c2740j = this.f23059b;
        return c2740j.d() && this.f23058a.read(c2740j, 8192L) == -1;
    }

    public final byte b() {
        A(1L);
        return this.f23059b.y();
    }

    public final C2743m c(long j4) {
        A(j4);
        return this.f23059b.A(j4);
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public final void close() {
        if (this.f23060c) {
            return;
        }
        this.f23060c = true;
        this.f23058a.close();
        this.f23059b.a();
    }

    public final int d() {
        A(4L);
        return this.f23059b.D();
    }

    @Override // p5.InterfaceC2742l
    public final C2740j e() {
        return this.f23059b;
    }

    public final int f() {
        A(4L);
        int iD = this.f23059b.D();
        return ((iD & 255) << 24) | (((-16777216) & iD) >>> 24) | ((16711680 & iD) >>> 8) | ((65280 & iD) << 8);
    }

    public final long g() throws EOFException {
        long jD;
        A(8L);
        C2740j c2740j = this.f23059b;
        if (c2740j.f23100b < 8) {
            throw new EOFException();
        }
        K k6 = c2740j.f23099a;
        kotlin.jvm.internal.o.e(k6);
        int i6 = k6.f23062b;
        int i7 = k6.f23063c;
        if (i7 - i6 < 8) {
            jD = ((((long) c2740j.D()) & 4294967295L) << 32) | (4294967295L & ((long) c2740j.D()));
        } else {
            byte[] bArr = k6.f23061a;
            int i8 = i6 + 7;
            long j4 = ((((long) bArr[i6 + 1]) & 255) << 48) | ((((long) bArr[i6]) & 255) << 56) | ((((long) bArr[i6 + 2]) & 255) << 40) | ((((long) bArr[i6 + 3]) & 255) << 32) | ((((long) bArr[i6 + 4]) & 255) << 24) | ((((long) bArr[i6 + 5]) & 255) << 16) | ((((long) bArr[i6 + 6]) & 255) << 8);
            int i9 = i6 + 8;
            long j6 = j4 | (((long) bArr[i8]) & 255);
            c2740j.f23100b -= 8;
            if (i9 == i7) {
                c2740j.f23099a = k6.a();
                L.a(k6);
            } else {
                k6.f23062b = i9;
            }
            jD = j6;
        }
        return ((jD & 255) << 56) | (((-72057594037927936L) & jD) >>> 56) | ((71776119061217280L & jD) >>> 40) | ((280375465082880L & jD) >>> 24) | ((1095216660480L & jD) >>> 8) | ((4278190080L & jD) << 8) | ((16711680 & jD) << 24) | ((65280 & jD) << 40);
    }

    public final short i() {
        A(2L);
        return this.f23059b.E();
    }

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

    @Override // p5.InterfaceC2742l
    public final long k(byte b5, long j4, long j6) {
        if (this.f23060c) {
            throw new IllegalStateException("closed");
        }
        if (0 > j4 || j4 > j6) {
            throw new IllegalArgumentException(("fromIndex=" + j4 + " toIndex=" + j6).toString());
        }
        long jMax = j4;
        while (jMax < j6) {
            C2740j c2740j = this.f23059b;
            byte b6 = b5;
            long j7 = j6;
            long jK = c2740j.k(b6, jMax, j7);
            if (jK != -1) {
                return jK;
            }
            long j8 = c2740j.f23100b;
            if (j8 >= j7 || this.f23058a.read(c2740j, 8192L) == -1) {
                break;
            }
            jMax = Math.max(jMax, j8);
            b5 = b6;
            j6 = j7;
        }
        return -1L;
    }

    @Override // p5.InterfaceC2742l
    public final int m(F options) throws EOFException {
        kotlin.jvm.internal.o.h(options, "options");
        if (this.f23060c) {
            throw new IllegalStateException("closed");
        }
        while (true) {
            C2740j c2740j = this.f23059b;
            int iB = q5.a.b(c2740j, options, true);
            if (iB != -2) {
                if (iB != -1) {
                    c2740j.J(options.f23045a[iB].d());
                    return iB;
                }
            } else if (this.f23058a.read(c2740j, 8192L) == -1) {
                break;
            }
        }
        return -1;
    }

    @Override // p5.InterfaceC2742l
    public final String o(Charset charset) {
        C2740j c2740j = this.f23059b;
        c2740j.q(this.f23058a);
        return c2740j.o(charset);
    }

    @Override // p5.InterfaceC2742l
    public final boolean p(long j4) {
        C2740j c2740j;
        if (j4 < 0) {
            throw new IllegalArgumentException(A0.a.m("byteCount < 0: ", j4).toString());
        }
        if (this.f23060c) {
            throw new IllegalStateException("closed");
        }
        do {
            c2740j = this.f23059b;
            if (c2740j.f23100b >= j4) {
                return true;
            }
        } while (this.f23058a.read(c2740j, 8192L) != -1);
        return false;
    }

    @Override // p5.InterfaceC2742l
    public final J peek() {
        return AbstractC2732b.c(new H(this));
    }

    @Override // p5.InterfaceC2742l
    public final boolean r(long j4, C2743m bytes) {
        kotlin.jvm.internal.o.h(bytes, "bytes");
        int iD = bytes.d();
        if (this.f23060c) {
            throw new IllegalStateException("closed");
        }
        if (j4 >= 0 && iD >= 0 && bytes.d() >= iD) {
            for (int i6 = 0; i6 < iD; i6++) {
                long j6 = ((long) i6) + j4;
                if (p(1 + j6) && this.f23059b.f(j6) == bytes.i(i6)) {
                }
            }
            return true;
        }
        return false;
    }

    @Override // java.nio.channels.ReadableByteChannel
    public final int read(ByteBuffer sink) {
        kotlin.jvm.internal.o.h(sink, "sink");
        C2740j c2740j = this.f23059b;
        if (c2740j.f23100b == 0 && this.f23058a.read(c2740j, 8192L) == -1) {
            return -1;
        }
        return c2740j.read(sink);
    }

    @Override // p5.InterfaceC2742l
    public final String s() {
        return z(Long.MAX_VALUE);
    }

    public final short t() {
        A(2L);
        return this.f23059b.F();
    }

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

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

    @Override // p5.InterfaceC2742l
    public final long v(I i6) {
        C2740j c2740j;
        long j4 = 0;
        while (true) {
            c2740j = this.f23059b;
            if (this.f23058a.read(c2740j, 8192L) == -1) {
                break;
            }
            long jB = c2740j.b();
            if (jB > 0) {
                j4 += jB;
                i6.write(c2740j, jB);
            }
        }
        long j6 = c2740j.f23100b;
        if (j6 <= 0) {
            return j4;
        }
        long j7 = j4 + j6;
        i6.write(c2740j, j6);
        return j7;
    }

    @Override // p5.InterfaceC2742l
    public final InputStream x() {
        return new C2739i(this, 1);
    }

    public final String y(long j4) {
        A(j4);
        C2740j c2740j = this.f23059b;
        c2740j.getClass();
        return c2740j.G(j4, S4.a.f6257a);
    }

    public final String z(long j4) throws EOFException {
        if (j4 < 0) {
            throw new IllegalArgumentException(A0.a.m("limit < 0: ", j4).toString());
        }
        long j6 = j4 == Long.MAX_VALUE ? Long.MAX_VALUE : j4 + 1;
        long jK = k((byte) 10, 0L, j6);
        C2740j c2740j = this.f23059b;
        if (jK != -1) {
            return q5.a.a(c2740j, jK);
        }
        if (j6 < Long.MAX_VALUE && p(j6) && c2740j.f(j6 - 1) == 13 && p(j6 + 1) && c2740j.f(j6) == 10) {
            return q5.a.a(c2740j, j6);
        }
        C2740j c2740j2 = new C2740j();
        c2740j.c(c2740j2, 0L, Math.min(32, c2740j.f23100b));
        throw new EOFException("\\n not found: limit=" + Math.min(c2740j.f23100b, j4) + " content=" + c2740j2.A(c2740j2.f23100b).e() + (char) 8230);
    }

    @Override // p5.P
    public final long read(C2740j sink, long j4) {
        kotlin.jvm.internal.o.h(sink, "sink");
        if (j4 >= 0) {
            if (!this.f23060c) {
                C2740j c2740j = this.f23059b;
                if (c2740j.f23100b == 0) {
                    if (j4 == 0) {
                        return 0L;
                    }
                    if (this.f23058a.read(c2740j, 8192L) == -1) {
                        return -1L;
                    }
                }
                return c2740j.read(sink, Math.min(j4, c2740j.f23100b));
            }
            throw new IllegalStateException("closed");
        }
        throw new IllegalArgumentException(A0.a.m("byteCount < 0: ", j4).toString());
    }
}
