package h;

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

/* JADX INFO: loaded from: classes.dex */
public final class n implements x {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final h f6558b;

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

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

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

    public n(h hVar, Inflater inflater) {
        this.f6558b = hVar;
        this.f6559c = inflater;
    }

    public final void a() {
        int i2 = this.f6560d;
        if (i2 == 0) {
            return;
        }
        int remaining = i2 - this.f6559c.getRemaining();
        this.f6560d -= remaining;
        this.f6558b.l(remaining);
    }

    @Override // h.x, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this.f6561e) {
            return;
        }
        this.f6559c.end();
        this.f6561e = true;
        this.f6558b.close();
    }

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

    @Override // h.x
    public long v(f fVar, long j2) throws IOException {
        boolean z;
        if (j2 < 0) {
            throw new IllegalArgumentException("byteCount < 0: " + j2);
        }
        if (this.f6561e) {
            throw new IllegalStateException("closed");
        }
        if (j2 == 0) {
            return 0L;
        }
        do {
            z = false;
            if (this.f6559c.needsInput()) {
                a();
                if (this.f6559c.getRemaining() != 0) {
                    throw new IllegalStateException("?");
                }
                if (this.f6558b.q()) {
                    z = true;
                } else {
                    t tVar = this.f6558b.b().f6542c;
                    int i2 = tVar.f6576c;
                    int i3 = tVar.f6575b;
                    int i4 = i2 - i3;
                    this.f6560d = i4;
                    this.f6559c.setInput(tVar.f6574a, i3, i4);
                }
            }
            try {
                t tVarV = fVar.V(1);
                int iInflate = this.f6559c.inflate(tVarV.f6574a, tVarV.f6576c, (int) Math.min(j2, 8192 - tVarV.f6576c));
                if (iInflate > 0) {
                    tVarV.f6576c += iInflate;
                    long j3 = iInflate;
                    fVar.f6543d += j3;
                    return j3;
                }
                if (!this.f6559c.finished() && !this.f6559c.needsDictionary()) {
                }
                a();
                if (tVarV.f6575b != tVarV.f6576c) {
                    return -1L;
                }
                fVar.f6542c = tVarV.a();
                u.a(tVarV);
                return -1L;
            } catch (DataFormatException e2) {
                throw new IOException(e2);
            }
        } while (!z);
        throw new EOFException("source exhausted prematurely");
    }
}
