package f;

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 t {

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

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

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

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

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

    public j(t tVar) {
        if (tVar == null) {
            throw new IllegalArgumentException("source == null");
        }
        Inflater inflater = new Inflater(true);
        this.f3086e = inflater;
        e eVarD = m.d(tVar);
        this.f3085d = eVarD;
        this.f3087f = new l(eVarD, inflater);
    }

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

    private void c() throws IOException {
        this.f3085d.c0(10L);
        byte bI = this.f3085d.buffer().i(3L);
        boolean z = ((bI >> 1) & 1) == 1;
        if (z) {
            g(this.f3085d.buffer(), 0L, 10L);
        }
        b("ID1ID2", 8075, this.f3085d.readShort());
        this.f3085d.skip(8L);
        if (((bI >> 2) & 1) == 1) {
            this.f3085d.c0(2L);
            if (z) {
                g(this.f3085d.buffer(), 0L, 2L);
            }
            long jW = this.f3085d.buffer().W();
            this.f3085d.c0(jW);
            if (z) {
                g(this.f3085d.buffer(), 0L, jW);
            }
            this.f3085d.skip(jW);
        }
        if (((bI >> 3) & 1) == 1) {
            long jF0 = this.f3085d.f0((byte) 0);
            if (jF0 == -1) {
                throw new EOFException();
            }
            if (z) {
                g(this.f3085d.buffer(), 0L, jF0 + 1);
            }
            this.f3085d.skip(jF0 + 1);
        }
        if (((bI >> 4) & 1) == 1) {
            long jF02 = this.f3085d.f0((byte) 0);
            if (jF02 == -1) {
                throw new EOFException();
            }
            if (z) {
                g(this.f3085d.buffer(), 0L, jF02 + 1);
            }
            this.f3085d.skip(jF02 + 1);
        }
        if (z) {
            b("FHCRC", this.f3085d.W(), (short) this.f3088g.getValue());
            this.f3088g.reset();
        }
    }

    private void e() throws IOException {
        b("CRC", this.f3085d.Q(), (int) this.f3088g.getValue());
        b("ISIZE", this.f3085d.Q(), (int) this.f3086e.getBytesWritten());
    }

    private void g(c cVar, long j, long j2) {
        p pVar = cVar.f3075d;
        while (true) {
            int i = pVar.f3110c;
            int i2 = pVar.f3109b;
            if (j < i - i2) {
                break;
            }
            j -= (long) (i - i2);
            pVar = pVar.f3113f;
        }
        while (j2 > 0) {
            int i3 = (int) (((long) pVar.f3109b) + j);
            int iMin = (int) Math.min(pVar.f3110c - i3, j2);
            this.f3088g.update(pVar.f3108a, i3, iMin);
            j2 -= (long) iMin;
            pVar = pVar.f3113f;
            j = 0;
        }
    }

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

    @Override // f.t
    public long read(c cVar, long j) throws IOException {
        if (j < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j);
        }
        if (j == 0) {
            return 0L;
        }
        if (this.f3084c == 0) {
            c();
            this.f3084c = 1;
        }
        if (this.f3084c == 1) {
            long j2 = cVar.f3076e;
            long j3 = this.f3087f.read(cVar, j);
            if (j3 != -1) {
                g(cVar, j2, j3);
                return j3;
            }
            this.f3084c = 2;
        }
        if (this.f3084c == 2) {
            e();
            this.f3084c = 3;
            if (!this.f3085d.m()) {
                throw new IOException("gzip finished without exhausting source");
            }
        }
        return -1L;
    }

    @Override // f.t
    public u timeout() {
        return this.f3085d.timeout();
    }
}
