package h;

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

/* JADX INFO: loaded from: classes.dex */
public final class s implements h {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final x f6557c;

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

    public class a extends InputStream {
        public a() {
        }

        @Override // java.io.InputStream
        public int available() throws IOException {
            s sVar = s.this;
            if (sVar.f6558d) {
                throw new IOException("closed");
            }
            return (int) Math.min(sVar.f6556b.f6529d, 2147483647L);
        }

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

        @Override // java.io.InputStream
        public int read() throws IOException {
            s sVar = s.this;
            if (sVar.f6558d) {
                throw new IOException("closed");
            }
            f fVar = sVar.f6556b;
            if (fVar.f6529d == 0 && sVar.f6557c.v(fVar, 8192L) == -1) {
                return -1;
            }
            return s.this.f6556b.J() & IMcuConstants.DEFAULT_VALUE;
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i2, int i3) throws IOException {
            if (s.this.f6558d) {
                throw new IOException("closed");
            }
            z.b(bArr.length, i2, i3);
            s sVar = s.this;
            f fVar = sVar.f6556b;
            if (fVar.f6529d == 0 && sVar.f6557c.v(fVar, 8192L) == -1) {
                return -1;
            }
            return s.this.f6556b.P(bArr, i2, i3);
        }

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

    public s(x xVar) {
        Objects.requireNonNull(xVar, "source == null");
        this.f6557c = xVar;
    }

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

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

    @Override // h.h
    public boolean G(long j2, i iVar) {
        int iK = iVar.k();
        if (this.f6558d) {
            throw new IllegalStateException("closed");
        }
        if (j2 < 0 || iK < 0 || iVar.k() - 0 < iK) {
            return false;
        }
        for (int i2 = 0; i2 < iK; i2++) {
            long j3 = ((long) i2) + j2;
            if (!o(1 + j3) || this.f6556b.N(j3) != iVar.f(0 + i2)) {
                return false;
            }
        }
        return true;
    }

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

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

    @Override // h.h
    public byte J() throws EOFException {
        A(1L);
        return this.f6556b.J();
    }

    public long a(byte b2, long j2, long j3) {
        if (this.f6558d) {
            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 jO = this.f6556b.O(b2, j2, j3);
            if (jO != -1) {
                return jO;
            }
            f fVar = this.f6556b;
            long j4 = fVar.f6529d;
            if (j4 >= j3 || this.f6557c.v(fVar, 8192L) == -1) {
                break;
            }
            j2 = Math.max(j2, j4);
        }
        return -1L;
    }

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

    @Override // h.h
    public int c() throws EOFException {
        A(4L);
        return this.f6556b.c();
    }

    @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this.f6558d) {
            return;
        }
        this.f6558d = true;
        this.f6557c.close();
        this.f6556b.K();
    }

    @Override // h.x
    public y e() {
        return this.f6557c.e();
    }

    public void i(byte[] bArr) throws EOFException {
        try {
            A(bArr.length);
            this.f6556b.R(bArr);
        } catch (EOFException e2) {
            int i2 = 0;
            while (true) {
                f fVar = this.f6556b;
                long j2 = fVar.f6529d;
                if (j2 <= 0) {
                    throw e2;
                }
                int iP = fVar.P(bArr, i2, (int) j2);
                if (iP == -1) {
                    throw new AssertionError();
                }
                i2 += iP;
            }
        }
    }

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

    @Override // h.h
    public i k(long j2) throws EOFException {
        if (o(j2)) {
            return this.f6556b.k(j2);
        }
        throw new EOFException();
    }

    @Override // h.h
    public void l(long j2) throws EOFException {
        if (this.f6558d) {
            throw new IllegalStateException("closed");
        }
        while (j2 > 0) {
            f fVar = this.f6556b;
            if (fVar.f6529d == 0 && this.f6557c.v(fVar, 8192L) == -1) {
                throw new EOFException();
            }
            long jMin = Math.min(j2, this.f6556b.f6529d);
            this.f6556b.l(jMin);
            j2 -= jMin;
        }
    }

    @Override // h.h
    public String n() {
        return w(Long.MAX_VALUE);
    }

    public boolean o(long j2) {
        f fVar;
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (this.f6558d) {
            throw new IllegalStateException("closed");
        }
        do {
            fVar = this.f6556b;
            if (fVar.f6529d >= j2) {
                return true;
            }
        } while (this.f6557c.v(fVar, 8192L) != -1);
        return false;
    }

    @Override // h.h
    public int p() throws EOFException {
        A(4L);
        return this.f6556b.p();
    }

    @Override // h.h
    public boolean q() {
        if (this.f6558d) {
            throw new IllegalStateException("closed");
        }
        return this.f6556b.q() && this.f6557c.v(this.f6556b, 8192L) == -1;
    }

    @Override // java.nio.channels.ReadableByteChannel
    public int read(ByteBuffer byteBuffer) {
        f fVar = this.f6556b;
        if (fVar.f6529d == 0 && this.f6557c.v(fVar, 8192L) == -1) {
            return -1;
        }
        return this.f6556b.read(byteBuffer);
    }

    @Override // h.h
    public byte[] s(long j2) throws EOFException {
        if (o(j2)) {
            return this.f6556b.s(j2);
        }
        throw new EOFException();
    }

    public String toString() {
        StringBuilder sbC = a.b.c.a.a.C("buffer(");
        sbC.append(this.f6557c);
        sbC.append(")");
        return sbC.toString();
    }

    @Override // h.h
    public short u() throws EOFException {
        A(2L);
        return this.f6556b.u();
    }

    @Override // h.x
    public long v(f fVar, long j2) {
        if (fVar == null) {
            throw new IllegalArgumentException("sink == null");
        }
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (this.f6558d) {
            throw new IllegalStateException("closed");
        }
        f fVar2 = this.f6556b;
        if (fVar2.f6529d == 0 && this.f6557c.v(fVar2, 8192L) == -1) {
            return -1L;
        }
        return this.f6556b.v(fVar, Math.min(j2, this.f6556b.f6529d));
    }

    @Override // h.h
    public String w(long j2) throws EOFException {
        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.f6556b.U(jA);
        }
        if (j3 < Long.MAX_VALUE && o(j3) && this.f6556b.N(j3 - 1) == 13 && o(1 + j3) && this.f6556b.N(j3) == 10) {
            return this.f6556b.U(j3);
        }
        f fVar = new f();
        f fVar2 = this.f6556b;
        fVar2.M(fVar, 0L, Math.min(32L, fVar2.f6529d));
        StringBuilder sbC = a.b.c.a.a.C("\\n not found: limit=");
        sbC.append(Math.min(this.f6556b.f6529d, j2));
        sbC.append(" content=");
        sbC.append(fVar.Q().g());
        sbC.append((char) 8230);
        throw new EOFException(sbC.toString());
    }

    @Override // h.h
    public long x(w wVar) {
        long j2 = 0;
        while (this.f6557c.v(this.f6556b, 8192L) != -1) {
            long jL = this.f6556b.L();
            if (jL > 0) {
                j2 += jL;
                ((f) wVar).h(this.f6556b, jL);
            }
        }
        f fVar = this.f6556b;
        long j3 = fVar.f6529d;
        if (j3 <= 0) {
            return j2;
        }
        long j4 = j2 + j3;
        ((f) wVar).h(fVar, j3);
        return j4;
    }

    @Override // h.h
    public short y() throws EOFException {
        A(2L);
        return this.f6556b.y();
    }
}
