package q9; import java.io.IOException; import java.net.ProtocolException; import n9.e0; import n9.g0; import n9.h0; import n9.v; import x9.n; import x9.u; /* loaded from: classes2.dex */ public final class c { /* renamed from: a, reason: collision with root package name */ final k f13509a; /* renamed from: b, reason: collision with root package name */ final n9.g f13510b; /* renamed from: c, reason: collision with root package name */ final v f13511c; /* renamed from: d, reason: collision with root package name */ final d f13512d; /* renamed from: e, reason: collision with root package name */ final r9.c f13513e; /* renamed from: f, reason: collision with root package name */ private boolean f13514f; private final class a extends x9.h { /* renamed from: b, reason: collision with root package name */ private boolean f13515b; /* renamed from: c, reason: collision with root package name */ private long f13516c; /* renamed from: d, reason: collision with root package name */ private long f13517d; /* renamed from: e, reason: collision with root package name */ private boolean f13518e; a(u uVar, long j10) { super(uVar); this.f13516c = j10; } private IOException c(IOException iOException) { if (this.f13515b) { return iOException; } this.f13515b = true; return c.this.a(this.f13517d, false, true, iOException); } @Override // x9.h, x9.u, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f13518e) { return; } this.f13518e = true; long j10 = this.f13516c; if (j10 != -1 && this.f13517d != j10) { throw new ProtocolException("unexpected end of stream"); } try { super.close(); c(null); } catch (IOException e10) { throw c(e10); } } @Override // x9.h, x9.u, java.io.Flushable public void flush() { try { super.flush(); } catch (IOException e10) { throw c(e10); } } @Override // x9.h, x9.u public void t0(x9.c cVar, long j10) { if (this.f13518e) { throw new IllegalStateException("closed"); } long j11 = this.f13516c; if (j11 == -1 || this.f13517d + j10 <= j11) { try { super.t0(cVar, j10); this.f13517d += j10; return; } catch (IOException e10) { throw c(e10); } } throw new ProtocolException("expected " + this.f13516c + " bytes but received " + (this.f13517d + j10)); } } final class b extends x9.i { /* renamed from: b, reason: collision with root package name */ private final long f13520b; /* renamed from: c, reason: collision with root package name */ private long f13521c; /* renamed from: d, reason: collision with root package name */ private boolean f13522d; /* renamed from: e, reason: collision with root package name */ private boolean f13523e; b(x9.v vVar, long j10) { super(vVar); this.f13520b = j10; if (j10 == 0) { z(null); } } @Override // x9.i, x9.v, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f13523e) { return; } this.f13523e = true; try { super.close(); z(null); } catch (IOException e10) { throw z(e10); } } @Override // x9.i, x9.v public long s0(x9.c cVar, long j10) { if (this.f13523e) { throw new IllegalStateException("closed"); } try { long s02 = c().s0(cVar, j10); if (s02 == -1) { z(null); return -1L; } long j11 = this.f13521c + s02; long j12 = this.f13520b; if (j12 != -1 && j11 > j12) { throw new ProtocolException("expected " + this.f13520b + " bytes but received " + j11); } this.f13521c = j11; if (j11 == j12) { z(null); } return s02; } catch (IOException e10) { throw z(e10); } } IOException z(IOException iOException) { if (this.f13522d) { return iOException; } this.f13522d = true; return c.this.a(this.f13521c, true, false, iOException); } } public c(k kVar, n9.g gVar, v vVar, d dVar, r9.c cVar) { this.f13509a = kVar; this.f13510b = gVar; this.f13511c = vVar; this.f13512d = dVar; this.f13513e = cVar; } IOException a(long j10, boolean z10, boolean z11, IOException iOException) { if (iOException != null) { o(iOException); } if (z11) { if (iOException != null) { this.f13511c.p(this.f13510b, iOException); } else { this.f13511c.n(this.f13510b, j10); } } if (z10) { if (iOException != null) { this.f13511c.u(this.f13510b, iOException); } else { this.f13511c.s(this.f13510b, j10); } } return this.f13509a.g(this, z11, z10, iOException); } public void b() { this.f13513e.cancel(); } public e c() { return this.f13513e.e(); } public u d(e0 e0Var, boolean z10) { this.f13514f = z10; long a10 = e0Var.a().a(); this.f13511c.o(this.f13510b); return new a(this.f13513e.h(e0Var, a10), a10); } public void e() { this.f13513e.cancel(); this.f13509a.g(this, true, true, null); } public void f() { try { this.f13513e.a(); } catch (IOException e10) { this.f13511c.p(this.f13510b, e10); o(e10); throw e10; } } public void g() { try { this.f13513e.c(); } catch (IOException e10) { this.f13511c.p(this.f13510b, e10); o(e10); throw e10; } } public boolean h() { return this.f13514f; } public void i() { this.f13513e.e().p(); } public void j() { this.f13509a.g(this, true, false, null); } public h0 k(g0 g0Var) { try { this.f13511c.t(this.f13510b); String O = g0Var.O("Content-Type"); long f10 = this.f13513e.f(g0Var); return new r9.h(O, f10, n.c(new b(this.f13513e.g(g0Var), f10))); } catch (IOException e10) { this.f13511c.u(this.f13510b, e10); o(e10); throw e10; } } public g0.a l(boolean z10) { try { g0.a b10 = this.f13513e.b(z10); if (b10 != null) { o9.a.f12822a.g(b10, this); } return b10; } catch (IOException e10) { this.f13511c.u(this.f13510b, e10); o(e10); throw e10; } } public void m(g0 g0Var) { this.f13511c.v(this.f13510b, g0Var); } public void n() { this.f13511c.w(this.f13510b); } void o(IOException iOException) { this.f13512d.h(); this.f13513e.e().v(iOException); } public void p(e0 e0Var) { try { this.f13511c.r(this.f13510b); this.f13513e.d(e0Var); this.f13511c.q(this.f13510b, e0Var); } catch (IOException e10) { this.f13511c.p(this.f13510b, e10); o(e10); throw e10; } } }