package i;

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 org.snmp4j.mp.MPv3;

/* JADX INFO: compiled from: RealBufferedSource.kt */
/* JADX INFO: loaded from: classes.dex */
public final class x implements h {
    public final f b;

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

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

    public x(d0 d0Var) {
        h.x.d.k.d(d0Var, "source");
        this.f1052f = d0Var;
        this.b = new f();
    }

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

    @Override // i.h
    public byte[] C(long j2) throws EOFException {
        Y(j2);
        return this.b.C(j2);
    }

    @Override // i.h
    public void I(f fVar, long j2) throws IOException {
        h.x.d.k.d(fVar, "sink");
        try {
            Y(j2);
            this.b.I(fVar, j2);
        } catch (EOFException e2) {
            fVar.e(this.b);
            throw e2;
        }
    }

    @Override // i.h
    public long M() throws EOFException {
        byte bN;
        Y(1L);
        long j2 = 0;
        while (true) {
            long j3 = j2 + 1;
            if (!o(j3)) {
                break;
            }
            bN = this.b.n(j2);
            if ((bN < ((byte) 48) || bN > ((byte) 57)) && !(j2 == 0 && bN == ((byte) 45))) {
                break;
            }
            j2 = j3;
        }
        if (j2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9] or '-' character but was 0x");
            h.c0.a.a(16);
            h.c0.a.a(16);
            String string = Integer.toString(bN, 16);
            h.x.d.k.c(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.b.M();
    }

    @Override // i.h
    public String O(long j2) throws EOFException {
        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 jB = b(b, 0L, j3);
        if (jB != -1) {
            return i.f0.a.c(this.b, jB);
        }
        if (j3 < Long.MAX_VALUE && o(j3) && this.b.n(j3 - 1) == ((byte) 13) && o(1 + j3) && this.b.n(j3) == b) {
            return i.f0.a.c(this.b, j3);
        }
        f fVar = new f();
        f fVar2 = this.b;
        fVar2.j(fVar, 0L, Math.min(32, fVar2.J()));
        throw new EOFException("\\n not found: limit=" + Math.min(this.b.J(), j2) + " content=" + fVar.g().i() + "…");
    }

    @Override // i.h
    public void Y(long j2) throws EOFException {
        if (!o(j2)) {
            throw new EOFException();
        }
    }

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

    public long b(byte b, long j2, long j3) {
        if (!(!this.f1051e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (!(0 <= j2 && j3 >= j2)) {
            throw new IllegalArgumentException(("fromIndex=" + j2 + " toIndex=" + j3).toString());
        }
        while (j2 < j3) {
            long jP = this.b.p(b, j2, j3);
            if (jP != -1) {
                return jP;
            }
            long J = this.b.J();
            if (J >= j3 || this.f1052f.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                return -1L;
            }
            j2 = Math.max(j2, J);
        }
        return -1L;
    }

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

    @Override // i.d0, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f1051e) {
            return;
        }
        this.f1051e = true;
        this.f1052f.close();
        this.b.a();
    }

    public boolean d(long j2, i iVar, int i2, int i3) {
        h.x.d.k.d(iVar, "bytes");
        if (!(!this.f1051e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (j2 >= 0 && i2 >= 0 && i3 >= 0 && iVar.s() - i2 >= i3) {
            for (int i4 = 0; i4 < i3; i4++) {
                long j3 = ((long) i4) + j2;
                if (o(1 + j3) && this.b.n(j3) == iVar.d(i2 + i4)) {
                }
            }
            return true;
        }
        return false;
    }

    @Override // i.h
    public boolean e0(long j2, i iVar) {
        h.x.d.k.d(iVar, "bytes");
        return d(j2, iVar, 0, iVar.s());
    }

    @Override // i.h
    public long f0() throws EOFException {
        byte bN;
        Y(1L);
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (!o(i3)) {
                break;
            }
            bN = this.b.n(i2);
            if ((bN < ((byte) 48) || bN > ((byte) 57)) && ((bN < ((byte) 97) || bN > ((byte) InvokeCmd.CMD_SYSTEMCTRL_GET_FREEZABLE_APPS)) && (bN < ((byte) 65) || bN > ((byte) 70)))) {
                break;
            }
            i2 = i3;
        }
        if (i2 == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append("Expected leading [0-9a-fA-F] character but was 0x");
            h.c0.a.a(16);
            h.c0.a.a(16);
            String string = Integer.toString(bN, 16);
            h.x.d.k.c(string, "java.lang.Integer.toStri…(this, checkRadix(radix))");
            sb.append(string);
            throw new NumberFormatException(sb.toString());
        }
        return this.b.f0();
    }

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

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

    @Override // i.h
    public String h0(Charset charset) throws IOException {
        h.x.d.k.d(charset, "charset");
        this.b.e(this.f1052f);
        return this.b.h0(charset);
    }

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

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

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

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

    @Override // i.h
    public int m0(t tVar) throws EOFException {
        h.x.d.k.d(tVar, "options");
        if (!(!this.f1051e)) {
            throw new IllegalStateException("closed".toString());
        }
        while (true) {
            int iD = i.f0.a.d(this.b, tVar, true);
            if (iD != -2) {
                if (iD != -1) {
                    this.b.skip(tVar.d()[iD].s());
                    return iD;
                }
            } else if (this.f1052f.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                break;
            }
        }
        return -1;
    }

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

    @Override // i.h
    public h peek() {
        return q.d(new v(this));
    }

    @Override // i.d0
    public long read(f fVar, long j2) {
        h.x.d.k.d(fVar, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (!(true ^ this.f1051e)) {
            throw new IllegalStateException("closed".toString());
        }
        if (this.b.J() == 0 && this.f1052f.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
            return -1L;
        }
        return this.b.read(fVar, Math.min(j2, this.b.J()));
    }

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

    @Override // i.h
    public void readFully(byte[] bArr) throws EOFException {
        h.x.d.k.d(bArr, "sink");
        try {
            Y(bArr.length);
            this.b.readFully(bArr);
        } catch (EOFException e2) {
            int i2 = 0;
            while (this.b.J() > 0) {
                f fVar = this.b;
                int i3 = fVar.read(bArr, i2, (int) fVar.J());
                if (i3 == -1) {
                    throw new AssertionError();
                }
                i2 += i3;
            }
            throw e2;
        }
    }

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

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

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

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

    @Override // i.d0
    public e0 timeout() {
        return this.f1052f.timeout();
    }

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

    @Override // i.h
    public String v() {
        return O(Long.MAX_VALUE);
    }

    @Override // i.h
    public byte[] w() throws IOException {
        this.b.e(this.f1052f);
        return this.b.w();
    }

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

        @Override // java.io.InputStream
        public int available() throws IOException {
            x xVar = x.this;
            if (xVar.f1051e) {
                throw new IOException("closed");
            }
            return (int) Math.min(xVar.b.J(), MPv3.MAX_MESSAGE_ID);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            x xVar = x.this;
            if (xVar.f1051e) {
                throw new IOException("closed");
            }
            if (xVar.b.J() == 0) {
                x xVar2 = x.this;
                if (xVar2.f1052f.read(xVar2.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                    return -1;
                }
            }
            return x.this.b.readByte() & 255;
        }

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

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i2, int i3) throws IOException {
            h.x.d.k.d(bArr, "data");
            if (!x.this.f1051e) {
                c.b(bArr.length, i2, i3);
                if (x.this.b.J() == 0) {
                    x xVar = x.this;
                    if (xVar.f1052f.read(xVar.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
                        return -1;
                    }
                }
                return x.this.b.read(bArr, i2, i3);
            }
            throw new IOException("closed");
        }
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer byteBuffer) {
        h.x.d.k.d(byteBuffer, "sink");
        if (this.b.J() == 0 && this.f1052f.read(this.b, InvokeCmd.CMD_TOUCH_CTRL_START) == -1) {
            return -1;
        }
        return this.b.read(byteBuffer);
    }
}
