package j;

import java.io.IOException;
import java.util.zip.Deflater;
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;

/* JADX INFO: compiled from: DeflaterSink.kt */
/* JADX INFO: loaded from: classes.dex */
public final class i implements z {

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final f f4617e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Deflater f4618f;

    public i(z zVar, Deflater deflater) {
        g.h0.c.l.f(zVar, "sink");
        g.h0.c.l.f(deflater, "deflater");
        f fVarS = f.a.y.a.s(zVar);
        g.h0.c.l.f(fVarS, "sink");
        g.h0.c.l.f(deflater, "deflater");
        this.f4617e = fVarS;
        this.f4618f = deflater;
    }

    @IgnoreJRERequirement
    public final void a(boolean z) throws IOException {
        w wVarX;
        int iDeflate;
        d buffer = this.f4617e.getBuffer();
        while (true) {
            wVarX = buffer.x(1);
            if (z) {
                Deflater deflater = this.f4618f;
                byte[] bArr = wVarX.a;
                int i2 = wVarX.f4650c;
                iDeflate = deflater.deflate(bArr, i2, 8192 - i2, 2);
            } else {
                Deflater deflater2 = this.f4618f;
                byte[] bArr2 = wVarX.a;
                int i3 = wVarX.f4650c;
                iDeflate = deflater2.deflate(bArr2, i3, 8192 - i3);
            }
            if (iDeflate > 0) {
                wVarX.f4650c += iDeflate;
                buffer.f4601e += (long) iDeflate;
                this.f4617e.emitCompleteSegments();
            } else if (this.f4618f.needsInput()) {
                break;
            }
        }
        if (wVarX.f4649b == wVarX.f4650c) {
            buffer.f4600b = wVarX.a();
            x.a(wVarX);
        }
    }

    @Override // j.z, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws Throwable {
        if (this.f4616b) {
            return;
        }
        Throwable th = null;
        try {
            this.f4618f.finish();
            a(false);
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f4618f.end();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        try {
            this.f4617e.close();
        } catch (Throwable th4) {
            if (th == null) {
                th = th4;
            }
        }
        this.f4616b = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // j.z, java.io.Flushable
    public void flush() throws IOException {
        a(true);
        this.f4617e.flush();
    }

    @Override // j.z
    public c0 timeout() {
        return this.f4617e.timeout();
    }

    public String toString() {
        StringBuilder sbB = d.b.a.a.a.B("DeflaterSink(");
        sbB.append(this.f4617e);
        sbB.append(')');
        return sbB.toString();
    }

    @Override // j.z
    public void write(d dVar, long j2) throws IOException {
        g.h0.c.l.f(dVar, "source");
        f.a.y.a.z(dVar.f4601e, 0L, j2);
        while (j2 > 0) {
            w wVar = dVar.f4600b;
            g.h0.c.l.c(wVar);
            int iMin = (int) Math.min(j2, wVar.f4650c - wVar.f4649b);
            this.f4618f.setInput(wVar.a, wVar.f4649b, iMin);
            a(false);
            long j3 = iMin;
            dVar.f4601e -= j3;
            int i2 = wVar.f4649b + iMin;
            wVar.f4649b = i2;
            if (i2 == wVar.f4650c) {
                dVar.f4600b = wVar.a();
                x.a(wVar);
            }
            j2 -= j3;
        }
    }
}
