package com.ironsource;

import android.text.TextUtils;
import com.ironsource.sdk.utils.IronSourceStorageUtils;
import com.ironsource.sdk.utils.Logger;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;
/* loaded from: classes4.dex */
class nc implements Callable<ta> {
    private static final String d = "FileWorkerThread";
    private static final String e = "X-Android-Protocols";
    private static final String f = "http/1.1,h2";

    /* renamed from: a  reason: collision with root package name */
    private final sa f2742a;
    private final String b;
    private long c;

    /* JADX INFO: Access modifiers changed from: package-private */
    public nc(sa saVar, String str, long j) {
        this.f2742a = saVar;
        this.b = str;
        this.c = j;
    }

    int a(byte[] bArr, String str) throws Exception {
        return IronSourceStorageUtils.saveFile(bArr, str);
    }

    @Override // java.util.concurrent.Callable
    /* renamed from: a */
    public ta call() {
        int i;
        if (this.c == 0) {
            this.c = 1L;
        }
        ta taVar = null;
        for (int i2 = 0; i2 < this.c; i2++) {
            taVar = a(this.f2742a.e(), i2, this.f2742a.a(), this.f2742a.c(), this.f2742a.f());
            int b = taVar.b();
            if (b != 1008 && b != 1009) {
                break;
            }
        }
        if (taVar != null && taVar.a() != null) {
            StringBuilder append = new StringBuilder().append(this.b);
            String str = File.separator;
            String sb = append.append(str).append(this.f2742a.b().getName()).toString();
            String str2 = this.f2742a.d() + str + a9.E + this.f2742a.b().getName();
            try {
                if (a(taVar.a(), str2) == 0) {
                    taVar.a(1006);
                } else if (!a(str2, sb)) {
                    taVar.a(1014);
                }
            } catch (FileNotFoundException e2) {
                l9.d().a(e2);
                i = 1018;
                taVar.a(i);
            } catch (Error e3) {
                l9.d().a(e3);
                if (!TextUtils.isEmpty(e3.getMessage())) {
                    Logger.i(d, e3.getMessage());
                }
                i = 1019;
                taVar.a(i);
            } catch (Exception e4) {
                l9.d().a(e4);
                if (!TextUtils.isEmpty(e4.getMessage())) {
                    Logger.i(d, e4.getMessage());
                }
                taVar.a(1009);
            }
        }
        return taVar;
    }

    /* JADX WARN: Code restructure failed: missing block: B:49:0x00c7, code lost:
        if (r5 != null) goto L21;
     */
    /* JADX WARN: Code restructure failed: missing block: B:63:0x0106, code lost:
        if (r5 != null) goto L39;
     */
    /* JADX WARN: Code restructure failed: missing block: B:64:0x0108, code lost:
        r5.disconnect();
     */
    /* JADX WARN: Code restructure failed: missing block: B:72:0x011c, code lost:
        if (r5 != null) goto L39;
     */
    /* JADX WARN: Code restructure failed: missing block: B:80:0x012f, code lost:
        if (r5 != null) goto L39;
     */
    /* JADX WARN: Code restructure failed: missing block: B:88:0x0142, code lost:
        if (r5 != null) goto L39;
     */
    /* JADX WARN: Code restructure failed: missing block: B:96:0x0155, code lost:
        if (r5 != null) goto L39;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:23:0x0089 A[Catch: all -> 0x0090, TRY_ENTER, TryCatch #11 {all -> 0x0090, blocks: (B:23:0x0089, B:24:0x008c, B:48:0x00c4), top: B:112:0x001a }] */
    /* JADX WARN: Type inference failed for: r3v1, types: [java.net.HttpURLConnection, java.io.InputStream] */
    /* JADX WARN: Type inference failed for: r3v2, types: [java.io.InputStream] */
    /* JADX WARN: Type inference failed for: r3v4 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    com.ironsource.ta a(java.lang.String r8, int r9, int r10, int r11, boolean r12) {
        /*
            Method dump skipped, instructions count: 405
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ironsource.nc.a(java.lang.String, int, int, int, boolean):com.ironsource.ta");
    }

    boolean a(String str, String str2) throws Exception {
        return IronSourceStorageUtils.renameFile(str, str2);
    }

    byte[] a(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[8192];
        while (true) {
            int read = inputStream.read(bArr, 0, 8192);
            if (read == -1) {
                byteArrayOutputStream.flush();
                return byteArrayOutputStream.toByteArray();
            }
            byteArrayOutputStream.write(bArr, 0, read);
        }
    }
}
