package s9; import com.google.android.gms.common.api.Api; import java.io.EOFException; import java.io.IOException; import java.net.ProtocolException; import java.util.concurrent.TimeUnit; import n9.b0; import n9.e0; import n9.g0; import n9.x; import n9.y; import r9.i; import r9.k; import x9.j; import x9.u; import x9.v; import x9.w; /* loaded from: classes2.dex */ public final class a implements r9.c { /* renamed from: a, reason: collision with root package name */ private final b0 f14112a; /* renamed from: b, reason: collision with root package name */ private final q9.e f14113b; /* renamed from: c, reason: collision with root package name */ private final x9.e f14114c; /* renamed from: d, reason: collision with root package name */ private final x9.d f14115d; /* renamed from: e, reason: collision with root package name */ private int f14116e = 0; /* renamed from: f, reason: collision with root package name */ private long f14117f = 262144; /* renamed from: g, reason: collision with root package name */ private x f14118g; private abstract class b implements v { /* renamed from: a, reason: collision with root package name */ protected final j f14119a; /* renamed from: b, reason: collision with root package name */ protected boolean f14120b; private b() { this.f14119a = new j(a.this.f14114c.b()); } @Override // x9.v public w b() { return this.f14119a; } final void c() { if (a.this.f14116e == 6) { return; } if (a.this.f14116e == 5) { a.this.s(this.f14119a); a.this.f14116e = 6; } else { throw new IllegalStateException("state: " + a.this.f14116e); } } @Override // x9.v public long s0(x9.c cVar, long j10) { try { return a.this.f14114c.s0(cVar, j10); } catch (IOException e10) { a.this.f14113b.p(); c(); throw e10; } } } private final class c implements u { /* renamed from: a, reason: collision with root package name */ private final j f14122a; /* renamed from: b, reason: collision with root package name */ private boolean f14123b; c() { this.f14122a = new j(a.this.f14115d.b()); } @Override // x9.u public w b() { return this.f14122a; } @Override // x9.u, java.io.Closeable, java.lang.AutoCloseable public synchronized void close() { if (this.f14123b) { return; } this.f14123b = true; a.this.f14115d.j("0\r\n\r\n"); a.this.s(this.f14122a); a.this.f14116e = 3; } @Override // x9.u, java.io.Flushable public synchronized void flush() { if (this.f14123b) { return; } a.this.f14115d.flush(); } @Override // x9.u public void t0(x9.c cVar, long j10) { if (this.f14123b) { throw new IllegalStateException("closed"); } if (j10 == 0) { return; } a.this.f14115d.m(j10); a.this.f14115d.j("\r\n"); a.this.f14115d.t0(cVar, j10); a.this.f14115d.j("\r\n"); } } private class d extends b { /* renamed from: d, reason: collision with root package name */ private final y f14125d; /* renamed from: e, reason: collision with root package name */ private long f14126e; /* renamed from: f, reason: collision with root package name */ private boolean f14127f; d(y yVar) { super(); this.f14126e = -1L; this.f14127f = true; this.f14125d = yVar; } private void z() { if (this.f14126e != -1) { a.this.f14114c.n(); } try { this.f14126e = a.this.f14114c.u(); String trim = a.this.f14114c.n().trim(); if (this.f14126e < 0 || !(trim.isEmpty() || trim.startsWith(";"))) { throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.f14126e + trim + "\""); } if (this.f14126e == 0) { this.f14127f = false; a aVar = a.this; aVar.f14118g = aVar.z(); r9.e.e(a.this.f14112a.i(), this.f14125d, a.this.f14118g); c(); } } catch (NumberFormatException e10) { throw new ProtocolException(e10.getMessage()); } } @Override // x9.v, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f14120b) { return; } if (this.f14127f && !o9.e.p(this, 100, TimeUnit.MILLISECONDS)) { a.this.f14113b.p(); c(); } this.f14120b = true; } @Override // s9.a.b, x9.v public long s0(x9.c cVar, long j10) { if (j10 < 0) { throw new IllegalArgumentException("byteCount < 0: " + j10); } if (this.f14120b) { throw new IllegalStateException("closed"); } if (!this.f14127f) { return -1L; } long j11 = this.f14126e; if (j11 == 0 || j11 == -1) { z(); if (!this.f14127f) { return -1L; } } long s02 = super.s0(cVar, Math.min(j10, this.f14126e)); if (s02 != -1) { this.f14126e -= s02; return s02; } a.this.f14113b.p(); ProtocolException protocolException = new ProtocolException("unexpected end of stream"); c(); throw protocolException; } } private class e extends b { /* renamed from: d, reason: collision with root package name */ private long f14129d; e(long j10) { super(); this.f14129d = j10; if (j10 == 0) { c(); } } @Override // x9.v, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f14120b) { return; } if (this.f14129d != 0 && !o9.e.p(this, 100, TimeUnit.MILLISECONDS)) { a.this.f14113b.p(); c(); } this.f14120b = true; } @Override // s9.a.b, x9.v public long s0(x9.c cVar, long j10) { if (j10 < 0) { throw new IllegalArgumentException("byteCount < 0: " + j10); } if (this.f14120b) { throw new IllegalStateException("closed"); } long j11 = this.f14129d; if (j11 == 0) { return -1L; } long s02 = super.s0(cVar, Math.min(j11, j10)); if (s02 == -1) { a.this.f14113b.p(); ProtocolException protocolException = new ProtocolException("unexpected end of stream"); c(); throw protocolException; } long j12 = this.f14129d - s02; this.f14129d = j12; if (j12 == 0) { c(); } return s02; } } private final class f implements u { /* renamed from: a, reason: collision with root package name */ private final j f14131a; /* renamed from: b, reason: collision with root package name */ private boolean f14132b; private f() { this.f14131a = new j(a.this.f14115d.b()); } @Override // x9.u public w b() { return this.f14131a; } @Override // x9.u, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f14132b) { return; } this.f14132b = true; a.this.s(this.f14131a); a.this.f14116e = 3; } @Override // x9.u, java.io.Flushable public void flush() { if (this.f14132b) { return; } a.this.f14115d.flush(); } @Override // x9.u public void t0(x9.c cVar, long j10) { if (this.f14132b) { throw new IllegalStateException("closed"); } o9.e.f(cVar.u0(), 0L, j10); a.this.f14115d.t0(cVar, j10); } } private class g extends b { /* renamed from: d, reason: collision with root package name */ private boolean f14134d; private g() { super(); } @Override // x9.v, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f14120b) { return; } if (!this.f14134d) { c(); } this.f14120b = true; } @Override // s9.a.b, x9.v public long s0(x9.c cVar, long j10) { if (j10 < 0) { throw new IllegalArgumentException("byteCount < 0: " + j10); } if (this.f14120b) { throw new IllegalStateException("closed"); } if (this.f14134d) { return -1L; } long s02 = super.s0(cVar, j10); if (s02 != -1) { return s02; } this.f14134d = true; c(); return -1L; } } public a(b0 b0Var, q9.e eVar, x9.e eVar2, x9.d dVar) { this.f14112a = b0Var; this.f14113b = eVar; this.f14114c = eVar2; this.f14115d = dVar; } /* JADX INFO: Access modifiers changed from: private */ public void s(j jVar) { w i10 = jVar.i(); jVar.j(w.f15519d); i10.a(); i10.b(); } private u t() { if (this.f14116e == 1) { this.f14116e = 2; return new c(); } throw new IllegalStateException("state: " + this.f14116e); } private v u(y yVar) { if (this.f14116e == 4) { this.f14116e = 5; return new d(yVar); } throw new IllegalStateException("state: " + this.f14116e); } private v v(long j10) { if (this.f14116e == 4) { this.f14116e = 5; return new e(j10); } throw new IllegalStateException("state: " + this.f14116e); } private u w() { if (this.f14116e == 1) { this.f14116e = 2; return new f(); } throw new IllegalStateException("state: " + this.f14116e); } private v x() { if (this.f14116e == 4) { this.f14116e = 5; this.f14113b.p(); return new g(); } throw new IllegalStateException("state: " + this.f14116e); } private String y() { String i10 = this.f14114c.i(this.f14117f); this.f14117f -= i10.length(); return i10; } /* JADX INFO: Access modifiers changed from: private */ public x z() { x.a aVar = new x.a(); while (true) { String y10 = y(); if (y10.length() == 0) { return aVar.d(); } o9.a.f12822a.a(aVar, y10); } } public void A(g0 g0Var) { long b10 = r9.e.b(g0Var); if (b10 == -1) { return; } v v10 = v(b10); o9.e.F(v10, Api.BaseClientBuilder.API_PRIORITY_OTHER, TimeUnit.MILLISECONDS); v10.close(); } public void B(x xVar, String str) { if (this.f14116e != 0) { throw new IllegalStateException("state: " + this.f14116e); } this.f14115d.j(str).j("\r\n"); int h10 = xVar.h(); for (int i10 = 0; i10 < h10; i10++) { this.f14115d.j(xVar.e(i10)).j(": ").j(xVar.i(i10)).j("\r\n"); } this.f14115d.j("\r\n"); this.f14116e = 1; } @Override // r9.c public void a() { this.f14115d.flush(); } @Override // r9.c public g0.a b(boolean z10) { int i10 = this.f14116e; if (i10 != 1 && i10 != 3) { throw new IllegalStateException("state: " + this.f14116e); } try { k a10 = k.a(y()); g0.a j10 = new g0.a().o(a10.f13764a).g(a10.f13765b).l(a10.f13766c).j(z()); if (z10 && a10.f13765b == 100) { return null; } if (a10.f13765b == 100) { this.f14116e = 3; return j10; } this.f14116e = 4; return j10; } catch (EOFException e10) { q9.e eVar = this.f14113b; throw new IOException("unexpected end of stream on " + (eVar != null ? eVar.q().a().l().B() : "unknown"), e10); } } @Override // r9.c public void c() { this.f14115d.flush(); } @Override // r9.c public void cancel() { q9.e eVar = this.f14113b; if (eVar != null) { eVar.c(); } } @Override // r9.c public void d(e0 e0Var) { B(e0Var.d(), i.a(e0Var, this.f14113b.q().b().type())); } @Override // r9.c public q9.e e() { return this.f14113b; } @Override // r9.c public long f(g0 g0Var) { if (!r9.e.c(g0Var)) { return 0L; } if ("chunked".equalsIgnoreCase(g0Var.O("Transfer-Encoding"))) { return -1L; } return r9.e.b(g0Var); } @Override // r9.c public v g(g0 g0Var) { if (!r9.e.c(g0Var)) { return v(0L); } if ("chunked".equalsIgnoreCase(g0Var.O("Transfer-Encoding"))) { return u(g0Var.j0().h()); } long b10 = r9.e.b(g0Var); return b10 != -1 ? v(b10) : x(); } @Override // r9.c public u h(e0 e0Var, long j10) { if (e0Var.a() != null && e0Var.a().f()) { throw new ProtocolException("Duplex connections are not supported for HTTP/1"); } if ("chunked".equalsIgnoreCase(e0Var.c("Transfer-Encoding"))) { return t(); } if (j10 != -1) { return w(); } throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!"); } }