package f6; import android.text.TextUtils; import android.util.Log; import f6.d; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.Map; /* loaded from: classes2.dex */ public class j implements d { /* renamed from: g, reason: collision with root package name */ static final b f9577g = new a(); /* renamed from: a, reason: collision with root package name */ private final l6.h f9578a; /* renamed from: b, reason: collision with root package name */ private final int f9579b; /* renamed from: c, reason: collision with root package name */ private final b f9580c; /* renamed from: d, reason: collision with root package name */ private HttpURLConnection f9581d; /* renamed from: e, reason: collision with root package name */ private InputStream f9582e; /* renamed from: f, reason: collision with root package name */ private volatile boolean f9583f; private static class a implements b { a() { } @Override // f6.j.b public HttpURLConnection a(URL url) { return (HttpURLConnection) url.openConnection(); } } interface b { HttpURLConnection a(URL url); } public j(l6.h hVar, int i10) { this(hVar, i10, f9577g); } private static int c(HttpURLConnection httpURLConnection) { try { return httpURLConnection.getResponseCode(); } catch (IOException e10) { if (!Log.isLoggable("HttpUrlFetcher", 3)) { return -1; } Log.d("HttpUrlFetcher", "Failed to get a response code", e10); return -1; } } private InputStream e(URL url, int i10, URL url2, Map map) { if (i10 >= 5) { throw new e6.e("Too many (> 5) redirects!", -1); } if (url2 != null) { try { if (url.toURI().equals(url2.toURI())) { throw new e6.e("In re-direct loop", -1); } } catch (URISyntaxException unused) { } } HttpURLConnection g10 = g(url, map); this.f9581d = g10; try { g10.connect(); this.f9582e = this.f9581d.getInputStream(); if (this.f9583f) { return null; } int c10 = c(this.f9581d); if (h(c10)) { return i(this.f9581d); } if (!j(c10)) { if (c10 == -1) { throw new e6.e(c10); } try { throw new e6.e(this.f9581d.getResponseMessage(), c10); } catch (IOException e10) { throw new e6.e("Failed to get a response message", c10, e10); } } String headerField = this.f9581d.getHeaderField("Location"); if (TextUtils.isEmpty(headerField)) { throw new e6.e("Received empty or null redirect url", c10); } try { URL url3 = new URL(url, headerField); b(); return e(url3, i10 + 1, url, map); } catch (MalformedURLException e11) { throw new e6.e("Bad redirect url: " + headerField, c10, e11); } } catch (IOException e12) { throw new e6.e("Failed to connect or obtain data", c(this.f9581d), e12); } } private HttpURLConnection g(URL url, Map map) { try { HttpURLConnection a10 = this.f9580c.a(url); for (Map.Entry entry : map.entrySet()) { a10.addRequestProperty((String) entry.getKey(), (String) entry.getValue()); } a10.setConnectTimeout(this.f9579b); a10.setReadTimeout(this.f9579b); a10.setUseCaches(false); a10.setDoInput(true); a10.setInstanceFollowRedirects(false); return a10; } catch (IOException e10) { throw new e6.e("URL.openConnection threw", 0, e10); } } private static boolean h(int i10) { return i10 / 100 == 2; } private InputStream i(HttpURLConnection httpURLConnection) { InputStream inputStream; try { if (TextUtils.isEmpty(httpURLConnection.getContentEncoding())) { inputStream = b7.c.z(httpURLConnection.getInputStream(), httpURLConnection.getContentLength()); } else { if (Log.isLoggable("HttpUrlFetcher", 3)) { Log.d("HttpUrlFetcher", "Got non empty content encoding: " + httpURLConnection.getContentEncoding()); } inputStream = httpURLConnection.getInputStream(); } this.f9582e = inputStream; return inputStream; } catch (IOException e10) { throw new e6.e("Failed to obtain InputStream", c(httpURLConnection), e10); } } private static boolean j(int i10) { return i10 / 100 == 3; } @Override // f6.d public Class a() { return InputStream.class; } @Override // f6.d public void b() { InputStream inputStream = this.f9582e; if (inputStream != null) { try { inputStream.close(); } catch (IOException unused) { } } HttpURLConnection httpURLConnection = this.f9581d; if (httpURLConnection != null) { httpURLConnection.disconnect(); } this.f9581d = null; } @Override // f6.d public void cancel() { this.f9583f = true; } @Override // f6.d public e6.a d() { return e6.a.REMOTE; } @Override // f6.d public void f(b6.g gVar, d.a aVar) { StringBuilder sb; long b10 = b7.f.b(); try { try { aVar.e(e(this.f9578a.h(), 0, null, this.f9578a.g())); } catch (IOException e10) { if (Log.isLoggable("HttpUrlFetcher", 3)) { Log.d("HttpUrlFetcher", "Failed to load data for url", e10); } aVar.c(e10); if (!Log.isLoggable("HttpUrlFetcher", 2)) { return; } else { sb = new StringBuilder(); } } if (Log.isLoggable("HttpUrlFetcher", 2)) { sb = new StringBuilder(); sb.append("Finished http url fetcher fetch in "); sb.append(b7.f.a(b10)); Log.v("HttpUrlFetcher", sb.toString()); } } catch (Throwable th) { if (Log.isLoggable("HttpUrlFetcher", 2)) { Log.v("HttpUrlFetcher", "Finished http url fetcher fetch in " + b7.f.a(b10)); } throw th; } } j(l6.h hVar, int i10, b bVar) { this.f9578a = hVar; this.f9579b = i10; this.f9580c = bVar; } }