package h;

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

/* JADX INFO: compiled from: RealBufferedSource.java */
/* JADX INFO: loaded from: classes.dex */
final class n implements e {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final c f4201b = new c();

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

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

    n(s sVar) {
        if (sVar == null) {
            throw new NullPointerException("source == null");
        }
        this.f4202f = sVar;
    }

    @Override // h.e
    public byte[] A(long j2) throws IOException {
        X(j2);
        return this.f4201b.A(j2);
    }

    @Override // h.e
    public void G(c cVar, long j2) throws IOException {
        try {
            X(j2);
            this.f4201b.G(cVar, j2);
        } catch (EOFException e2) {
            cVar.b(this.f4201b);
            throw e2;
        }
    }

    @Override // h.e
    public short I() throws IOException {
        X(2L);
        return this.f4201b.I();
    }

    @Override // h.e
    public long L() throws IOException {
        byte bK;
        X(1L);
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (!l(i3)) {
                break;
            }
            bK = this.f4201b.k(i2);
            if ((bK < 48 || bK > 57) && !(i2 == 0 && bK == 45)) {
                break;
            }
            i2 = i3;
        }
        if (i2 == 0) {
            throw new NumberFormatException(String.format("Expected leading [0-9] or '-' character but was %#x", Byte.valueOf(bK)));
        }
        return this.f4201b.L();
    }

    @Override // h.e
    public long O(r rVar) throws IOException {
        if (rVar == null) {
            throw new IllegalArgumentException("sink == null");
        }
        long j2 = 0;
        while (this.f4202f.read(this.f4201b, 8192L) != -1) {
            long jG = this.f4201b.g();
            if (jG > 0) {
                j2 += jG;
                rVar.write(this.f4201b, jG);
            }
        }
        if (this.f4201b.B() <= 0) {
            return j2;
        }
        long jB = j2 + this.f4201b.B();
        c cVar = this.f4201b;
        rVar.write(cVar, cVar.B());
        return jB;
    }

    @Override // h.e
    public void X(long j2) throws IOException {
        if (!l(j2)) {
            throw new EOFException();
        }
    }

    public long a(byte b2, long j2, long j3) throws IOException {
        if (this.f4203g) {
            throw new IllegalStateException("closed");
        }
        if (j2 < 0 || j3 < j2) {
            throw new IllegalArgumentException(String.format("fromIndex=%s toIndex=%s", Long.valueOf(j2), Long.valueOf(j3)));
        }
        while (j2 < j3) {
            long jM = this.f4201b.m(b2, j2, j3);
            if (jM != -1) {
                return jM;
            }
            c cVar = this.f4201b;
            long j4 = cVar.f4177f;
            if (j4 >= j3 || this.f4202f.read(cVar, 8192L) == -1) {
                break;
            }
            j2 = Math.max(j2, j4);
        }
        return -1L;
    }

    @Override // h.e, h.d
    public c buffer() {
        return this.f4201b;
    }

    @Override // h.e
    public long c0(byte b2) throws IOException {
        return a(b2, 0L, Long.MAX_VALUE);
    }

    @Override // h.s, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f4203g) {
            return;
        }
        this.f4203g = true;
        this.f4202f.close();
        this.f4201b.a();
    }

    public boolean d(long j2, f fVar, int i2, int i3) throws IOException {
        if (this.f4203g) {
            throw new IllegalStateException("closed");
        }
        if (j2 < 0 || i2 < 0 || i3 < 0 || fVar.q() - i2 < i3) {
            return false;
        }
        for (int i4 = 0; i4 < i3; i4++) {
            long j3 = ((long) i4) + j2;
            if (!l(1 + j3) || this.f4201b.k(j3) != fVar.i(i2 + i4)) {
                return false;
            }
        }
        return true;
    }

    @Override // h.e
    public f e(long j2) throws IOException {
        X(j2);
        return this.f4201b.e(j2);
    }

    @Override // h.e
    public boolean e0(long j2, f fVar) throws IOException {
        return d(j2, fVar, 0, fVar.q());
    }

    public String f(long j2) throws IOException {
        if (j2 < 0) {
            throw new IllegalArgumentException("limit < 0: " + j2);
        }
        long j3 = j2 == Long.MAX_VALUE ? Long.MAX_VALUE : j2 + 1;
        long jA = a((byte) 10, 0L, j3);
        if (jA != -1) {
            return this.f4201b.x(jA);
        }
        if (j3 < Long.MAX_VALUE && l(j3) && this.f4201b.k(j3 - 1) == 13 && l(1 + j3) && this.f4201b.k(j3) == 10) {
            return this.f4201b.x(j3);
        }
        c cVar = new c();
        c cVar2 = this.f4201b;
        cVar2.h(cVar, 0L, Math.min(32L, cVar2.B()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.f4201b.B(), j2) + " content=" + cVar.p().j() + (char) 8230);
    }

    @Override // h.e
    public long f0() throws IOException {
        byte bK;
        X(1L);
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (!l(i3)) {
                break;
            }
            bK = this.f4201b.k(i2);
            if ((bK < 48 || bK > 57) && ((bK < 97 || bK > 102) && (bK < 65 || bK > 70))) {
                break;
            }
            i2 = i3;
        }
        if (i2 == 0) {
            throw new NumberFormatException(String.format("Expected leading [0-9a-fA-F] character but was %#x", Byte.valueOf(bK)));
        }
        return this.f4201b.f0();
    }

    @Override // h.e
    public String h0(Charset charset) throws IOException {
        if (charset == null) {
            throw new IllegalArgumentException("charset == null");
        }
        this.f4201b.b(this.f4202f);
        return this.f4201b.h0(charset);
    }

    @Override // h.e
    public InputStream k0() {
        return new a();
    }

    @Override // h.e
    public boolean l(long j2) throws IOException {
        c cVar;
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (this.f4203g) {
            throw new IllegalStateException("closed");
        }
        do {
            cVar = this.f4201b;
            if (cVar.f4177f >= j2) {
                return true;
            }
        } while (this.f4202f.read(cVar, 8192L) != -1);
        return false;
    }

    @Override // h.s
    public long read(c cVar, long j2) throws IOException {
        if (cVar == null) {
            throw new IllegalArgumentException("sink == null");
        }
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (this.f4203g) {
            throw new IllegalStateException("closed");
        }
        c cVar2 = this.f4201b;
        if (cVar2.f4177f == 0 && this.f4202f.read(cVar2, 8192L) == -1) {
            return -1L;
        }
        return this.f4201b.read(cVar, Math.min(j2, this.f4201b.f4177f));
    }

    @Override // h.e
    public byte readByte() throws IOException {
        X(1L);
        return this.f4201b.readByte();
    }

    @Override // h.e
    public void readFully(byte[] bArr) throws IOException {
        try {
            X(bArr.length);
            this.f4201b.readFully(bArr);
        } catch (EOFException e2) {
            int i2 = 0;
            while (true) {
                c cVar = this.f4201b;
                long j2 = cVar.f4177f;
                if (j2 <= 0) {
                    throw e2;
                }
                int i3 = cVar.read(bArr, i2, (int) j2);
                if (i3 == -1) {
                    throw new AssertionError();
                }
                i2 += i3;
            }
        }
    }

    @Override // h.e
    public int readInt() throws IOException {
        X(4L);
        return this.f4201b.readInt();
    }

    @Override // h.e
    public long readLong() throws IOException {
        X(8L);
        return this.f4201b.readLong();
    }

    @Override // h.e
    public short readShort() throws IOException {
        X(2L);
        return this.f4201b.readShort();
    }

    @Override // h.e
    public String s() throws IOException {
        return f(Long.MAX_VALUE);
    }

    @Override // h.e
    public void skip(long j2) throws IOException {
        if (this.f4203g) {
            throw new IllegalStateException("closed");
        }
        while (j2 > 0) {
            c cVar = this.f4201b;
            if (cVar.f4177f == 0 && this.f4202f.read(cVar, 8192L) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j2, this.f4201b.B());
            this.f4201b.skip(jMin);
            j2 -= jMin;
        }
    }

    @Override // h.e
    public byte[] t() throws IOException {
        this.f4201b.b(this.f4202f);
        return this.f4201b.t();
    }

    @Override // h.s
    public t timeout() {
        return this.f4202f.timeout();
    }

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

    @Override // h.e
    public int v() throws IOException {
        X(4L);
        return this.f4201b.v();
    }

    @Override // h.e
    public boolean z() throws IOException {
        if (this.f4203g) {
            throw new IllegalStateException("closed");
        }
        return this.f4201b.z() && this.f4202f.read(this.f4201b, 8192L) == -1;
    }

    /* JADX INFO: compiled from: RealBufferedSource.java */
    class a extends InputStream {
        a() {
        }

        @Override // java.io.InputStream
        public int available() throws IOException {
            n nVar = n.this;
            if (nVar.f4203g) {
                throw new IOException("closed");
            }
            return (int) Math.min(nVar.f4201b.f4177f, 2147483647L);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            n nVar = n.this;
            if (nVar.f4203g) {
                throw new IOException("closed");
            }
            c cVar = nVar.f4201b;
            if (cVar.f4177f == 0 && nVar.f4202f.read(cVar, 8192L) == -1) {
                return -1;
            }
            return n.this.f4201b.readByte() & 255;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i2, int i3) throws IOException {
            if (!n.this.f4203g) {
                u.b(bArr.length, i2, i3);
                n nVar = n.this;
                c cVar = nVar.f4201b;
                if (cVar.f4177f == 0 && nVar.f4202f.read(cVar, 8192L) == -1) {
                    return -1;
                }
                return n.this.f4201b.read(bArr, i2, i3);
            }
            throw new IOException("closed");
        }
    }

    @Override // h.e
    public int read(byte[] bArr, int i2, int i3) throws IOException {
        long j2 = i3;
        u.b(bArr.length, i2, j2);
        c cVar = this.f4201b;
        if (cVar.f4177f == 0 && this.f4202f.read(cVar, 8192L) == -1) {
            return -1;
        }
        return this.f4201b.read(bArr, i2, (int) Math.min(j2, this.f4201b.f4177f));
    }
}
