package okio;

import androidx.core.location.LocationRequestCompat;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/* JADX INFO: loaded from: classes2.dex */
public final class x implements h {

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

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

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

    public x(E source) {
        kotlin.jvm.internal.l.e(source, "source");
        this.f6068r = source;
        this.f6066b = new C0688f();
    }

    @Override // okio.h
    public i B() {
        this.f6066b.writeAll(this.f6068r);
        return this.f6066b.B();
    }

    @Override // okio.h
    public boolean E(long j2) {
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        while (this.f6066b.G() < j2) {
            if (this.f6068r.read(this.f6066b, 8192) == -1) {
                return false;
            }
        }
        return true;
    }

    @Override // okio.h
    public String J() {
        return r(LocationRequestCompat.PASSIVE_INTERVAL);
    }

    @Override // okio.h
    public byte[] M(long j2) throws EOFException {
        W(j2);
        return this.f6066b.M(j2);
    }

    @Override // okio.h
    public h T() {
        return q.d(new v(this));
    }

    @Override // okio.h
    public void W(long j2) throws EOFException {
        if (!E(j2)) {
            throw new EOFException();
        }
    }

    @Override // okio.h
    public long Z() throws EOFException {
        byte bJ;
        W(1L);
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (!E(i3)) {
                break;
            }
            bJ = this.f6066b.j(i2);
            if ((bJ < ((byte) 48) || bJ > ((byte) 57)) && ((bJ < ((byte) 97) || bJ > ((byte) 102)) && (bJ < ((byte) 65) || bJ > ((byte) 70)))) {
                break;
            }
            i2 = i3;
        }
        if (i2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9a-fA-F] character but was 0x");
            String string = Integer.toString(bJ, E1.a.a(E1.a.a(16)));
            kotlin.jvm.internal.l.d(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.f6066b.Z();
    }

    public long a(byte b2) {
        return b(b2, 0L, LocationRequestCompat.PASSIVE_INTERVAL);
    }

    public long b(byte b2, long j2, long j3) {
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        if (!(0 <= j2 && j3 >= j2)) {
            throw new IllegalArgumentException(("fromIndex=" + j2 + " toIndex=" + j3).toString());
        }
        while (j2 < j3) {
            long jL = this.f6066b.l(b2, j2, j3);
            if (jL != -1) {
                return jL;
            }
            long jG = this.f6066b.G();
            if (jG >= j3 || this.f6068r.read(this.f6066b, 8192) == -1) {
                return -1L;
            }
            j2 = Math.max(j2, jG);
        }
        return -1L;
    }

    @Override // okio.h
    public InputStream b0() {
        return new a();
    }

    @Override // okio.h
    public i c(long j2) throws EOFException {
        W(j2);
        return this.f6066b.c(j2);
    }

    @Override // okio.E, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws EOFException {
        if (this.f6067g) {
            return;
        }
        this.f6067g = true;
        this.f6068r.close();
        this.f6066b.a();
    }

    public boolean d(long j2, i bytes, int i2, int i3) {
        kotlin.jvm.internal.l.e(bytes, "bytes");
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        if (j2 < 0 || i2 < 0 || i3 < 0 || bytes.s() - i2 < i3) {
            return false;
        }
        for (int i4 = 0; i4 < i3; i4++) {
            long j3 = ((long) i4) + j2;
            if (!E(1 + j3) || this.f6066b.j(j3) != bytes.d(i2 + i4)) {
                return false;
            }
        }
        return true;
    }

    @Override // okio.h
    public int d0(t options) throws EOFException {
        kotlin.jvm.internal.l.e(options, "options");
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        while (true) {
            int iD = I1.a.d(this.f6066b, options, true);
            if (iD != -2) {
                if (iD != -1) {
                    this.f6066b.skip(options.d()[iD].s());
                    return iD;
                }
            } else if (this.f6068r.read(this.f6066b, 8192) == -1) {
                break;
            }
        }
        return -1;
    }

    public int e() throws EOFException {
        W(4L);
        return this.f6066b.w();
    }

    public short f() throws EOFException {
        W(2L);
        return this.f6066b.z();
    }

    @Override // okio.h
    public byte[] g() {
        this.f6066b.writeAll(this.f6068r);
        return this.f6066b.g();
    }

    @Override // okio.h, okio.g
    public C0688f getBuffer() {
        return this.f6066b;
    }

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

    @Override // okio.h
    public boolean k() {
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        return this.f6066b.k() && this.f6068r.read(this.f6066b, (long) 8192) == -1;
    }

    @Override // okio.h
    public void o(C0688f sink, long j2) throws EOFException {
        kotlin.jvm.internal.l.e(sink, "sink");
        try {
            W(j2);
            this.f6066b.o(sink, j2);
        } catch (EOFException e2) {
            sink.writeAll(this.f6066b);
            throw e2;
        }
    }

    @Override // okio.h
    public long p() throws EOFException {
        byte bJ;
        W(1L);
        long j2 = 0;
        while (true) {
            long j3 = j2 + 1;
            if (!E(j3)) {
                break;
            }
            bJ = this.f6066b.j(j2);
            if ((bJ < ((byte) 48) || bJ > ((byte) 57)) && !(j2 == 0 && bJ == ((byte) 45))) {
                break;
            }
            j2 = j3;
        }
        if (j2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9] or '-' character but was 0x");
            String string = Integer.toString(bJ, E1.a.a(E1.a.a(16)));
            kotlin.jvm.internal.l.d(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.f6066b.p();
    }

    @Override // okio.h
    public String r(long j2) throws EOFException {
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("limit < 0: " + j2).toString());
        }
        long j3 = j2 == LocationRequestCompat.PASSIVE_INTERVAL ? Long.MAX_VALUE : j2 + 1;
        byte b2 = (byte) 10;
        long jB = b(b2, 0L, j3);
        if (jB != -1) {
            return I1.a.c(this.f6066b, jB);
        }
        if (j3 < LocationRequestCompat.PASSIVE_INTERVAL && E(j3) && this.f6066b.j(j3 - 1) == ((byte) 13) && E(1 + j3) && this.f6066b.j(j3) == b2) {
            return I1.a.c(this.f6066b, j3);
        }
        C0688f c0688f = new C0688f();
        C0688f c0688f2 = this.f6066b;
        c0688f2.f(c0688f, 0L, Math.min(32, c0688f2.G()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.f6066b.G(), j2) + " content=" + c0688f.B().i() + "…");
    }

    @Override // okio.E
    public long read(C0688f sink, long j2) {
        kotlin.jvm.internal.l.e(sink, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        if (this.f6066b.G() == 0 && this.f6068r.read(this.f6066b, 8192) == -1) {
            return -1L;
        }
        return this.f6066b.read(sink, Math.min(j2, this.f6066b.G()));
    }

    @Override // okio.h
    public byte readByte() throws EOFException {
        W(1L);
        return this.f6066b.readByte();
    }

    @Override // okio.h
    public void readFully(byte[] sink) throws EOFException {
        kotlin.jvm.internal.l.e(sink, "sink");
        try {
            W(sink.length);
            this.f6066b.readFully(sink);
        } catch (EOFException e2) {
            int i2 = 0;
            while (this.f6066b.G() > 0) {
                C0688f c0688f = this.f6066b;
                int i3 = c0688f.read(sink, i2, (int) c0688f.G());
                if (i3 == -1) {
                    throw new AssertionError();
                }
                i2 += i3;
            }
            throw e2;
        }
    }

    @Override // okio.h
    public int readInt() throws EOFException {
        W(4L);
        return this.f6066b.readInt();
    }

    @Override // okio.h
    public long readLong() throws EOFException {
        W(8L);
        return this.f6066b.readLong();
    }

    @Override // okio.h
    public short readShort() throws EOFException {
        W(2L);
        return this.f6066b.readShort();
    }

    @Override // okio.h
    public void skip(long j2) throws EOFException {
        if (this.f6067g) {
            throw new IllegalStateException("closed");
        }
        while (j2 > 0) {
            if (this.f6066b.G() == 0 && this.f6068r.read(this.f6066b, 8192) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j2, this.f6066b.G());
            this.f6066b.skip(jMin);
            j2 -= jMin;
        }
    }

    @Override // okio.E
    public F timeout() {
        return this.f6068r.timeout();
    }

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

    @Override // okio.h
    public boolean x(long j2, i bytes) {
        kotlin.jvm.internal.l.e(bytes, "bytes");
        return d(j2, bytes, 0, bytes.s());
    }

    @Override // okio.h
    public String y(Charset charset) {
        kotlin.jvm.internal.l.e(charset, "charset");
        this.f6066b.writeAll(this.f6068r);
        return this.f6066b.y(charset);
    }

    public static final class a extends InputStream {
        a() {
        }

        @Override // java.io.InputStream
        public int available() throws IOException {
            x xVar = x.this;
            if (xVar.f6067g) {
                throw new IOException("closed");
            }
            return (int) Math.min(xVar.f6066b.G(), Integer.MAX_VALUE);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            x xVar = x.this;
            if (xVar.f6067g) {
                throw new IOException("closed");
            }
            if (xVar.f6066b.G() == 0) {
                x xVar2 = x.this;
                if (xVar2.f6068r.read(xVar2.f6066b, 8192) == -1) {
                    return -1;
                }
            }
            return x.this.f6066b.readByte() & 255;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] data, int i2, int i3) throws IOException {
            kotlin.jvm.internal.l.e(data, "data");
            if (!x.this.f6067g) {
                AbstractC0685c.b(data.length, i2, i3);
                if (x.this.f6066b.G() == 0) {
                    x xVar = x.this;
                    if (xVar.f6068r.read(xVar.f6066b, 8192) == -1) {
                        return -1;
                    }
                }
                return x.this.f6066b.read(data, i2, i3);
            }
            throw new IOException("closed");
        }
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer sink) {
        kotlin.jvm.internal.l.e(sink, "sink");
        if (this.f6066b.G() == 0 && this.f6068r.read(this.f6066b, 8192) == -1) {
            return -1;
        }
        return this.f6066b.read(sink);
    }
}
