package x7;

import d5.AbstractC0844a;
import java.io.EOFException;
import java.io.InputStream;
import java.nio.ByteBuffer;

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

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

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

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

    public B(H h8) {
        P6.g.e(h8, "source");
        this.f18724q = h8;
        this.f18725r = new C1731g();
    }

    public final short A() {
        O(2L);
        return this.f18725r.m0();
    }

    @Override // x7.InterfaceC1733i
    public final boolean C(long j8) {
        C1731g c1731g;
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        do {
            c1731g = this.f18725r;
            if (c1731g.f18765r >= j8) {
                return true;
            }
        } while (this.f18724q.M(c1731g, 8192L) != -1);
        return false;
    }

    @Override // x7.InterfaceC1733i
    public final long F(C1734j c1734j) {
        P6.g.e(c1734j, "bytes");
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        long jMax = 0;
        while (true) {
            C1731g c1731g = this.f18725r;
            long jH = c1731g.H(c1734j, jMax);
            if (jH != -1) {
                return jH;
            }
            long j8 = c1731g.f18765r;
            if (this.f18724q.M(c1731g, 8192L) == -1) {
                return -1L;
            }
            jMax = Math.max(jMax, (j8 - ((long) c1734j.f18767q.length)) + 1);
        }
    }

    public final String H(long j8) {
        O(j8);
        C1731g c1731g = this.f18725r;
        c1731g.getClass();
        return c1731g.n0(j8, X6.a.f7235a);
    }

    @Override // x7.InterfaceC1733i
    public final boolean I(C1734j c1734j) {
        P6.g.e(c1734j, "bytes");
        byte[] bArr = c1734j.f18767q;
        int length = bArr.length;
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        if (length >= 0 && bArr.length >= length) {
            for (int i = 0; i < length; i++) {
                long j8 = i;
                if (C(1 + j8) && this.f18725r.A(j8) == bArr[i]) {
                }
            }
            return true;
        }
        return false;
    }

    public final String K(long j8) {
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "limit < 0: ").toString());
        }
        long j9 = j8 == Long.MAX_VALUE ? Long.MAX_VALUE : j8 + 1;
        long jB = b((byte) 10, 0L, j9);
        C1731g c1731g = this.f18725r;
        if (jB != -1) {
            return y7.a.b(c1731g, jB);
        }
        if (j9 < Long.MAX_VALUE && C(j9) && c1731g.A(j9 - 1) == 13 && C(j9 + 1) && c1731g.A(j9) == 10) {
            return y7.a.b(c1731g, j9);
        }
        C1731g c1731g2 = new C1731g();
        c1731g.r(c1731g2, 0L, Math.min(32, c1731g.f18765r));
        throw new EOFException("\\n not found: limit=" + Math.min(c1731g.f18765r, j8) + " content=" + c1731g2.f0(c1731g2.f18765r).d() + (char) 8230);
    }

    @Override // x7.InterfaceC1733i
    public final long L(InterfaceC1732h interfaceC1732h) {
        C1731g c1731g;
        long j8 = 0;
        while (true) {
            c1731g = this.f18725r;
            if (this.f18724q.M(c1731g, 8192L) == -1) {
                break;
            }
            long jN = c1731g.n();
            if (jN > 0) {
                j8 += jN;
                interfaceC1732h.X(c1731g, jN);
            }
        }
        long j9 = c1731g.f18765r;
        if (j9 <= 0) {
            return j8;
        }
        long j10 = j8 + j9;
        interfaceC1732h.X(c1731g, j9);
        return j10;
    }

    @Override // x7.H
    public final long M(C1731g c1731g, long j8) {
        P6.g.e(c1731g, "sink");
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        C1731g c1731g2 = this.f18725r;
        if (c1731g2.f18765r == 0 && this.f18724q.M(c1731g2, 8192L) == -1) {
            return -1L;
        }
        return c1731g2.M(c1731g, Math.min(j8, c1731g2.f18765r));
    }

    public final void O(long j8) {
        if (!C(j8)) {
            throw new EOFException();
        }
    }

    @Override // x7.InterfaceC1733i
    public final byte[] U() {
        C1731g c1731g = this.f18725r;
        c1731g.t0(this.f18724q);
        return c1731g.V(c1731g.f18765r);
    }

    public final boolean a() {
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        C1731g c1731g = this.f18725r;
        return c1731g.u() && this.f18724q.M(c1731g, 8192L) == -1;
    }

    public final long b(byte b8, long j8, long j9) {
        C c3;
        long j10;
        long j11;
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        long jMax = 0;
        if (0 > j9) {
            throw new IllegalArgumentException(AbstractC0844a.i(j9, "fromIndex=0 toIndex=").toString());
        }
        while (jMax < j9) {
            C1731g c1731g = this.f18725r;
            c1731g.getClass();
            long j12 = 0;
            if (0 > jMax || jMax > j9) {
                throw new IllegalArgumentException(("size=" + c1731g.f18765r + " fromIndex=" + jMax + " toIndex=" + j9).toString());
            }
            long j13 = c1731g.f18765r;
            long j14 = j9 > j13 ? j13 : j9;
            if (jMax == j14 || (c3 = c1731g.f18764q) == null) {
                j10 = -1;
                j11 = -1;
            } else if (j13 - jMax < jMax) {
                while (j13 > jMax) {
                    c3 = c3.f18733g;
                    P6.g.b(c3);
                    j13 -= (long) (c3.f18729c - c3.f18728b);
                }
                long j15 = jMax;
                while (j13 < j14) {
                    j10 = -1;
                    int iMin = (int) Math.min(c3.f18729c, (((long) c3.f18728b) + j14) - j13);
                    for (int i = (int) ((((long) c3.f18728b) + j15) - j13); i < iMin; i++) {
                        if (c3.f18727a[i] == b8) {
                            j11 = ((long) (i - c3.f18728b)) + j13;
                            break;
                        }
                    }
                    j15 = j13 + ((long) (c3.f18729c - c3.f18728b));
                    c3 = c3.f18732f;
                    P6.g.b(c3);
                    j13 = j15;
                }
                j10 = -1;
                j11 = -1;
            } else {
                j10 = -1;
                while (true) {
                    long j16 = ((long) (c3.f18729c - c3.f18728b)) + j12;
                    if (j16 > jMax) {
                        break;
                    }
                    c3 = c3.f18732f;
                    P6.g.b(c3);
                    j12 = j16;
                }
                long j17 = jMax;
                while (j12 < j14) {
                    long j18 = j17;
                    int iMin2 = (int) Math.min(c3.f18729c, (((long) c3.f18728b) + j14) - j12);
                    for (int i5 = (int) ((((long) c3.f18728b) + j18) - j12); i5 < iMin2; i5++) {
                        if (c3.f18727a[i5] == b8) {
                            j11 = ((long) (i5 - c3.f18728b)) + j12;
                            break;
                        }
                    }
                    j17 = ((long) (c3.f18729c - c3.f18728b)) + j12;
                    c3 = c3.f18732f;
                    P6.g.b(c3);
                    j12 = j17;
                }
                j11 = -1;
            }
            if (j11 != j10) {
                return j11;
            }
            long j19 = c1731g.f18765r;
            if (j19 >= j9 || this.f18724q.M(c1731g, 8192L) == j10) {
                return j10;
            }
            jMax = Math.max(jMax, j19);
        }
        return -1L;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public final void close() {
        if (this.f18726s) {
            return;
        }
        this.f18726s = true;
        this.f18724q.close();
        this.f18725r.m();
    }

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

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

    public final byte g() {
        O(1L);
        return this.f18725r.Q();
    }

    @Override // x7.InterfaceC1733i
    public final long i(C1734j c1734j) {
        P6.g.e(c1734j, "targetBytes");
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        long jMax = 0;
        while (true) {
            C1731g c1731g = this.f18725r;
            long jK = c1731g.K(c1734j, jMax);
            if (jK != -1) {
                return jK;
            }
            long j8 = c1731g.f18765r;
            if (this.f18724q.M(c1731g, 8192L) == -1) {
                return -1L;
            }
            jMax = Math.max(jMax, j8);
        }
    }

    @Override // x7.InterfaceC1733i
    public final InputStream i0() {
        return new C1730f(this, 1);
    }

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

    public final C1734j j(long j8) {
        O(j8);
        return this.f18725r.f0(j8);
    }

    public final int m() {
        O(4L);
        return this.f18725r.k0();
    }

    public final int n() {
        O(4L);
        int iK0 = this.f18725r.k0();
        return ((iK0 & 255) << 24) | (((-16777216) & iK0) >>> 24) | ((16711680 & iK0) >>> 8) | ((65280 & iK0) << 8);
    }

    public final long r() throws EOFException {
        char c3;
        char c8;
        char c9;
        char c10;
        long jK0;
        O(8L);
        C1731g c1731g = this.f18725r;
        if (c1731g.f18765r < 8) {
            throw new EOFException();
        }
        C c11 = c1731g.f18764q;
        P6.g.b(c11);
        int i = c11.f18728b;
        int i5 = c11.f18729c;
        if (i5 - i < 8) {
            jK0 = ((((long) c1731g.k0()) & 4294967295L) << 32) | (4294967295L & ((long) c1731g.k0()));
            c9 = '(';
            c10 = '8';
            c3 = '\b';
            c8 = 24;
        } else {
            byte[] bArr = c11.f18727a;
            c3 = '\b';
            c8 = 24;
            c9 = '(';
            c10 = '8';
            int i8 = i + 7;
            long j8 = ((((long) bArr[i]) & 255) << 56) | ((((long) bArr[i + 1]) & 255) << 48) | ((((long) bArr[i + 2]) & 255) << 40) | ((((long) bArr[i + 3]) & 255) << 32) | ((((long) bArr[i + 4]) & 255) << 24) | ((((long) bArr[i + 5]) & 255) << 16) | ((((long) bArr[i + 6]) & 255) << 8);
            int i9 = i + 8;
            long j9 = j8 | (((long) bArr[i8]) & 255);
            c1731g.f18765r -= 8;
            if (i9 == i5) {
                c1731g.f18764q = c11.a();
                D.a(c11);
            } else {
                c11.f18728b = i9;
            }
            jK0 = j9;
        }
        return ((jK0 & 255) << c10) | (((-72057594037927936L) & jK0) >>> c10) | ((71776119061217280L & jK0) >>> c9) | ((280375465082880L & jK0) >>> c8) | ((1095216660480L & jK0) >>> c3) | ((4278190080L & jK0) << c3) | ((16711680 & jK0) << c8) | ((65280 & jK0) << c9);
    }

    @Override // java.nio.channels.ReadableByteChannel
    public final int read(ByteBuffer byteBuffer) {
        P6.g.e(byteBuffer, "sink");
        C1731g c1731g = this.f18725r;
        if (c1731g.f18765r == 0 && this.f18724q.M(c1731g, 8192L) == -1) {
            return -1;
        }
        return c1731g.read(byteBuffer);
    }

    @Override // x7.InterfaceC1733i
    public final int t(w wVar) throws EOFException {
        P6.g.e(wVar, "options");
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        while (true) {
            C1731g c1731g = this.f18725r;
            int iC = y7.a.c(c1731g, wVar, true);
            if (iC != -2) {
                if (iC != -1) {
                    c1731g.y(wVar.f18795q[iC].c());
                    return iC;
                }
            } else if (this.f18724q.M(c1731g, 8192L) == -1) {
                break;
            }
        }
        return -1;
    }

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

    public final short u() {
        O(2L);
        return this.f18725r.l0();
    }

    @Override // x7.InterfaceC1733i
    public final void y(long j8) {
        if (this.f18726s) {
            throw new IllegalStateException("closed");
        }
        while (j8 > 0) {
            C1731g c1731g = this.f18725r;
            if (c1731g.f18765r == 0 && this.f18724q.M(c1731g, 8192L) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j8, c1731g.f18765r);
            c1731g.y(jMin);
            j8 -= jMin;
        }
    }
}
