package okio;

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

/* JADX INFO: loaded from: classes2.dex */
public final class o implements E {

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

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

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

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

    public o(h source, Inflater inflater) {
        kotlin.jvm.internal.l.e(source, "source");
        kotlin.jvm.internal.l.e(inflater, "inflater");
        this.f6047r = source;
        this.f6048x = inflater;
    }

    private final void d() {
        int i2 = this.f6045b;
        if (i2 == 0) {
            return;
        }
        int remaining = i2 - this.f6048x.getRemaining();
        this.f6045b -= remaining;
        this.f6047r.skip(remaining);
    }

    public final long a(C0688f sink, long j2) throws IOException {
        kotlin.jvm.internal.l.e(sink, "sink");
        if (!(j2 >= 0)) {
            throw new IllegalArgumentException(("byteCount < 0: " + j2).toString());
        }
        if (this.f6046g) {
            throw new IllegalStateException("closed");
        }
        if (j2 == 0) {
            return 0L;
        }
        try {
            y yVarK = sink.K(1);
            int iMin = (int) Math.min(j2, 8192 - yVarK.f6073c);
            b();
            int iInflate = this.f6048x.inflate(yVarK.f6071a, yVarK.f6073c, iMin);
            d();
            if (iInflate > 0) {
                yVarK.f6073c += iInflate;
                long j3 = iInflate;
                sink.F(sink.G() + j3);
                return j3;
            }
            if (yVarK.f6072b == yVarK.f6073c) {
                sink.f6021b = yVarK.b();
                A.b(yVarK);
            }
            return 0L;
        } catch (DataFormatException e2) {
            throw new IOException(e2);
        }
    }

    public final boolean b() {
        if (!this.f6048x.needsInput()) {
            return false;
        }
        if (this.f6047r.k()) {
            return true;
        }
        y yVar = this.f6047r.getBuffer().f6021b;
        kotlin.jvm.internal.l.b(yVar);
        int i2 = yVar.f6073c;
        int i3 = yVar.f6072b;
        int i4 = i2 - i3;
        this.f6045b = i4;
        this.f6048x.setInput(yVar.f6071a, i3, i4);
        return false;
    }

    @Override // okio.E, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        if (this.f6046g) {
            return;
        }
        this.f6048x.end();
        this.f6046g = true;
        this.f6047r.close();
    }

    @Override // okio.E
    public long read(C0688f sink, long j2) throws IOException {
        kotlin.jvm.internal.l.e(sink, "sink");
        do {
            long jA = a(sink, j2);
            if (jA > 0) {
                return jA;
            }
            if (this.f6048x.finished() || this.f6048x.needsDictionary()) {
                return -1L;
            }
        } while (!this.f6047r.k());
        throw new EOFException("source exhausted prematurely");
    }

    @Override // okio.E
    public F timeout() {
        return this.f6047r.timeout();
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public o(E source, Inflater inflater) {
        this(q.d(source), inflater);
        kotlin.jvm.internal.l.e(source, "source");
        kotlin.jvm.internal.l.e(inflater, "inflater");
    }
}
