package com.bytedance.sdk.component.jnr.fm;

import com.bytedance.sdk.component.jnr.jnr;
import com.bytedance.sdk.component.utils.vt;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class fm implements com.bytedance.sdk.component.jnr.yz<lb> {
    /* JADX WARN: Can't wrap try/catch for region: R(12:0|2|(5:47|3|4|50|5)|(7:52|6|(1:8)(1:55)|41|40|43|44)|15|48|16|(1:18)|24|43|44|(1:(0))) */
    /* JADX WARN: Code restructure failed: missing block: B:20:0x0054, code lost:
    
        r9 = e;
     */
    /* JADX WARN: Code restructure failed: missing block: B:22:0x0056, code lost:
    
        r9 = e;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r5v10 */
    /* JADX WARN: Type inference failed for: r5v11 */
    /* JADX WARN: Type inference failed for: r5v3 */
    /* JADX WARN: Type inference failed for: r5v4 */
    /* JADX WARN: Type inference failed for: r5v5 */
    /* JADX WARN: Type inference failed for: r5v6, types: [java.io.Closeable] */
    /* JADX WARN: Type inference failed for: r5v7 */
    /* JADX WARN: Type inference failed for: r5v9, types: [java.io.ByteArrayOutputStream, java.io.Closeable] */
    @Override // com.bytedance.sdk.component.jnr.yz
    /* JADX INFO: renamed from: ro, reason: merged with bridge method [inline-methods] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public lb<byte[]> fm(jnr jnrVar) throws Throwable {
        Closeable closeable;
        InputStream inputStream;
        byte[] byteArray;
        ?? byteArrayOutputStream;
        String message;
        ?? r52;
        HttpURLConnection httpURLConnection;
        byte[] bArr;
        InputStream inputStream2 = null;
        Map<String, String> mapFm = null;
        int i10 = 0;
        try {
            try {
                httpURLConnection = (HttpURLConnection) new URL(jnrVar.fm()).openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.setReadTimeout(5000);
                httpURLConnection.connect();
                inputStream = httpURLConnection.getInputStream();
            } catch (Throwable th) {
                th = th;
            }
            try {
                bArr = new byte[1024];
                byteArrayOutputStream = new ByteArrayOutputStream();
            } catch (MalformedURLException e10) {
                e = e10;
                byteArray = null;
                byteArrayOutputStream = byteArray;
            } catch (IOException e11) {
                e = e11;
                byteArray = null;
                byteArrayOutputStream = byteArray;
                e.getMessage();
                message = e.getMessage();
                r52 = byteArrayOutputStream;
                vt.fm(inputStream);
                vt.fm(r52);
                return new lb<>(i10, byteArray, message, mapFm);
            } catch (Throwable th2) {
                th = th2;
                closeable = null;
                inputStream2 = inputStream;
                vt.fm(inputStream2);
                vt.fm(closeable);
                throw th;
            }
        } catch (MalformedURLException e12) {
            e = e12;
            inputStream = null;
            byteArray = null;
        } catch (IOException e13) {
            e = e13;
            inputStream = null;
            byteArray = null;
        } catch (Throwable th3) {
            th = th3;
            closeable = null;
            vt.fm(inputStream2);
            vt.fm(closeable);
            throw th;
        }
        while (true) {
            try {
                int i11 = inputStream.read(bArr);
                if (i11 == -1) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, i11);
            } catch (MalformedURLException e14) {
                e = e14;
                byteArray = null;
            } catch (IOException e15) {
                e = e15;
                byteArray = null;
            }
            e.getMessage();
            message = e.getMessage();
            r52 = byteArrayOutputStream;
            vt.fm(inputStream);
            vt.fm(r52);
            return new lb<>(i10, byteArray, message, mapFm);
        }
        i10 = 200;
        byteArray = byteArrayOutputStream.toByteArray();
        mapFm = jnrVar.ro() ? fm(httpURLConnection) : null;
        vt.fm(inputStream);
        vt.fm(byteArrayOutputStream);
        message = "success";
        return new lb<>(i10, byteArray, message, mapFm);
    }

    private Map<String, String> fm(HttpURLConnection httpURLConnection) {
        HashMap map = new HashMap();
        Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
        for (String str : headerFields.keySet()) {
            List<String> list = headerFields.get(str);
            if (list != null && list.size() > 0) {
                map.put(str, list.get(0));
            }
        }
        return map;
    }
}
