package h;

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f4192g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private boolean f4193h;

    k(e eVar, Inflater inflater) {
        if (eVar == null) {
            throw new IllegalArgumentException("source == null");
        }
        if (inflater == null) {
            throw new IllegalArgumentException("inflater == null");
        }
        this.f4190b = eVar;
        this.f4191f = inflater;
    }

    private void d() throws IOException {
        int i2 = this.f4192g;
        if (i2 == 0) {
            return;
        }
        int remaining = i2 - this.f4191f.getRemaining();
        this.f4192g -= remaining;
        this.f4190b.skip(remaining);
    }

    public boolean a() throws IOException {
        if (!this.f4191f.needsInput()) {
            return false;
        }
        d();
        if (this.f4191f.getRemaining() != 0) {
            throw new IllegalStateException("?");
        }
        if (this.f4190b.z()) {
            return true;
        }
        o oVar = this.f4190b.buffer().f4176b;
        int i2 = oVar.f4206c;
        int i3 = oVar.f4205b;
        int i4 = i2 - i3;
        this.f4192g = i4;
        this.f4191f.setInput(oVar.a, i3, i4);
        return false;
    }

    @Override // h.s, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.f4193h) {
            return;
        }
        this.f4191f.end();
        this.f4193h = true;
        this.f4190b.close();
    }

    @Override // h.s
    public long read(c cVar, long j2) throws IOException {
        boolean zA;
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (this.f4193h) {
            throw new IllegalStateException("closed");
        }
        if (j2 == 0) {
            return 0L;
        }
        do {
            zA = a();
            try {
                o oVarF = cVar.F(1);
                int iInflate = this.f4191f.inflate(oVarF.a, oVarF.f4206c, 8192 - oVarF.f4206c);
                if (iInflate > 0) {
                    oVarF.f4206c += iInflate;
                    long j3 = iInflate;
                    cVar.f4177f += j3;
                    return j3;
                }
                if (!this.f4191f.finished() && !this.f4191f.needsDictionary()) {
                }
                d();
                if (oVarF.f4205b != oVarF.f4206c) {
                    return -1L;
                }
                cVar.f4176b = oVarF.b();
                p.a(oVarF);
                return -1L;
            } catch (DataFormatException e2) {
                throw new IOException(e2);
            }
        } while (!zA);
        throw new EOFException("source exhausted prematurely");
    }

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