package com.bumptech.glide.load.data; import android.text.TextUtils; import android.util.Log; import com.bumptech.glide.Priority; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.HttpException; import com.bumptech.glide.load.data.DataFetcher; import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.util.ContentLengthInputStream; import com.bumptech.glide.util.LogTime; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL; import java.util.Map; /* loaded from: classes.dex */ public class HttpUrlFetcher implements DataFetcher { static final HttpUrlConnectionFactory g = new DefaultHttpUrlConnectionFactory(); private final GlideUrl a; private final int b; private final HttpUrlConnectionFactory c; private HttpURLConnection d; private InputStream e; private volatile boolean f; private static class DefaultHttpUrlConnectionFactory implements HttpUrlConnectionFactory { DefaultHttpUrlConnectionFactory() { } @Override // com.bumptech.glide.load.data.HttpUrlFetcher.HttpUrlConnectionFactory public HttpURLConnection a(URL url) throws IOException { return (HttpURLConnection) url.openConnection(); } } interface HttpUrlConnectionFactory { HttpURLConnection a(URL url) throws IOException; } public HttpUrlFetcher(GlideUrl glideUrl, int i) { this(glideUrl, i, g); } private static boolean b(int i) { return i / 100 == 3; } @Override // com.bumptech.glide.load.data.DataFetcher public void a(Priority priority, DataFetcher.DataCallback dataCallback) { StringBuilder sb; long a = LogTime.a(); try { try { dataCallback.a((DataFetcher.DataCallback) a(this.a.d(), 0, null, this.a.b())); } catch (IOException e) { if (Log.isLoggable("HttpUrlFetcher", 3)) { Log.d("HttpUrlFetcher", "Failed to load data for url", e); } dataCallback.a((Exception) e); 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(LogTime.a(a)); Log.v("HttpUrlFetcher", sb.toString()); } } catch (Throwable th) { if (Log.isLoggable("HttpUrlFetcher", 2)) { Log.v("HttpUrlFetcher", "Finished http url fetcher fetch in " + LogTime.a(a)); } throw th; } } @Override // com.bumptech.glide.load.data.DataFetcher public void cancel() { this.f = true; } @Override // com.bumptech.glide.load.data.DataFetcher public DataSource getDataSource() { return DataSource.REMOTE; } HttpUrlFetcher(GlideUrl glideUrl, int i, HttpUrlConnectionFactory httpUrlConnectionFactory) { this.a = glideUrl; this.b = i; this.c = httpUrlConnectionFactory; } @Override // com.bumptech.glide.load.data.DataFetcher public void b() { InputStream inputStream = this.e; if (inputStream != null) { try { inputStream.close(); } catch (IOException unused) { } } HttpURLConnection httpURLConnection = this.d; if (httpURLConnection != null) { httpURLConnection.disconnect(); } this.d = null; } private InputStream a(URL url, int i, URL url2, Map map) throws IOException { if (i < 5) { if (url2 != null) { try { if (url.toURI().equals(url2.toURI())) { throw new HttpException("In re-direct loop"); } } catch (URISyntaxException unused) { } } this.d = this.c.a(url); for (Map.Entry entry : map.entrySet()) { this.d.addRequestProperty(entry.getKey(), entry.getValue()); } this.d.setConnectTimeout(this.b); this.d.setReadTimeout(this.b); this.d.setUseCaches(false); this.d.setDoInput(true); this.d.setInstanceFollowRedirects(false); this.d.connect(); this.e = this.d.getInputStream(); if (this.f) { return null; } int responseCode = this.d.getResponseCode(); if (a(responseCode)) { return a(this.d); } if (!b(responseCode)) { if (responseCode == -1) { throw new HttpException(responseCode); } throw new HttpException(this.d.getResponseMessage(), responseCode); } String headerField = this.d.getHeaderField("Location"); if (!TextUtils.isEmpty(headerField)) { URL url3 = new URL(url, headerField); b(); return a(url3, i + 1, url, map); } throw new HttpException("Received empty or null redirect url"); } throw new HttpException("Too many (> 5) redirects!"); } private static boolean a(int i) { return i / 100 == 2; } private InputStream a(HttpURLConnection httpURLConnection) throws IOException { if (TextUtils.isEmpty(httpURLConnection.getContentEncoding())) { this.e = ContentLengthInputStream.a(httpURLConnection.getInputStream(), httpURLConnection.getContentLength()); } else { if (Log.isLoggable("HttpUrlFetcher", 3)) { Log.d("HttpUrlFetcher", "Got non empty content encoding: " + httpURLConnection.getContentEncoding()); } this.e = httpURLConnection.getInputStream(); } return this.e; } @Override // com.bumptech.glide.load.data.DataFetcher public Class a() { return InputStream.class; } }