package okhttp3.internal.ws; import java.io.Closeable; import java.io.IOException; import java.util.zip.Deflater; import o.C14957gcv; import o.C15111ghZ; import o.C15153gih; import o.C15155gij; import o.C15169gix; import o.C15170giy; import o.InterfaceC15168giw; import o.gbF; /* loaded from: classes6.dex */ public final class MessageDeflater implements Closeable { private final C15111ghZ deflatedBytes; private final Deflater deflater; private final C15153gih deflaterSink; private final boolean noContextTakeover; public MessageDeflater(boolean z) { this.noContextTakeover = z; C15111ghZ c15111ghZ = new C15111ghZ(); this.deflatedBytes = c15111ghZ; Deflater deflater = new Deflater(-1, true); this.deflater = deflater; this.deflaterSink = new C15153gih((InterfaceC15168giw) c15111ghZ, deflater); } @Override // java.io.Closeable, java.lang.AutoCloseable public final void close() throws IOException { this.deflaterSink.close(); } private final boolean endsWith(C15111ghZ c15111ghZ, C15155gij c15155gij) { long a = c15111ghZ.c - c15155gij.a(); C14957gcv.e(c15155gij, ""); return c15111ghZ.b(a, c15155gij, c15155gij.a()); } public final void deflate(C15111ghZ c15111ghZ) throws IOException { C15155gij c15155gij; C14957gcv.e(c15111ghZ, ""); long j = 0; if (this.deflatedBytes.c == 0) { if (this.noContextTakeover) { this.deflater.reset(); } this.deflaterSink.write(c15111ghZ, c15111ghZ.c); this.deflaterSink.flush(); C15111ghZ c15111ghZ2 = this.deflatedBytes; c15155gij = MessageDeflaterKt.EMPTY_DEFLATE_BLOCK; if (!endsWith(c15111ghZ2, c15155gij)) { this.deflatedBytes.b(0); } else { long j2 = this.deflatedBytes.c; C15111ghZ.RVV c = C15111ghZ.c(this.deflatedBytes); try { C15111ghZ.RVV rvv = c; long j3 = j2 - 4; C15111ghZ c15111ghZ3 = rvv.b; if (c15111ghZ3 == null) { throw new IllegalStateException("not attached to a buffer".toString()); } if (!rvv.a) { throw new IllegalStateException("resizeBuffer() only permitted for read/write buffers".toString()); } long j4 = c15111ghZ3.c; if (j3 <= j4) { if (j3 < 0) { throw new IllegalArgumentException("newSize < 0: ".concat(String.valueOf(j3)).toString()); } long j5 = j4 - j3; while (true) { if (j5 <= 0) { break; } C15170giy c15170giy = c15111ghZ3.d; C14957gcv.e(c15170giy); C15170giy c15170giy2 = c15170giy.h; C14957gcv.e(c15170giy2); long j6 = c15170giy2.d - c15170giy2.e; if (j6 <= j5) { c15111ghZ3.d = c15170giy2.d(); C15169gix.e(c15170giy2); j5 -= j6; } else { c15170giy2.d -= (int) j5; break; } } rvv.f = null; rvv.d = j3; rvv.c = null; rvv.g = -1; rvv.e = -1; } else if (j3 > j4) { long j7 = j3 - j4; int i = 1; boolean z = true; while (j7 > j) { C15170giy d = c15111ghZ3.d(i); int min = (int) Math.min(j7, 8192 - d.d); d.d += min; j7 -= min; if (z) { rvv.f = d; rvv.d = j4; rvv.c = d.c; rvv.g = d.d - min; rvv.e = d.d; z = false; } j = 0; i = 1; } } c15111ghZ3.c = j3; gbF.d(c, null); } finally { } } C15111ghZ c15111ghZ4 = this.deflatedBytes; c15111ghZ.write(c15111ghZ4, c15111ghZ4.c); return; } throw new IllegalArgumentException("Failed requirement.".toString()); } }