package d;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

/* JADX INFO: compiled from: RealBufferedSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class u implements h {

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final a0 f1604d;

    public u(a0 a0Var) {
        c.q.d.j.c(a0Var, "source");
        this.f1604d = a0Var;
        this.f1602b = new f();
    }

    @Override // d.h
    public byte[] A(long j) throws EOFException {
        u(j);
        return this.f1602b.A(j);
    }

    @Override // d.h
    public long B() throws EOFException {
        byte bK;
        u(1L);
        int i = 0;
        while (true) {
            int i2 = i + 1;
            if (!p(i2)) {
                break;
            }
            bK = this.f1602b.K(i);
            if ((bK < ((byte) 48) || bK > ((byte) 57)) && ((bK < ((byte) 97) || bK > ((byte) a.b.d.a.j.AppCompatTheme_textAppearanceSearchResultTitle)) && (bK < ((byte) 65) || bK > ((byte) 70)))) {
                break;
            }
            i = i2;
        }
        if (i == 0) {
            c.q.d.t tVar = c.q.d.t.f1451a;
            String str = String.format("Expected leading [0-9a-fA-F] character but was %#x", Arrays.copyOf(new Object[]{Byte.valueOf(bK)}, 1));
            c.q.d.j.b(str, "java.lang.String.format(format, *args)");
            throw new NumberFormatException(str);
        }
        return this.f1602b.B();
    }

    @Override // d.h
    public String C(Charset charset) throws IOException {
        c.q.d.j.c(charset, "charset");
        this.f1602b.f(this.f1604d);
        return this.f1602b.C(charset);
    }

    @Override // d.h
    public InputStream D() {
        return new a();
    }

    @Override // d.h
    public int E(q qVar) throws EOFException {
        c.q.d.j.c(qVar, "options");
        if (!(!this.f1603c)) {
            throw new IllegalStateException("closed".toString());
        }
        do {
            int iY = this.f1602b.Y(qVar, true);
            if (iY != -2) {
                if (iY == -1) {
                    return -1;
                }
                this.f1602b.l(qVar.c()[iY].s());
                return iY;
            }
        } while (this.f1604d.read(this.f1602b, 8192) != -1);
        return -1;
    }

    public long F(byte b2, long j, long j2) {
        if (!(!this.f1603c)) {
            throw new IllegalStateException("closed".toString());
        }
        if (!(0 <= j && j2 >= j)) {
            throw new IllegalArgumentException(("fromIndex=" + j + " toIndex=" + j2).toString());
        }
        while (j < j2) {
            long jL = this.f1602b.L(b2, j, j2);
            if (jL != -1) {
                return jL;
            }
            long jB0 = this.f1602b.b0();
            if (jB0 >= j2 || this.f1604d.read(this.f1602b, 8192) == -1) {
                break;
            }
            j = Math.max(j, jB0);
        }
        return -1L;
    }

    public int G() throws EOFException {
        u(4L);
        return this.f1602b.S();
    }

    public short H() throws EOFException {
        u(2L);
        return this.f1602b.T();
    }

    @Override // d.h, d.g
    public f a() {
        return this.f1602b;
    }

    @Override // d.a0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f1603c) {
            return;
        }
        this.f1603c = true;
        this.f1604d.close();
        this.f1602b.clear();
    }

    @Override // d.h
    public void e(f fVar, long j) throws IOException {
        c.q.d.j.c(fVar, "sink");
        try {
            u(j);
            this.f1602b.e(fVar, j);
        } catch (EOFException e2) {
            fVar.f(this.f1602b);
            throw e2;
        }
    }

    @Override // d.h
    public long i() throws EOFException {
        byte bK;
        u(1L);
        long j = 0;
        while (true) {
            long j2 = j + 1;
            if (!p(j2)) {
                break;
            }
            bK = this.f1602b.K(j);
            if ((bK < ((byte) 48) || bK > ((byte) 57)) && !(j == 0 && bK == ((byte) 45))) {
                break;
            }
            j = j2;
        }
        if (j == 0) {
            c.q.d.t tVar = c.q.d.t.f1451a;
            String str = String.format("Expected leading [0-9] or '-' character but was %#x", Arrays.copyOf(new Object[]{Byte.valueOf(bK)}, 1));
            c.q.d.j.b(str, "java.lang.String.format(format, *args)");
            throw new NumberFormatException(str);
        }
        return this.f1602b.i();
    }

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

    @Override // d.h
    public i j(long j) throws EOFException {
        u(j);
        return this.f1602b.j(j);
    }

    @Override // d.h
    public String k(long j) throws EOFException {
        if (!(j >= 0)) {
            throw new IllegalArgumentException(("limit < 0: " + j).toString());
        }
        long j2 = j == Long.MAX_VALUE ? Long.MAX_VALUE : j + 1;
        byte b2 = (byte) 10;
        long jF = F(b2, 0L, j2);
        if (jF != -1) {
            return this.f1602b.X(jF);
        }
        if (j2 < Long.MAX_VALUE && p(j2) && this.f1602b.K(j2 - 1) == ((byte) 13) && p(1 + j2) && this.f1602b.K(j2) == b2) {
            return this.f1602b.X(j2);
        }
        f fVar = new f();
        f fVar2 = this.f1602b;
        fVar2.H(fVar, 0L, Math.min(32, fVar2.b0()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.f1602b.b0(), j) + " content=" + fVar.R().i() + "…");
    }

    @Override // d.h
    public void l(long j) throws EOFException {
        if (!(!this.f1603c)) {
            throw new IllegalStateException("closed".toString());
        }
        while (j > 0) {
            if (this.f1602b.b0() == 0 && this.f1604d.read(this.f1602b, 8192) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j, this.f1602b.b0());
            this.f1602b.l(jMin);
            j -= jMin;
        }
    }

    @Override // d.h
    public long m(y yVar) throws IOException {
        c.q.d.j.c(yVar, "sink");
        long j = 0;
        while (this.f1604d.read(this.f1602b, 8192) != -1) {
            long jG = this.f1602b.G();
            if (jG > 0) {
                j += jG;
                yVar.write(this.f1602b, jG);
            }
        }
        if (this.f1602b.b0() <= 0) {
            return j;
        }
        long jB0 = j + this.f1602b.b0();
        f fVar = this.f1602b;
        yVar.write(fVar, fVar.b0());
        return jB0;
    }

    @Override // d.h
    public boolean p(long j) {
        if (!(j >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j).toString());
        }
        if (!(!this.f1603c)) {
            throw new IllegalStateException("closed".toString());
        }
        while (this.f1602b.b0() < j) {
            if (this.f1604d.read(this.f1602b, 8192) == -1) {
                return false;
            }
        }
        return true;
    }

    @Override // d.h
    public h r() {
        return p.d(new s(this));
    }

    @Override // d.a0
    public long read(f fVar, long j) {
        c.q.d.j.c(fVar, "sink");
        if (!(j >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j).toString());
        }
        if (!(!this.f1603c)) {
            throw new IllegalStateException("closed".toString());
        }
        if (this.f1602b.b0() == 0 && this.f1604d.read(this.f1602b, 8192) == -1) {
            return -1L;
        }
        return this.f1602b.read(fVar, Math.min(j, this.f1602b.b0()));
    }

    @Override // d.h
    public byte readByte() throws EOFException {
        u(1L);
        return this.f1602b.readByte();
    }

    @Override // d.h
    public void readFully(byte[] bArr) throws EOFException {
        c.q.d.j.c(bArr, "sink");
        try {
            u(bArr.length);
            this.f1602b.readFully(bArr);
        } catch (EOFException e2) {
            int i = 0;
            while (this.f1602b.b0() > 0) {
                f fVar = this.f1602b;
                int iP = fVar.P(bArr, i, (int) fVar.b0());
                if (iP == -1) {
                    throw new AssertionError();
                }
                i += iP;
            }
            throw e2;
        }
    }

    @Override // d.h
    public int readInt() throws EOFException {
        u(4L);
        return this.f1602b.readInt();
    }

    @Override // d.h
    public long readLong() throws EOFException {
        u(8L);
        return this.f1602b.readLong();
    }

    @Override // d.h
    public short readShort() throws EOFException {
        u(2L);
        return this.f1602b.readShort();
    }

    @Override // d.h
    public String s() {
        return k(Long.MAX_VALUE);
    }

    @Override // d.h
    public byte[] t() throws IOException {
        this.f1602b.f(this.f1604d);
        return this.f1602b.t();
    }

    @Override // d.a0
    public b0 timeout() {
        return this.f1604d.timeout();
    }

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

    @Override // d.h
    public void u(long j) throws EOFException {
        if (!p(j)) {
            throw new EOFException();
        }
    }

    public long v(byte b2) {
        return F(b2, 0L, Long.MAX_VALUE);
    }

    @Override // d.h
    public boolean y() {
        if (!this.f1603c) {
            return this.f1602b.y() && this.f1604d.read(this.f1602b, (long) 8192) == -1;
        }
        throw new IllegalStateException("closed".toString());
    }

    /* JADX INFO: compiled from: RealBufferedSource.kt */
    public static final class a extends InputStream {
        a() {
        }

        @Override // java.io.InputStream
        public int available() throws IOException {
            u uVar = u.this;
            if (uVar.f1603c) {
                throw new IOException("closed");
            }
            return (int) Math.min(uVar.f1602b.b0(), Integer.MAX_VALUE);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            u uVar = u.this;
            if (uVar.f1603c) {
                throw new IOException("closed");
            }
            if (uVar.f1602b.b0() == 0) {
                u uVar2 = u.this;
                if (uVar2.f1604d.read(uVar2.f1602b, 8192) == -1) {
                    return -1;
                }
            }
            return u.this.f1602b.readByte() & 255;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) throws IOException {
            c.q.d.j.c(bArr, "data");
            if (!u.this.f1603c) {
                c.b(bArr.length, i, i2);
                if (u.this.f1602b.b0() == 0) {
                    u uVar = u.this;
                    if (uVar.f1604d.read(uVar.f1602b, 8192) == -1) {
                        return -1;
                    }
                }
                return u.this.f1602b.P(bArr, i, i2);
            }
            throw new IOException("closed");
        }
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer byteBuffer) {
        c.q.d.j.c(byteBuffer, "sink");
        if (this.f1602b.b0() == 0 && this.f1604d.read(this.f1602b, 8192) == -1) {
            return -1;
        }
        return this.f1602b.read(byteBuffer);
    }
}
