package m5; import h5.a0; import h5.b0; import h5.f0; import h5.g0; import h5.h0; import h5.i0; import h5.l0; import h5.n0; import h5.o0; import h5.p0; import h5.x; import java.io.EOFException; import java.io.IOException; import java.net.ProtocolException; import java.util.concurrent.TimeUnit; import l5.h; import l5.i; import l5.k; /* loaded from: classes2.dex */ public final class a implements l5.c { /* renamed from: a, reason: collision with root package name */ final f0 f11853a; /* renamed from: b, reason: collision with root package name */ final k5.g f11854b; /* renamed from: c, reason: collision with root package name */ final h5.f f11855c; /* renamed from: d, reason: collision with root package name */ final h5.e f11856d; /* renamed from: e, reason: collision with root package name */ int f11857e = 0; /* renamed from: f, reason: collision with root package name */ private long f11858f = 262144; private abstract class b implements o0 { /* renamed from: a, reason: collision with root package name */ protected final x f11859a; /* renamed from: b, reason: collision with root package name */ protected boolean f11860b; /* renamed from: c, reason: collision with root package name */ protected long f11861c; private b() { this.f11859a = new x(a.this.f11855c.b()); this.f11861c = 0L; } @Override // h5.o0 public long H(h5.d dVar, long j10) { try { long H = a.this.f11855c.H(dVar, j10); if (H > 0) { this.f11861c += H; } return H; } catch (IOException e10) { c(false, e10); throw e10; } } @Override // h5.o0 public p0 b() { return this.f11859a; } protected final void c(boolean z10, IOException iOException) { a aVar = a.this; int i10 = aVar.f11857e; if (i10 == 6) { return; } if (i10 != 5) { throw new IllegalStateException("state: " + a.this.f11857e); } aVar.h(this.f11859a); a aVar2 = a.this; aVar2.f11857e = 6; k5.g gVar = aVar2.f11854b; if (gVar != null) { gVar.q(!z10, aVar2, this.f11861c, iOException); } } } private final class c implements n0 { /* renamed from: a, reason: collision with root package name */ private final x f11863a; /* renamed from: b, reason: collision with root package name */ private boolean f11864b; c() { this.f11863a = new x(a.this.f11856d.b()); } @Override // h5.n0 public p0 b() { return this.f11863a; } @Override // h5.n0, java.io.Closeable, java.lang.AutoCloseable public synchronized void close() { if (this.f11864b) { return; } this.f11864b = true; a.this.f11856d.j("0\r\n\r\n"); a.this.h(this.f11863a); a.this.f11857e = 3; } @Override // h5.n0, java.io.Flushable public synchronized void flush() { if (this.f11864b) { return; } a.this.f11856d.flush(); } @Override // h5.n0 public void k0(h5.d dVar, long j10) { if (this.f11864b) { throw new IllegalStateException("closed"); } if (j10 == 0) { return; } a.this.f11856d.m(j10); a.this.f11856d.j("\r\n"); a.this.f11856d.k0(dVar, j10); a.this.f11856d.j("\r\n"); } } private class d extends b { /* renamed from: e, reason: collision with root package name */ private final b0 f11866e; /* renamed from: f, reason: collision with root package name */ private long f11867f; /* renamed from: g, reason: collision with root package name */ private boolean f11868g; d(b0 b0Var) { super(); this.f11867f = -1L; this.f11868g = true; this.f11866e = b0Var; } private void z() { if (this.f11867f != -1) { a.this.f11855c.n(); } try { this.f11867f = a.this.f11855c.u(); String trim = a.this.f11855c.n().trim(); if (this.f11867f < 0 || !(trim.isEmpty() || trim.startsWith(";"))) { throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f11867f + trim + "\""); } if (this.f11867f == 0) { this.f11868g = false; l5.e.f(a.this.f11853a.j(), this.f11866e, a.this.n()); c(true, null); } } catch (NumberFormatException e10) { throw new ProtocolException(e10.toString()); } } @Override // m5.a.b, h5.o0 public long H(h5.d dVar, long j10) { if (j10 < 0) { throw new IllegalArgumentException("byteCount < 0: " + j10); } if (this.f11860b) { throw new IllegalStateException("closed"); } if (!this.f11868g) { return -1L; } long j11 = this.f11867f; if (j11 == 0 || j11 == -1) { z(); if (!this.f11868g) { return -1L; } } long H = super.H(dVar, Math.min(j10, this.f11867f)); if (H != -1) { this.f11867f -= H; return H; } ProtocolException protocolException = new ProtocolException("unexpected end of stream"); c(false, protocolException); throw protocolException; } @Override // h5.o0, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f11860b) { return; } if (this.f11868g && !i5.c.p(this, 100, TimeUnit.MILLISECONDS)) { c(false, null); } this.f11860b = true; } } private final class e implements n0 { /* renamed from: a, reason: collision with root package name */ private final x f11870a; /* renamed from: b, reason: collision with root package name */ private boolean f11871b; /* renamed from: c, reason: collision with root package name */ private long f11872c; e(long j10) { this.f11870a = new x(a.this.f11856d.b()); this.f11872c = j10; } @Override // h5.n0 public p0 b() { return this.f11870a; } @Override // h5.n0, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f11871b) { return; } this.f11871b = true; if (this.f11872c > 0) { throw new ProtocolException("unexpected end of stream"); } a.this.h(this.f11870a); a.this.f11857e = 3; } @Override // h5.n0, java.io.Flushable public void flush() { if (this.f11871b) { return; } a.this.f11856d.flush(); } @Override // h5.n0 public void k0(h5.d dVar, long j10) { if (this.f11871b) { throw new IllegalStateException("closed"); } i5.c.i(dVar.u0(), 0L, j10); if (j10 <= this.f11872c) { a.this.f11856d.k0(dVar, j10); this.f11872c -= j10; return; } throw new ProtocolException("expected " + this.f11872c + " bytes but received " + j10); } } private class f extends b { /* renamed from: e, reason: collision with root package name */ private long f11874e; f(a aVar, long j10) { super(); this.f11874e = j10; if (j10 == 0) { c(true, null); } } @Override // m5.a.b, h5.o0 public long H(h5.d dVar, long j10) { if (j10 < 0) { throw new IllegalArgumentException("byteCount < 0: " + j10); } if (this.f11860b) { throw new IllegalStateException("closed"); } long j11 = this.f11874e; if (j11 == 0) { return -1L; } long H = super.H(dVar, Math.min(j11, j10)); if (H == -1) { ProtocolException protocolException = new ProtocolException("unexpected end of stream"); c(false, protocolException); throw protocolException; } long j12 = this.f11874e - H; this.f11874e = j12; if (j12 == 0) { c(true, null); } return H; } @Override // h5.o0, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f11860b) { return; } if (this.f11874e != 0 && !i5.c.p(this, 100, TimeUnit.MILLISECONDS)) { c(false, null); } this.f11860b = true; } } private class g extends b { /* renamed from: e, reason: collision with root package name */ private boolean f11875e; g(a aVar) { super(); } @Override // m5.a.b, h5.o0 public long H(h5.d dVar, long j10) { if (j10 < 0) { throw new IllegalArgumentException("byteCount < 0: " + j10); } if (this.f11860b) { throw new IllegalStateException("closed"); } if (this.f11875e) { return -1L; } long H = super.H(dVar, j10); if (H != -1) { return H; } this.f11875e = true; c(true, null); return -1L; } @Override // h5.o0, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f11860b) { return; } if (!this.f11875e) { c(false, null); } this.f11860b = true; } } public a(f0 f0Var, k5.g gVar, h5.f fVar, h5.e eVar) { this.f11853a = f0Var; this.f11854b = gVar; this.f11855c = fVar; this.f11856d = eVar; } private String g() { String i10 = this.f11855c.i(this.f11858f); this.f11858f -= i10.length(); return i10; } @Override // l5.c public void a() { this.f11856d.flush(); } @Override // l5.c public i0.a b(boolean z10) { int i10 = this.f11857e; if (i10 != 1 && i10 != 3) { throw new IllegalStateException("state: " + this.f11857e); } try { k a10 = k.a(g()); i0.a i11 = new i0.a().m(a10.f11625a).g(a10.f11626b).j(a10.f11627c).i(n()); if (z10 && a10.f11626b == 100) { return null; } if (a10.f11626b == 100) { this.f11857e = 3; return i11; } this.f11857e = 4; return i11; } catch (EOFException e10) { throw new IOException("unexpected end of stream on " + this.f11854b, e10); } } @Override // l5.c public void c() { this.f11856d.flush(); } @Override // l5.c public void d(h0 h0Var) { o(h0Var.d(), i.b(h0Var, this.f11854b.i().q().b().type())); } @Override // l5.c public n0 e(h0 h0Var, long j10) { if ("chunked".equalsIgnoreCase(h0Var.c("Transfer-Encoding"))) { return i(); } if (j10 != -1) { return k(j10); } throw new IllegalStateException("Cannot stream a mobonRequest body without chunked encoding or a known content length!"); } @Override // l5.c public l0 f(i0 i0Var) { k5.g gVar = this.f11854b; gVar.f11257f.q(gVar.f11256e); String O = i0Var.O("Content-Type"); if (!l5.e.d(i0Var)) { return new h(O, 0L, g0.f(l(0L))); } if ("chunked".equalsIgnoreCase(i0Var.O("Transfer-Encoding"))) { return new h(O, -1L, g0.f(j(i0Var.j0().h()))); } long c10 = l5.e.c(i0Var); return c10 != -1 ? new h(O, c10, g0.f(l(c10))) : new h(O, -1L, g0.f(m())); } void h(x xVar) { p0 i10 = xVar.i(); xVar.j(p0.f10264d); i10.a(); i10.b(); } public n0 i() { if (this.f11857e == 1) { this.f11857e = 2; return new c(); } throw new IllegalStateException("state: " + this.f11857e); } public o0 j(b0 b0Var) { if (this.f11857e == 4) { this.f11857e = 5; return new d(b0Var); } throw new IllegalStateException("state: " + this.f11857e); } public n0 k(long j10) { if (this.f11857e == 1) { this.f11857e = 2; return new e(j10); } throw new IllegalStateException("state: " + this.f11857e); } public o0 l(long j10) { if (this.f11857e == 4) { this.f11857e = 5; return new f(this, j10); } throw new IllegalStateException("state: " + this.f11857e); } public o0 m() { if (this.f11857e != 4) { throw new IllegalStateException("state: " + this.f11857e); } k5.g gVar = this.f11854b; if (gVar == null) { throw new IllegalStateException("streamAllocation == null"); } this.f11857e = 5; gVar.l(); return new g(this); } public a0 n() { a0.a aVar = new a0.a(); while (true) { String g10 = g(); if (g10.length() == 0) { return aVar.e(); } i5.a.f10869a.a(aVar, g10); } } public void o(a0 a0Var, String str) { if (this.f11857e != 0) { throw new IllegalStateException("state: " + this.f11857e); } this.f11856d.j(str).j("\r\n"); int e10 = a0Var.e(); for (int i10 = 0; i10 < e10; i10++) { this.f11856d.j(a0Var.c(i10)).j(": ").j(a0Var.f(i10)).j("\r\n"); } this.f11856d.j("\r\n"); this.f11857e = 1; } }