package j;

import com.cvte.vman.instance.InvokeCmd;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Objects;
import jxl.SheetSettings;

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

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

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

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

    public v(b0 b0Var) {
        g.h0.c.l.f(b0Var, "source");
        this.f4647f = b0Var;
        this.f4645b = new d();
    }

    @Override // j.g
    public boolean B(long j2, h hVar) {
        g.h0.c.l.f(hVar, "bytes");
        int iC = hVar.c();
        g.h0.c.l.f(hVar, "bytes");
        if (!(!this.f4646e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j2 >= 0 && iC >= 0 && hVar.c() - 0 >= iC) {
            for (int i2 = 0; i2 < iC; i2++) {
                long j3 = ((long) i2) + j2;
                if (M(1 + j3) && this.f4645b.e(j3) == hVar.f(0 + i2)) {
                }
            }
            return true;
        }
        return false;
    }

    @Override // j.g
    public String C(Charset charset) throws IOException {
        g.h0.c.l.f(charset, "charset");
        this.f4645b.writeAll(this.f4647f);
        d dVar = this.f4645b;
        Objects.requireNonNull(dVar);
        g.h0.c.l.f(charset, "charset");
        return dVar.q(dVar.f4601e, charset);
    }

    @Override // j.g
    public byte F() throws EOFException {
        e0(1L);
        return this.f4645b.F();
    }

    @Override // j.g
    public void H(byte[] bArr) throws EOFException {
        g.h0.c.l.f(bArr, "sink");
        try {
            e0(bArr.length);
            this.f4645b.H(bArr);
        } catch (EOFException e2) {
            int i2 = 0;
            while (true) {
                d dVar = this.f4645b;
                long j2 = dVar.f4601e;
                if (j2 <= 0) {
                    throw e2;
                }
                int iL = dVar.l(bArr, i2, (int) j2);
                if (iL == -1) {
                    throw new AssertionError();
                }
                i2 += iL;
            }
        }
    }

    @Override // j.g
    public h I() throws IOException {
        this.f4645b.writeAll(this.f4647f);
        return this.f4645b.I();
    }

    @Override // j.g
    public void K(long j2) throws EOFException {
        if (!(!this.f4646e)) {
            throw new IllegalStateException("closed".toString());
        }
        while (j2 > 0) {
            d dVar = this.f4645b;
            if (dVar.f4601e == 0 && this.f4647f.read(dVar, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j2, this.f4645b.f4601e);
            this.f4645b.K(jMin);
            j2 -= jMin;
        }
    }

    @Override // j.g
    public boolean M(long j2) {
        d dVar;
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(d.b.a.a.a.l("byteCount < 0: ", j2).toString());
        }
        if (!(!this.f4646e)) {
            throw new IllegalStateException("closed".toString());
        }
        do {
            dVar = this.f4645b;
            if (dVar.f4601e >= j2) {
                return true;
            }
        } while (this.f4647f.read(dVar, InvokeCmd.CMD_TOUCH_CTRL_START) != -1);
        return false;
    }

    @Override // j.g
    public String R() {
        return u(Long.MAX_VALUE);
    }

    @Override // j.g
    public long Y(z zVar) {
        g.h0.c.l.f(zVar, "sink");
        long j2 = 0;
        while (this.f4647f.read(this.f4645b, InvokeCmd.CMD_TOUCH_CTRL_START) != -1) {
            long jB = this.f4645b.b();
            if (jB > 0) {
                j2 += jB;
                ((d) zVar).write(this.f4645b, jB);
            }
        }
        d dVar = this.f4645b;
        long j3 = dVar.f4601e;
        if (j3 <= 0) {
            return j2;
        }
        long j4 = j2 + j3;
        ((d) zVar).write(dVar, j3);
        return j4;
    }

    @Override // j.g
    public short Z() throws EOFException {
        e0(2L);
        return this.f4645b.Z();
    }

    public long a(byte b2, long j2, long j3) {
        if (!(!this.f4646e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (!(0 <= j2 && j3 >= j2)) {
            throw new IllegalArgumentException(("fromIndex=" + j2 + " toIndex=" + j3).toString());
        }
        while (j2 < j3) {
            long jG = this.f4645b.g(b2, j2, j3);
            if (jG != -1) {
                return jG;
            }
            d dVar = this.f4645b;
            long j4 = dVar.f4601e;
            if (j4 >= j3 || this.f4647f.read(dVar, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                return -1L;
            }
            j2 = Math.max(j2, j4);
        }
        return -1L;
    }

    public byte[] b(long j2) throws EOFException {
        if (M(j2)) {
            return this.f4645b.o(j2);
        }
        throw new EOFException();
    }

    @Override // j.g
    public g b0() {
        return f.a.y.a.t(new t(this));
    }

    @Override // j.g
    public h c(long j2) throws EOFException {
        if (M(j2)) {
            return this.f4645b.c(j2);
        }
        throw new EOFException();
    }

    @Override // j.b0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f4646e) {
            return;
        }
        this.f4646e = true;
        this.f4647f.close();
        d dVar = this.f4645b;
        dVar.K(dVar.f4601e);
    }

    public int d() throws EOFException {
        e0(4L);
        int iH = this.f4645b.h();
        return ((iH & SheetSettings.DEFAULT_DEFAULT_ROW_HEIGHT) << 24) | (((-16777216) & iH) >>> 24) | ((16711680 & iH) >>> 8) | ((65280 & iH) << 8);
    }

    @Override // j.g
    public void e0(long j2) throws EOFException {
        if (!M(j2)) {
            throw new EOFException();
        }
    }

    @Override // j.g, j.f
    public d getBuffer() {
        return this.f4645b;
    }

    @Override // j.g
    public int h() throws EOFException {
        e0(4L);
        return this.f4645b.h();
    }

    @Override // j.g
    public long h0() throws EOFException {
        byte bE;
        e0(1L);
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (!M(i3)) {
                break;
            }
            bE = this.f4645b.e(i2);
            if ((bE < ((byte) 48) || bE > ((byte) 57)) && ((bE < ((byte) 97) || bE > ((byte) 102)) && (bE < ((byte) 65) || bE > ((byte) 70)))) {
                break;
            }
            i2 = i3;
        }
        if (i2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9a-fA-F] character but was 0x");
            f.a.y.a.A(16);
            f.a.y.a.A(16);
            String string = Integer.toString(bE, 16);
            g.h0.c.l.e(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.f4645b.h0();
    }

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

    @Override // j.g
    public long j() throws EOFException {
        e0(8L);
        return this.f4645b.j();
    }

    @Override // j.g
    public InputStream j0() {
        return new a();
    }

    @Override // j.g
    public byte[] k() throws IOException {
        this.f4645b.writeAll(this.f4647f);
        return this.f4645b.k();
    }

    @Override // j.g
    public int k0(r rVar) throws EOFException {
        g.h0.c.l.f(rVar, "options");
        if (!(!this.f4646e)) {
            throw new IllegalStateException("closed".toString());
        }
        while (true) {
            int iC = j.d0.a.c(this.f4645b, rVar, true);
            if (iC != -2) {
                if (iC != -1) {
                    this.f4645b.K(rVar.f4632e[iC].c());
                    return iC;
                }
            } else if (this.f4647f.read(this.f4645b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                break;
            }
        }
        return -1;
    }

    @Override // j.g
    public boolean n() {
        if (!this.f4646e) {
            return this.f4645b.n() && this.f4647f.read(this.f4645b, (long) InvokeCmd.CMD_TOUCH_CTRL_START) == -1;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // j.g
    public void r(d dVar, long j2) throws IOException {
        g.h0.c.l.f(dVar, "sink");
        try {
            if (!M(j2)) {
                throw new EOFException();
            }
            this.f4645b.r(dVar, j2);
        } catch (EOFException e2) {
            dVar.writeAll(this.f4645b);
            throw e2;
        }
    }

    @Override // j.b0
    public long read(d dVar, long j2) {
        g.h0.c.l.f(dVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(d.b.a.a.a.l("byteCount < 0: ", j2).toString());
        }
        if (!(!this.f4646e)) {
            throw new IllegalStateException("closed".toString());
        }
        d dVar2 = this.f4645b;
        if (dVar2.f4601e == 0 && this.f4647f.read(dVar2, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
            return -1L;
        }
        return this.f4645b.read(dVar, Math.min(j2, this.f4645b.f4601e));
    }

    @Override // j.g
    public long s() throws EOFException {
        byte bE;
        e0(1L);
        long j2 = 0;
        while (true) {
            long j3 = j2 + 1;
            if (!M(j3)) {
                break;
            }
            bE = this.f4645b.e(j2);
            if ((bE < ((byte) 48) || bE > ((byte) 57)) && !(j2 == 0 && bE == ((byte) 45))) {
                break;
            }
            j2 = j3;
        }
        if (j2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9] or '-' character but was 0x");
            f.a.y.a.A(16);
            f.a.y.a.A(16);
            String string = Integer.toString(bE, 16);
            g.h0.c.l.e(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.f4645b.s();
    }

    @Override // j.b0
    public c0 timeout() {
        return this.f4647f.timeout();
    }

    public String toString() {
        StringBuilder sbB = d.b.a.a.a.B("buffer(");
        sbB.append(this.f4647f);
        sbB.append(')');
        return sbB.toString();
    }

    @Override // j.g
    public String u(long j2) throws EOFException {
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(d.b.a.a.a.l("limit < 0: ", j2).toString());
        }
        long j3 = j2 == Long.MAX_VALUE ? Long.MAX_VALUE : j2 + 1;
        byte b2 = (byte) 10;
        long jA = a(b2, 0L, j3);
        if (jA != -1) {
            return j.d0.a.b(this.f4645b, jA);
        }
        if (j3 < Long.MAX_VALUE && M(j3) && this.f4645b.e(j3 - 1) == ((byte) 13) && M(1 + j3) && this.f4645b.e(j3) == b2) {
            return j.d0.a.b(this.f4645b, j3);
        }
        d dVar = new d();
        d dVar2 = this.f4645b;
        dVar2.d(dVar, 0L, Math.min(32, dVar2.f4601e));
        StringBuilder sbB = d.b.a.a.a.B("\\n not found: limit=");
        sbB.append(Math.min(this.f4645b.f4601e, j2));
        sbB.append(" content=");
        sbB.append(dVar.I().d());
        sbB.append("…");
        throw new EOFException(sbB.toString());
    }

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

        @Override // java.io.InputStream
        public int available() throws IOException {
            v vVar = v.this;
            if (vVar.f4646e) {
                throw new IOException("closed");
            }
            return (int) Math.min(vVar.f4645b.f4601e, Integer.MAX_VALUE);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            v vVar = v.this;
            if (vVar.f4646e) {
                throw new IOException("closed");
            }
            d dVar = vVar.f4645b;
            if (dVar.f4601e == 0 && vVar.f4647f.read(dVar, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                return -1;
            }
            return v.this.f4645b.F() & 255;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i2, int i3) throws IOException {
            g.h0.c.l.f(bArr, "data");
            if (!v.this.f4646e) {
                f.a.y.a.z(bArr.length, i2, i3);
                v vVar = v.this;
                d dVar = vVar.f4645b;
                if (dVar.f4601e == 0 && vVar.f4647f.read(dVar, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                    return -1;
                }
                return v.this.f4645b.l(bArr, i2, i3);
            }
            throw new IOException("closed");
        }
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer byteBuffer) {
        g.h0.c.l.f(byteBuffer, "sink");
        d dVar = this.f4645b;
        if (dVar.f4601e == 0 && this.f4647f.read(dVar, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
            return -1;
        }
        return this.f4645b.read(byteBuffer);
    }
}
