package f;

import com.seewo.libmcuservice.constant.IMcuConstants;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Objects;

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

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

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

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

    o(t tVar) {
        Objects.requireNonNull(tVar, "source == null");
        this.f3105d = tVar;
    }

    @Override // f.e
    public boolean B(long j, f fVar) throws IOException {
        return c(j, fVar, 0, fVar.q());
    }

    @Override // f.e
    public String C(Charset charset) throws IOException {
        if (charset == null) {
            throw new IllegalArgumentException("charset == null");
        }
        this.f3104c.G(this.f3105d);
        return this.f3104c.C(charset);
    }

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

    @Override // f.e
    public int Q() throws IOException {
        c0(4L);
        return this.f3104c.Q();
    }

    @Override // f.e
    public byte[] S(long j) throws IOException {
        c0(j);
        return this.f3104c.S(j);
    }

    @Override // f.e
    public short W() throws IOException {
        c0(2L);
        return this.f3104c.W();
    }

    @Override // f.e
    public boolean a(long j) throws IOException {
        c cVar;
        if (j < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j);
        }
        if (this.f3106e) {
            throw new IllegalStateException("closed");
        }
        do {
            cVar = this.f3104c;
            if (cVar.f3076e >= j) {
                return true;
            }
        } while (this.f3105d.read(cVar, 8192L) != -1);
        return false;
    }

    public long b(byte b2, long j, long j2) throws IOException {
        if (this.f3106e) {
            throw new IllegalStateException("closed");
        }
        if (j < 0 || j2 < j) {
            throw new IllegalArgumentException(String.format("fromIndex=%s toIndex=%s", Long.valueOf(j), Long.valueOf(j2)));
        }
        while (j < j2) {
            long jK = this.f3104c.k(b2, j, j2);
            if (jK != -1) {
                return jK;
            }
            c cVar = this.f3104c;
            long j3 = cVar.f3076e;
            if (j3 >= j2 || this.f3105d.read(cVar, 8192L) == -1) {
                break;
            }
            j = Math.max(j, j3);
        }
        return -1L;
    }

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

    public boolean c(long j, f fVar, int i, int i2) throws IOException {
        if (this.f3106e) {
            throw new IllegalStateException("closed");
        }
        if (j < 0 || i < 0 || i2 < 0 || fVar.q() - i < i2) {
            return false;
        }
        for (int i3 = 0; i3 < i2; i3++) {
            long j2 = ((long) i3) + j;
            if (!a(1 + j2) || this.f3104c.i(j2) != fVar.i(i + i3)) {
                return false;
            }
        }
        return true;
    }

    @Override // f.e
    public void c0(long j) throws IOException {
        if (!a(j)) {
            throw new EOFException();
        }
    }

    @Override // f.t, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f3106e) {
            return;
        }
        this.f3106e = true;
        this.f3105d.close();
        this.f3104c.b();
    }

    @Override // f.e
    public f d(long j) throws IOException {
        c0(j);
        return this.f3104c.d(j);
    }

    public String e(long j) throws IOException {
        if (j < 0) {
            throw new IllegalArgumentException("limit < 0: " + j);
        }
        long j2 = j == Long.MAX_VALUE ? Long.MAX_VALUE : j + 1;
        long jB = b((byte) 10, 0L, j2);
        if (jB != -1) {
            return this.f3104c.v(jB);
        }
        if (j2 < Long.MAX_VALUE && a(j2) && this.f3104c.i(j2 - 1) == 13 && a(1 + j2) && this.f3104c.i(j2) == 10) {
            return this.f3104c.v(j2);
        }
        c cVar = new c();
        c cVar2 = this.f3104c;
        cVar2.g(cVar, 0L, Math.min(32L, cVar2.x()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.f3104c.x(), j) + " content=" + cVar.n().j() + (char) 8230);
    }

    @Override // f.e
    public long f0(byte b2) throws IOException {
        return b(b2, 0L, Long.MAX_VALUE);
    }

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

    @Override // f.e
    public byte[] j() throws IOException {
        this.f3104c.G(this.f3105d);
        return this.f3104c.j();
    }

    @Override // f.e
    public InputStream j0() {
        return new a();
    }

    @Override // f.e
    public boolean m() throws IOException {
        if (this.f3106e) {
            throw new IllegalStateException("closed");
        }
        return this.f3104c.m() && this.f3105d.read(this.f3104c, 8192L) == -1;
    }

    @Override // f.e
    public void q(c cVar, long j) throws IOException {
        try {
            c0(j);
            this.f3104c.q(cVar, j);
        } catch (EOFException e2) {
            cVar.G(this.f3104c);
            throw e2;
        }
    }

    @Override // f.t
    public long read(c cVar, long j) throws IOException {
        if (cVar == null) {
            throw new IllegalArgumentException("sink == null");
        }
        if (j < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j);
        }
        if (this.f3106e) {
            throw new IllegalStateException("closed");
        }
        c cVar2 = this.f3104c;
        if (cVar2.f3076e == 0 && this.f3105d.read(cVar2, 8192L) == -1) {
            return -1L;
        }
        return this.f3104c.read(cVar, Math.min(j, this.f3104c.f3076e));
    }

    @Override // f.e
    public byte readByte() throws IOException {
        c0(1L);
        return this.f3104c.readByte();
    }

    @Override // f.e
    public void readFully(byte[] bArr) throws IOException {
        try {
            c0(bArr.length);
            this.f3104c.readFully(bArr);
        } catch (EOFException e2) {
            int i = 0;
            while (true) {
                c cVar = this.f3104c;
                long j = cVar.f3076e;
                if (j <= 0) {
                    throw e2;
                }
                int i2 = cVar.read(bArr, i, (int) j);
                if (i2 == -1) {
                    throw new AssertionError();
                }
                i += i2;
            }
        }
    }

    @Override // f.e
    public int readInt() throws IOException {
        c0(4L);
        return this.f3104c.readInt();
    }

    @Override // f.e
    public long readLong() throws IOException {
        c0(8L);
        return this.f3104c.readLong();
    }

    @Override // f.e
    public short readShort() throws IOException {
        c0(2L);
        return this.f3104c.readShort();
    }

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

    @Override // f.e
    public void skip(long j) throws IOException {
        if (this.f3106e) {
            throw new IllegalStateException("closed");
        }
        while (j > 0) {
            c cVar = this.f3104c;
            if (cVar.f3076e == 0 && this.f3105d.read(cVar, 8192L) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j, this.f3104c.x());
            this.f3104c.skip(jMin);
            j -= jMin;
        }
    }

    @Override // f.t
    public u timeout() {
        return this.f3105d.timeout();
    }

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

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

        @Override // java.io.InputStream
        public int available() throws IOException {
            o oVar = o.this;
            if (oVar.f3106e) {
                throw new IOException("closed");
            }
            return (int) Math.min(oVar.f3104c.f3076e, 2147483647L);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            o oVar = o.this;
            if (oVar.f3106e) {
                throw new IOException("closed");
            }
            c cVar = oVar.f3104c;
            if (cVar.f3076e == 0 && oVar.f3105d.read(cVar, 8192L) == -1) {
                return -1;
            }
            return o.this.f3104c.readByte() & IMcuConstants.DEFAULT_VALUE;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) throws IOException {
            if (!o.this.f3106e) {
                v.b(bArr.length, i, i2);
                o oVar = o.this;
                c cVar = oVar.f3104c;
                if (cVar.f3076e == 0 && oVar.f3105d.read(cVar, 8192L) == -1) {
                    return -1;
                }
                return o.this.f3104c.read(bArr, i, i2);
            }
            throw new IOException("closed");
        }
    }

    @Override // f.e
    public int read(byte[] bArr, int i, int i2) throws IOException {
        long j = i2;
        v.b(bArr.length, i, j);
        c cVar = this.f3104c;
        if (cVar.f3076e == 0 && this.f3105d.read(cVar, 8192L) == -1) {
            return -1;
        }
        return this.f3104c.read(bArr, i, (int) Math.min(j, this.f3104c.f3076e));
    }
}
