package i;

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 j implements b0 {
    private boolean b;

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

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

    public j(g gVar, Deflater deflater) {
        h.x.d.k.d(gVar, "sink");
        h.x.d.k.d(deflater, "deflater");
        this.f1030e = gVar;
        this.f1031f = deflater;
    }

    @IgnoreJRERequirement
    private final void a(boolean z) throws IOException {
        y yVarP;
        int iDeflate;
        f fVarC = this.f1030e.c();
        while (true) {
            yVarP = fVarC.P(1);
            if (z) {
                Deflater deflater = this.f1031f;
                byte[] bArr = yVarP.a;
                int i2 = yVarP.f1053c;
                iDeflate = deflater.deflate(bArr, i2, 8192 - i2, 2);
            } else {
                Deflater deflater2 = this.f1031f;
                byte[] bArr2 = yVarP.a;
                int i3 = yVarP.f1053c;
                iDeflate = deflater2.deflate(bArr2, i3, 8192 - i3);
            }
            if (iDeflate > 0) {
                yVarP.f1053c += iDeflate;
                fVarC.H(fVarC.J() + ((long) iDeflate));
                this.f1030e.L();
            } else if (this.f1031f.needsInput()) {
                break;
            }
        }
        if (yVarP.b == yVarP.f1053c) {
            fVarC.b = yVarP.b();
            z.b(yVarP);
        }
    }

    public final void b() throws IOException {
        this.f1031f.finish();
        a(false);
    }

    @Override // i.b0, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
    public void close() throws Throwable {
        if (this.b) {
            return;
        }
        Throwable th = null;
        try {
            b();
        } catch (Throwable th2) {
            th = th2;
        }
        try {
            this.f1031f.end();
        } catch (Throwable th3) {
            if (th == null) {
                th = th3;
            }
        }
        try {
            this.f1030e.close();
        } catch (Throwable th4) {
            if (th == null) {
                th = th4;
            }
        }
        this.b = true;
        if (th != null) {
            throw th;
        }
    }

    @Override // i.b0, java.io.Flushable
    public void flush() throws IOException {
        a(true);
        this.f1030e.flush();
    }

    @Override // i.b0
    public e0 timeout() {
        return this.f1030e.timeout();
    }

    public String toString() {
        return "DeflaterSink(" + this.f1030e + ')';
    }

    @Override // i.b0
    public void write(f fVar, long j2) throws IOException {
        h.x.d.k.d(fVar, "source");
        c.b(fVar.J(), 0L, j2);
        while (j2 > 0) {
            y yVar = fVar.b;
            h.x.d.k.b(yVar);
            int iMin = (int) Math.min(j2, yVar.f1053c - yVar.b);
            this.f1031f.setInput(yVar.a, yVar.b, iMin);
            a(false);
            long j3 = iMin;
            fVar.H(fVar.J() - j3);
            int i2 = yVar.b + iMin;
            yVar.b = i2;
            if (i2 == yVar.f1053c) {
                fVar.b = yVar.b();
                z.b(yVar);
            }
            j2 -= j3;
        }
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public j(b0 b0Var, Deflater deflater) {
        this(q.c(b0Var), deflater);
        h.x.d.k.d(b0Var, "sink");
        h.x.d.k.d(deflater, "deflater");
    }
}
