package h;

import java.io.EOFException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Inflater;

/* JADX INFO: compiled from: GzipSource.java */
/* JADX INFO: loaded from: classes.dex */
public final class j implements s {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final e f4186f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final Inflater f4187g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final k f4188h;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private int f4185b = 0;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final CRC32 f4189i = new CRC32();

    public j(s sVar) {
        if (sVar == null) {
            throw new IllegalArgumentException("source == null");
        }
        this.f4187g = new Inflater(true);
        e eVarD = l.d(sVar);
        this.f4186f = eVarD;
        this.f4188h = new k(eVarD, this.f4187g);
    }

    private void a(String str, int i2, int i3) throws IOException {
        if (i3 != i2) {
            throw new IOException(String.format("%s: actual 0x%08x != expected 0x%08x", str, Integer.valueOf(i3), Integer.valueOf(i2)));
        }
    }

    private void d() throws IOException {
        this.f4186f.X(10L);
        byte bK = this.f4186f.buffer().k(3L);
        boolean z = ((bK >> 1) & 1) == 1;
        if (z) {
            g(this.f4186f.buffer(), 0L, 10L);
        }
        a("ID1ID2", 8075, this.f4186f.readShort());
        this.f4186f.skip(8L);
        if (((bK >> 2) & 1) == 1) {
            this.f4186f.X(2L);
            if (z) {
                g(this.f4186f.buffer(), 0L, 2L);
            }
            long jI = this.f4186f.buffer().I();
            this.f4186f.X(jI);
            if (z) {
                g(this.f4186f.buffer(), 0L, jI);
            }
            this.f4186f.skip(jI);
        }
        if (((bK >> 3) & 1) == 1) {
            long jC0 = this.f4186f.c0((byte) 0);
            if (jC0 == -1) {
                throw new EOFException();
            }
            if (z) {
                g(this.f4186f.buffer(), 0L, jC0 + 1);
            }
            this.f4186f.skip(jC0 + 1);
        }
        if (((bK >> 4) & 1) == 1) {
            long jC02 = this.f4186f.c0((byte) 0);
            if (jC02 == -1) {
                throw new EOFException();
            }
            if (z) {
                g(this.f4186f.buffer(), 0L, jC02 + 1);
            }
            this.f4186f.skip(jC02 + 1);
        }
        if (z) {
            a("FHCRC", this.f4186f.I(), (short) this.f4189i.getValue());
            this.f4189i.reset();
        }
    }

    private void f() throws IOException {
        a("CRC", this.f4186f.v(), (int) this.f4189i.getValue());
        a("ISIZE", this.f4186f.v(), (int) this.f4187g.getBytesWritten());
    }

    private void g(c cVar, long j2, long j3) {
        o oVar = cVar.f4176b;
        while (true) {
            int i2 = oVar.f4206c;
            int i3 = oVar.f4205b;
            if (j2 < i2 - i3) {
                break;
            }
            j2 -= (long) (i2 - i3);
            oVar = oVar.f4209f;
        }
        while (j3 > 0) {
            int i4 = (int) (((long) oVar.f4205b) + j2);
            int iMin = (int) Math.min(oVar.f4206c - i4, j3);
            this.f4189i.update(oVar.a, i4, iMin);
            j3 -= (long) iMin;
            oVar = oVar.f4209f;
            j2 = 0;
        }
    }

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

    @Override // h.s
    public long read(c cVar, long j2) throws IOException {
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (j2 == 0) {
            return 0L;
        }
        if (this.f4185b == 0) {
            d();
            this.f4185b = 1;
        }
        if (this.f4185b == 1) {
            long j3 = cVar.f4177f;
            long j4 = this.f4188h.read(cVar, j2);
            if (j4 != -1) {
                g(cVar, j3, j4);
                return j4;
            }
            this.f4185b = 2;
        }
        if (this.f4185b == 2) {
            f();
            this.f4185b = 3;
            if (!this.f4186f.z()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // h.s
    public t timeout() {
        return this.f4186f.timeout();
    }
}
