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 f6544b;

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

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

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

    public n(h hVar, Inflater inflater) {
        this.f6544b = hVar;
        this.f6545c = inflater;
    }

    public final void a() {
        int i2 = this.f6546d;
        if (i2 == 0) {
            return;
        }
        int remaining = i2 - this.f6545c.getRemaining();
        this.f6546d -= remaining;
        this.f6544b.l(remaining);
    }

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

    @Override // h.x
    public y e() {
        return this.f6544b.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.f6547e) {
            throw new IllegalStateException("closed");
        }
        if (j2 == 0) {
            return 0L;
        }
        do {
            z = false;
            if (this.f6545c.needsInput()) {
                a();
                if (this.f6545c.getRemaining() != 0) {
                    throw new IllegalStateException("?");
                }
                if (this.f6544b.q()) {
                    z = true;
                } else {
                    t tVar = this.f6544b.b().f6528c;
                    int i2 = tVar.f6562c;
                    int i3 = tVar.f6561b;
                    int i4 = i2 - i3;
                    this.f6546d = i4;
                    this.f6545c.setInput(tVar.f6560a, i3, i4);
                }
            }
            try {
                t tVarV = fVar.V(1);
                int iInflate = this.f6545c.inflate(tVarV.f6560a, tVarV.f6562c, (int) Math.min(j2, 8192 - tVarV.f6562c));
                if (iInflate > 0) {
                    tVarV.f6562c += iInflate;
                    long j3 = iInflate;
                    fVar.f6529d += j3;
                    return j3;
                }
                if (!this.f6545c.finished() && !this.f6545c.needsDictionary()) {
                }
                a();
                if (tVarV.f6561b != tVarV.f6562c) {
                    return -1L;
                }
                fVar.f6528c = tVarV.a();
                u.a(tVarV);
                return -1L;
            } catch (DataFormatException e2) {
                throw new IOException(e2);
            }
        } while (!z);
        throw new EOFException("source exhausted prematurely");
    }
}
