package p138.p145.p146.p151.p152;

import android.text.TextUtils;
import android.util.Log;
import com.bumptech.glide.load.HttpException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import p138.p139.p143.p144.C2803;
import p138.p145.p146.EnumC2812;
import p138.p145.p146.p151.EnumC2840;
import p138.p145.p146.p151.p152.InterfaceC2856;
import p138.p145.p146.p151.p158.C3015;
import p138.p145.p146.p174.C3231;
import p138.p145.p146.p174.C3236;

/* JADX INFO: renamed from: ދ.ؠ.֏.ތ.މ.އ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C2866 implements InterfaceC2856<InputStream> {

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public static final InterfaceC2868 f8835 = new C2867();

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final C3015 f8836;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final int f8837;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final InterfaceC2868 f8838;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public HttpURLConnection f8839;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public InputStream f8840;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public volatile boolean f8841;

    /* JADX INFO: renamed from: ދ.ؠ.֏.ތ.މ.އ$֏, reason: contains not printable characters */
    public static class C2867 implements InterfaceC2868 {
    }

    /* JADX INFO: renamed from: ދ.ؠ.֏.ތ.މ.އ$ؠ, reason: contains not printable characters */
    public interface InterfaceC2868 {
    }

    public C2866(C3015 c3015, int i) {
        InterfaceC2868 interfaceC2868 = f8835;
        this.f8836 = c3015;
        this.f8837 = i;
        this.f8838 = interfaceC2868;
    }

    @Override // p138.p145.p146.p151.p152.InterfaceC2856
    public void cancel() {
        this.f8841 = true;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final InputStream m4997(URL url, int i, URL url2, Map<String, String> map) throws IOException {
        if (i >= 5) {
            throw new HttpException("Too many (> 5) redirects!");
        }
        if (url2 != null) {
            try {
                if (url.toURI().equals(url2.toURI())) {
                    throw new HttpException("In re-direct loop");
                }
            } catch (URISyntaxException unused) {
            }
        }
        if (((C2867) this.f8838) == null) {
            throw null;
        }
        this.f8839 = (HttpURLConnection) url.openConnection();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            this.f8839.addRequestProperty(entry.getKey(), entry.getValue());
        }
        this.f8839.setConnectTimeout(this.f8837);
        this.f8839.setReadTimeout(this.f8837);
        this.f8839.setUseCaches(false);
        this.f8839.setDoInput(true);
        this.f8839.setInstanceFollowRedirects(false);
        this.f8839.connect();
        this.f8840 = this.f8839.getInputStream();
        if (this.f8841) {
            return null;
        }
        int responseCode = this.f8839.getResponseCode();
        int i2 = responseCode / 100;
        if (i2 == 2) {
            HttpURLConnection httpURLConnection = this.f8839;
            if (TextUtils.isEmpty(httpURLConnection.getContentEncoding())) {
                this.f8840 = new C3231(httpURLConnection.getInputStream(), httpURLConnection.getContentLength());
            } else {
                if (Log.isLoggable("HttpUrlFetcher", 3)) {
                    StringBuilder sbM4889 = C2803.m4889("Got non empty content encoding: ");
                    sbM4889.append(httpURLConnection.getContentEncoding());
                    Log.d("HttpUrlFetcher", sbM4889.toString());
                }
                this.f8840 = httpURLConnection.getInputStream();
            }
            return this.f8840;
        }
        if (!(i2 == 3)) {
            if (responseCode == -1) {
                throw new HttpException(responseCode);
            }
            throw new HttpException(this.f8839.getResponseMessage(), responseCode);
        }
        String headerField = this.f8839.getHeaderField("Location");
        if (TextUtils.isEmpty(headerField)) {
            throw new HttpException("Received empty or null redirect url");
        }
        URL url3 = new URL(url, headerField);
        mo4973();
        return m4997(url3, i + 1, url, map);
    }

    @Override // p138.p145.p146.p151.p152.InterfaceC2856
    /* JADX INFO: renamed from: ֏ */
    public Class<InputStream> mo4969() {
        return InputStream.class;
    }

    @Override // p138.p145.p146.p151.p152.InterfaceC2856
    /* JADX INFO: renamed from: ֏ */
    public void mo4970(EnumC2812 enumC2812, InterfaceC2856.InterfaceC2857<? super InputStream> interfaceC2857) {
        StringBuilder sb;
        long jM5320 = C3236.m5320();
        try {
            try {
                C3015 c3015 = this.f8836;
                if (c3015.f9179 == null) {
                    c3015.f9179 = new URL(c3015.m5141());
                }
                interfaceC2857.mo4990(m4997(c3015.f9179, 0, null, this.f8836.f9175.mo5142()));
            } catch (IOException e) {
                if (Log.isLoggable("HttpUrlFetcher", 3)) {
                    Log.d("HttpUrlFetcher", "Failed to load data for url", e);
                }
                interfaceC2857.mo4989((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(C3236.m5319(jM5320));
                Log.v("HttpUrlFetcher", sb.toString());
            }
        } catch (Throwable th) {
            if (Log.isLoggable("HttpUrlFetcher", 2)) {
                StringBuilder sbM4889 = C2803.m4889("Finished http url fetcher fetch in ");
                sbM4889.append(C3236.m5319(jM5320));
                Log.v("HttpUrlFetcher", sbM4889.toString());
            }
            throw th;
        }
    }

    @Override // p138.p145.p146.p151.p152.InterfaceC2856
    /* JADX INFO: renamed from: ؠ */
    public void mo4973() {
        InputStream inputStream = this.f8840;
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException unused) {
            }
        }
        HttpURLConnection httpURLConnection = this.f8839;
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
        this.f8839 = null;
    }

    @Override // p138.p145.p146.p151.p152.InterfaceC2856
    /* JADX INFO: renamed from: ހ */
    public EnumC2840 mo4974() {
        return EnumC2840.REMOTE;
    }
}
