package l;

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;

/* JADX INFO: compiled from: RealBufferedSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class u implements h {
    public final f b;
    public boolean d;
    public final a0 e;

    public u(a0 a0Var) {
        j.p0.d.r.g(a0Var, "source");
        this.e = a0Var;
        this.b = new f();
    }

    @Override // l.h
    public boolean E() {
        if (!this.d) {
            return this.b.E() && this.e.read(this.b, (long) InvokeCmd.CMD_TOUCH_CTRL_START) == -1;
        }
        throw new IllegalStateException("closed".toString());
    }

    @Override // l.h
    public byte[] G(long j2) throws EOFException {
        e0(j2);
        return this.b.G(j2);
    }

    @Override // l.h
    public void N(f fVar, long j2) throws IOException {
        j.p0.d.r.g(fVar, "sink");
        try {
            e0(j2);
            this.b.N(fVar, j2);
        } catch (EOFException e) {
            fVar.d(this.b);
            throw e;
        }
    }

    @Override // l.h
    public long R() throws EOFException {
        byte bR;
        e0(1L);
        long j2 = 0;
        while (true) {
            long j3 = j2 + 1;
            if (!q(j3)) {
                break;
            }
            bR = this.b.r(j2);
            if ((bR < ((byte) 48) || bR > ((byte) 57)) && !(j2 == 0 && bR == ((byte) 45))) {
                break;
            }
            j2 = j3;
        }
        if (j2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9] or '-' character but was 0x");
            j.u0.a.a(16);
            j.u0.a.a(16);
            String string = Integer.toString(bR, 16);
            j.p0.d.r.b(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.b.R();
    }

    @Override // l.h
    public String T(long j2) throws Throwable {
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("limit < 0: " + j2).toString());
        }
        long j3 = j2 == Long.MAX_VALUE ? Long.MAX_VALUE : j2 + 1;
        byte b = (byte) 10;
        long jF = f(b, 0L, j3);
        if (jF != -1) {
            return l.c0.a.b(this.b, jF);
        }
        if (j3 < Long.MAX_VALUE && q(j3) && this.b.r(j3 - 1) == ((byte) 13) && q(1 + j3) && this.b.r(j3) == b) {
            return l.c0.a.b(this.b, j3);
        }
        f fVar = new f();
        f fVar2 = this.b;
        fVar2.l(fVar, 0L, Math.min(32, fVar2.K()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.b.K(), j2) + " content=" + fVar.g().i() + "…");
    }

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

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

    public long c(byte b) {
        return f(b, 0L, Long.MAX_VALUE);
    }

    @Override // l.a0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.d) {
            return;
        }
        this.d = true;
        this.e.close();
        this.b.c();
    }

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

    public long f(byte b, long j2, long j3) throws Throwable {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        if (!(0 <= j2 && j3 >= j2)) {
            throw new IllegalArgumentException(("fromIndex=" + j2 + " toIndex=" + j3).toString());
        }
        while (j2 < j3) {
            long jS = this.b.s(b, j2, j3);
            if (jS != -1) {
                return jS;
            }
            long jK = this.b.K();
            if (jK >= j3 || this.e.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                return -1L;
            }
            j2 = Math.max(j2, jK);
        }
        return -1L;
    }

    @Override // l.h
    public i g() throws IOException {
        this.b.d(this.e);
        return this.b.g();
    }

    @Override // l.h
    public i h(long j2) throws EOFException {
        e0(j2);
        return this.b.h(j2);
    }

    public int i() throws EOFException {
        e0(4L);
        return this.b.A();
    }

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

    public short j() throws EOFException {
        e0(2L);
        return this.b.B();
    }

    @Override // l.h
    public void k(long j2) throws EOFException {
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        while (j2 > 0) {
            if (this.b.K() == 0 && this.e.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j2, this.b.K());
            this.b.k(jMin);
            j2 -= jMin;
        }
    }

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

    @Override // l.h
    public String m0(Charset charset) throws IOException {
        j.p0.d.r.g(charset, "charset");
        this.b.d(this.e);
        return this.b.m0(charset);
    }

    @Override // l.h
    public InputStream p0() {
        return new a();
    }

    @Override // l.h
    public boolean q(long j2) {
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        while (this.b.K() < j2) {
            if (this.e.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                return false;
            }
        }
        return true;
    }

    @Override // l.h
    public int r0(q qVar) throws EOFException {
        j.p0.d.r.g(qVar, "options");
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        while (true) {
            int iC = l.c0.a.c(this.b, qVar, true);
            if (iC != -2) {
                if (iC != -1) {
                    this.b.k(qVar.e()[iC].s());
                    return iC;
                }
            } else if (this.e.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                break;
            }
        }
        return -1;
    }

    @Override // l.a0
    public long read(f fVar, long j2) {
        j.p0.d.r.g(fVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (!(!this.d)) {
            throw new IllegalStateException("closed".toString());
        }
        if (this.b.K() == 0 && this.e.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
            return -1L;
        }
        return this.b.read(fVar, Math.min(j2, this.b.K()));
    }

    @Override // l.h
    public byte readByte() throws EOFException {
        e0(1L);
        return this.b.readByte();
    }

    @Override // l.h
    public void readFully(byte[] bArr) throws EOFException {
        j.p0.d.r.g(bArr, "sink");
        try {
            e0(bArr.length);
            this.b.readFully(bArr);
        } catch (EOFException e) {
            int i2 = 0;
            while (this.b.K() > 0) {
                f fVar = this.b;
                int iW = fVar.w(bArr, i2, (int) fVar.K());
                if (iW == -1) {
                    throw new AssertionError();
                }
                i2 += iW;
            }
            throw e;
        }
    }

    @Override // l.h
    public int readInt() throws EOFException {
        e0(4L);
        return this.b.readInt();
    }

    @Override // l.h
    public long readLong() throws EOFException {
        e0(8L);
        return this.b.readLong();
    }

    @Override // l.h
    public short readShort() throws EOFException {
        e0(2L);
        return this.b.readShort();
    }

    @Override // l.a0
    public b0 timeout() {
        return this.e.timeout();
    }

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

    @Override // l.h
    public String y() {
        return T(Long.MAX_VALUE);
    }

    @Override // l.h
    public byte[] z() throws IOException {
        this.b.d(this.e);
        return this.b.z();
    }

    /* 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.d) {
                throw new IOException("closed");
            }
            return (int) Math.min(uVar.b.K(), 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.d) {
                throw new IOException("closed");
            }
            if (uVar.b.K() == 0) {
                u uVar2 = u.this;
                if (uVar2.e.read(uVar2.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                    return -1;
                }
            }
            return u.this.b.readByte() & 255;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i2, int i3) throws IOException {
            j.p0.d.r.g(bArr, "data");
            if (!u.this.d) {
                c.b(bArr.length, i2, i3);
                if (u.this.b.K() == 0) {
                    u uVar = u.this;
                    if (uVar.e.read(uVar.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                        return -1;
                    }
                }
                return u.this.b.w(bArr, i2, i3);
            }
            throw new IOException("closed");
        }
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer byteBuffer) {
        j.p0.d.r.g(byteBuffer, "sink");
        if (this.b.K() == 0 && this.e.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
            return -1;
        }
        return this.b.read(byteBuffer);
    }
}
