package kr;

import com.google.android.gms.common.api.Api;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/* loaded from: classes4.dex */
public final class u implements f {

    /* renamed from: p, reason: collision with root package name */
    public final a0 f26680p;

    /* renamed from: q, reason: collision with root package name */
    public final d f26681q;

    /* renamed from: r, reason: collision with root package name */
    public boolean f26682r;

    public u(a0 source) {
        kotlin.jvm.internal.t.g(source, "source");
        this.f26680p = source;
        this.f26681q = new d();
    }

    @Override // kr.f
    public byte[] B() {
        this.f26681q.o0(this.f26680p);
        return this.f26681q.B();
    }

    @Override // kr.f
    public boolean E() {
        if (!this.f26682r) {
            return this.f26681q.E() && this.f26680p.J0(this.f26681q, 8192L) == -1;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // kr.f
    public long G0(y sink) {
        kotlin.jvm.internal.t.g(sink, "sink");
        long j10 = 0;
        while (this.f26680p.J0(this.f26681q, 8192L) != -1) {
            long c10 = this.f26681q.c();
            if (c10 > 0) {
                j10 += c10;
                sink.U(this.f26681q, c10);
            }
        }
        if (this.f26681q.X() <= 0) {
            return j10;
        }
        long X = j10 + this.f26681q.X();
        d dVar = this.f26681q;
        sink.U(dVar, dVar.X());
        return X;
    }

    @Override // kr.a0
    public long J0(d sink, long j10) {
        kotlin.jvm.internal.t.g(sink, "sink");
        if (!(j10 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j10).toString());
        }
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        if (this.f26681q.X() == 0 && this.f26680p.J0(this.f26681q, 8192L) == -1) {
            return -1L;
        }
        return this.f26681q.J0(sink, Math.min(j10, this.f26681q.X()));
    }

    @Override // kr.f
    public void L0(long j10) {
        if (!m0(j10)) {
            throw new EOFException();
        }
    }

    @Override // kr.f
    public int M0(r options) {
        kotlin.jvm.internal.t.g(options, "options");
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        while (true) {
            int e10 = lr.a.e(this.f26681q, options, true);
            if (e10 != -2) {
                if (e10 != -1) {
                    this.f26681q.skip(options.m()[e10].size());
                    return e10;
                }
            } else if (this.f26680p.J0(this.f26681q, 8192L) == -1) {
                break;
            }
        }
        return -1;
    }

    @Override // kr.f
    public String O(long j10) {
        if (!(j10 >= 0)) {
            throw new IllegalArgumentException(("limit < 0: " + j10).toString());
        }
        long j11 = j10 == Long.MAX_VALUE ? Long.MAX_VALUE : j10 + 1;
        long b10 = b((byte) 10, 0L, j11);
        if (b10 != -1) {
            return lr.a.d(this.f26681q, b10);
        }
        if (j11 < Long.MAX_VALUE && m0(j11) && this.f26681q.p(j11 - 1) == 13 && m0(1 + j11) && this.f26681q.p(j11) == 10) {
            return lr.a.d(this.f26681q, j11);
        }
        d dVar = new d();
        d dVar2 = this.f26681q;
        dVar2.f(dVar, 0L, Math.min(32, dVar2.X()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.f26681q.X(), j10) + " content=" + dVar.C().x() + (char) 8230);
    }

    @Override // kr.f
    public boolean P(long j10, g bytes) {
        kotlin.jvm.internal.t.g(bytes, "bytes");
        return e(j10, bytes, 0, bytes.size());
    }

    /* JADX WARN: Code restructure failed: missing block: B:20:0x0031, code lost:
    
        if (r0 == 0) goto L21;
     */
    /* JADX WARN: Code restructure failed: missing block: B:21:0x0034, code lost:
    
        r0 = new java.lang.StringBuilder();
        r0.append("Expected leading [0-9a-fA-F] character but was 0x");
        r1 = kotlin.text.b.a(16);
        r1 = java.lang.Integer.toString(r2, r1);
        kotlin.jvm.internal.t.f(r1, "toString(...)");
        r0.append(r1);
     */
    /* JADX WARN: Code restructure failed: missing block: B:22:0x0059, code lost:
    
        throw new java.lang.NumberFormatException(r0.toString());
     */
    @Override // kr.f
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public long P0() {
        L0(1L);
        int i10 = 0;
        while (true) {
            int i11 = i10 + 1;
            if (!m0(i11)) {
                break;
            }
            byte p10 = this.f26681q.p(i10);
            if ((p10 < 48 || p10 > 57) && ((p10 < 97 || p10 > 102) && (p10 < 65 || p10 > 70))) {
                break;
            }
            i10 = i11;
        }
        return this.f26681q.P0();
    }

    @Override // kr.f
    public void R(d sink, long j10) {
        kotlin.jvm.internal.t.g(sink, "sink");
        try {
            L0(j10);
            this.f26681q.R(sink, j10);
        } catch (EOFException e10) {
            sink.o0(this.f26681q);
            throw e10;
        }
    }

    @Override // kr.f
    public InputStream R0() {
        return new a();
    }

    @Override // kr.f
    public String Z(Charset charset) {
        kotlin.jvm.internal.t.g(charset, "charset");
        this.f26681q.o0(this.f26680p);
        return this.f26681q.Z(charset);
    }

    public long a(byte b10) {
        return b(b10, 0L, Long.MAX_VALUE);
    }

    public long b(byte b10, long j10, long j11) {
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        if (!(0 <= j10 && j10 <= j11)) {
            throw new IllegalArgumentException(("fromIndex=" + j10 + " toIndex=" + j11).toString());
        }
        while (j10 < j11) {
            long s10 = this.f26681q.s(b10, j10, j11);
            if (s10 != -1) {
                return s10;
            }
            long X = this.f26681q.X();
            if (X >= j11 || this.f26680p.J0(this.f26681q, 8192L) == -1) {
                return -1L;
            }
            j10 = Math.max(j10, X);
        }
        return -1L;
    }

    @Override // kr.f
    public long b0(g targetBytes) {
        kotlin.jvm.internal.t.g(targetBytes, "targetBytes");
        return d(targetBytes, 0L);
    }

    public long c(g bytes, long j10) {
        kotlin.jvm.internal.t.g(bytes, "bytes");
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        while (true) {
            long t10 = this.f26681q.t(bytes, j10);
            if (t10 != -1) {
                return t10;
            }
            long X = this.f26681q.X();
            if (this.f26680p.J0(this.f26681q, 8192L) == -1) {
                return -1L;
            }
            j10 = Math.max(j10, (X - bytes.size()) + 1);
        }
    }

    @Override // kr.a0, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this.f26682r) {
            return;
        }
        this.f26682r = true;
        this.f26680p.close();
        this.f26681q.a();
    }

    public long d(g targetBytes, long j10) {
        kotlin.jvm.internal.t.g(targetBytes, "targetBytes");
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        while (true) {
            long v10 = this.f26681q.v(targetBytes, j10);
            if (v10 != -1) {
                return v10;
            }
            long X = this.f26681q.X();
            if (this.f26680p.J0(this.f26681q, 8192L) == -1) {
                return -1L;
            }
            j10 = Math.max(j10, X);
        }
    }

    public boolean e(long j10, g bytes, int i10, int i11) {
        int i12;
        kotlin.jvm.internal.t.g(bytes, "bytes");
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j10 >= 0 && i10 >= 0 && i11 >= 0 && bytes.size() - i10 >= i11) {
            for (0; i12 < i11; i12 + 1) {
                long j11 = i12 + j10;
                i12 = (m0(1 + j11) && this.f26681q.p(j11) == bytes.s(i10 + i12)) ? i12 + 1 : 0;
            }
            return true;
        }
        return false;
    }

    public int f() {
        L0(4L);
        return this.f26681q.G();
    }

    public short g() {
        L0(2L);
        return this.f26681q.K();
    }

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

    @Override // kr.f, kr.e
    public d k() {
        return this.f26681q;
    }

    @Override // kr.a0
    public b0 l() {
        return this.f26680p.l();
    }

    @Override // kr.f
    public long l0(g bytes) {
        kotlin.jvm.internal.t.g(bytes, "bytes");
        return c(bytes, 0L);
    }

    @Override // kr.f
    public boolean m0(long j10) {
        if (!(j10 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j10).toString());
        }
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        while (this.f26681q.X() < j10) {
            if (this.f26680p.J0(this.f26681q, 8192L) == -1) {
                return false;
            }
        }
        return true;
    }

    @Override // kr.f
    public g q(long j10) {
        L0(j10);
        return this.f26681q.q(j10);
    }

    @Override // kr.f
    public String r0() {
        return O(Long.MAX_VALUE);
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer sink) {
        kotlin.jvm.internal.t.g(sink, "sink");
        if (this.f26681q.X() == 0 && this.f26680p.J0(this.f26681q, 8192L) == -1) {
            return -1;
        }
        return this.f26681q.read(sink);
    }

    @Override // kr.f
    public byte readByte() {
        L0(1L);
        return this.f26681q.readByte();
    }

    @Override // kr.f
    public void readFully(byte[] sink) {
        kotlin.jvm.internal.t.g(sink, "sink");
        try {
            L0(sink.length);
            this.f26681q.readFully(sink);
        } catch (EOFException e10) {
            int i10 = 0;
            while (this.f26681q.X() > 0) {
                d dVar = this.f26681q;
                int read = dVar.read(sink, i10, (int) dVar.X());
                if (read == -1) {
                    throw new AssertionError();
                }
                i10 += read;
            }
            throw e10;
        }
    }

    @Override // kr.f
    public int readInt() {
        L0(4L);
        return this.f26681q.readInt();
    }

    @Override // kr.f
    public long readLong() {
        L0(8L);
        return this.f26681q.readLong();
    }

    @Override // kr.f
    public short readShort() {
        L0(2L);
        return this.f26681q.readShort();
    }

    @Override // kr.f
    public void skip(long j10) {
        if (!(!this.f26682r)) {
            throw new IllegalStateException("closed".toString());
        }
        while (j10 > 0) {
            if (this.f26681q.X() == 0 && this.f26680p.J0(this.f26681q, 8192L) == -1) {
                throw new EOFException();
            }
            long min = Math.min(j10, this.f26681q.X());
            this.f26681q.skip(min);
            j10 -= min;
        }
    }

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

    @Override // kr.f
    public byte[] v0(long j10) {
        L0(j10);
        return this.f26681q.v0(j10);
    }

    /* loaded from: classes4.dex */
    public static final class a extends InputStream {
        a() {
        }

        @Override // java.io.InputStream
        public int available() {
            u uVar = u.this;
            if (uVar.f26682r) {
                throw new IOException("closed");
            }
            return (int) Math.min(uVar.f26681q.X(), Api.BaseClientBuilder.API_PRIORITY_OTHER);
        }

        @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            u.this.close();
        }

        @Override // java.io.InputStream
        public int read() {
            u uVar = u.this;
            if (!uVar.f26682r) {
                if (uVar.f26681q.X() == 0) {
                    u uVar2 = u.this;
                    if (uVar2.f26680p.J0(uVar2.f26681q, 8192L) == -1) {
                        return -1;
                    }
                }
                return u.this.f26681q.readByte() & 255;
            }
            throw new IOException("closed");
        }

        public String toString() {
            return u.this + ".inputStream()";
        }

        @Override // java.io.InputStream
        public int read(byte[] data, int i10, int i11) {
            kotlin.jvm.internal.t.g(data, "data");
            if (!u.this.f26682r) {
                b.b(data.length, i10, i11);
                if (u.this.f26681q.X() == 0) {
                    u uVar = u.this;
                    if (uVar.f26680p.J0(uVar.f26681q, 8192L) == -1) {
                        return -1;
                    }
                }
                return u.this.f26681q.read(data, i10, i11);
            }
            throw new IOException("closed");
        }
    }
}
