package o; import java.io.IOException; import java.util.zip.Deflater; /* loaded from: classes6.dex */ public final class giN implements giX { private final giO a; private final Deflater c; private boolean e; public giN(giX gix, Deflater deflater) { this(C15145giV.e(gix), deflater); } private giN(giO gio, Deflater deflater) { if (gio == null) { throw new IllegalArgumentException("source == null"); } if (deflater == null) { throw new IllegalArgumentException("inflater == null"); } this.a = gio; this.c = deflater; } @Override // o.giX public final void write(giM gim, long j) throws IOException { C15183gjd.e(gim.a, 0L, j); while (j > 0) { C15186gjg c15186gjg = gim.c; int min = (int) Math.min(j, c15186gjg.c - c15186gjg.g); this.c.setInput(c15186gjg.a, c15186gjg.g, min); b(false); long j2 = min; gim.a -= j2; c15186gjg.g += min; if (c15186gjg.g == c15186gjg.c) { gim.c = c15186gjg.b(); C15184gje.c(c15186gjg); } j -= j2; } } private void b(boolean z) throws IOException { C15186gjg e; int deflate; giM b = this.a.b(); while (true) { e = b.e(1); if (z) { deflate = this.c.deflate(e.a, e.c, 2048 - e.c, 2); } else { deflate = this.c.deflate(e.a, e.c, 2048 - e.c); } if (deflate > 0) { e.c += deflate; b.a += deflate; this.a.c(); } else if (this.c.needsInput()) { break; } } if (e.g == e.c) { b.c = e.b(); C15184gje.c(e); } } @Override // o.giX, java.io.Flushable public final void flush() throws IOException { b(true); this.a.flush(); } @Override // o.giX, java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { if (this.e) { return; } try { this.c.finish(); b(false); th = null; } catch (Throwable th) { th = th; } try { this.c.end(); } catch (Throwable th2) { if (th == null) { th = th2; } } try { this.a.close(); } catch (Throwable th3) { if (th == null) { th = th3; } } this.e = true; if (th != null) { C15183gjd.e(th); } } @Override // o.giX public final giY timeout() { return this.a.timeout(); } public final String toString() { StringBuilder sb = new StringBuilder("DeflaterSink("); sb.append(this.a); sb.append(")"); return sb.toString(); } }