package xyz.stream.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/* JADX INFO: loaded from: classes3.dex */
public final class v implements Callback {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ File f36180a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final /* synthetic */ long f36181b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ String f36182c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ Request f36183d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final /* synthetic */ y f36184e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ z f36185f;

    public v(z zVar, File file, long j10, String str, Request request, y yVar) {
        this.f36185f = zVar;
        this.f36180a = file;
        this.f36181b = j10;
        this.f36182c = str;
        this.f36183d = request;
        this.f36184e = yVar;
    }

    @Override // okhttp3.Callback
    public final void onFailure(Call call, IOException iOException) {
        String str = "download error | " + this.f36180a.getAbsolutePath() + " | " + (System.currentTimeMillis() - this.f36181b) + "ms | " + this.f36182c + " | " + iOException;
        if (str == null) {
            str = "null";
        }
        f.p().y("okhttp", str);
        this.f36185f.getClass();
        z.b(this.f36183d, iOException, this.f36184e);
    }

    /* JADX WARN: Removed duplicated region for block: B:62:0x0133 A[Catch: IOException -> 0x012f, TRY_LEAVE, TryCatch #7 {IOException -> 0x012f, blocks: (B:58:0x012b, B:62:0x0133), top: B:72:0x012b }] */
    /* JADX WARN: Removed duplicated region for block: B:72:0x012b A[EXC_TOP_SPLITTER, SYNTHETIC] */
    @Override // okhttp3.Callback
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void onResponse(Call call, Response response) throws Throwable {
        FileOutputStream fileOutputStream;
        int iCode = response.code();
        File file = this.f36180a;
        Request request = this.f36183d;
        y yVar = this.f36184e;
        z zVar = this.f36185f;
        if (iCode != 200) {
            f.p().y("okhttp", "download failed" + response.code() + " | " + file.getAbsolutePath() + " | " + (System.currentTimeMillis() - this.f36181b) + "ms | " + this.f36182c + " |  | " + response.message());
            if (response.code() == 204) {
                z.a(zVar, response.header(""), yVar);
                return;
            }
            Exception exc = new Exception("download failed: " + response.code());
            zVar.getClass();
            z.b(request, exc, yVar);
            return;
        }
        byte[] bArr = new byte[2048];
        InputStream inputStream = null;
        try {
            long j10 = 0;
            long jContentLength = response.body() != null ? response.body().getContentLength() : 0L;
            if (jContentLength == 0) {
                z.a(zVar, file.getAbsolutePath(), yVar);
                return;
            }
            InputStream inputStreamByteStream = response.body().byteStream();
            try {
                fileOutputStream = new FileOutputStream(file);
                while (true) {
                    try {
                        try {
                            int i10 = inputStreamByteStream.read(bArr);
                            if (i10 == -1) {
                                fileOutputStream.flush();
                                z.a(zVar, file.getAbsolutePath(), yVar);
                                try {
                                    inputStreamByteStream.close();
                                    fileOutputStream.close();
                                    return;
                                } catch (IOException e10) {
                                    e = e10;
                                    h.c(e.toString());
                                    z.b(request, e, yVar);
                                    return;
                                }
                            }
                            j10 += (long) i10;
                            fileOutputStream.write(bArr, 0, i10);
                            int i11 = (int) ((100 * j10) / jContentLength);
                            zVar.getClass();
                            if (yVar != null) {
                                try {
                                    yVar.g(i11);
                                } catch (Exception e11) {
                                    e11.printStackTrace();
                                }
                            }
                        } catch (Throwable th) {
                            th = th;
                            inputStream = inputStreamByteStream;
                            if (inputStream != null) {
                                try {
                                    inputStream.close();
                                } catch (IOException e12) {
                                    h.c(e12.toString());
                                    zVar.getClass();
                                    z.b(request, e12, yVar);
                                    throw th;
                                }
                            }
                            if (fileOutputStream != null) {
                                fileOutputStream.close();
                            }
                            throw th;
                        }
                    } catch (Exception e13) {
                        e = e13;
                        inputStream = inputStreamByteStream;
                        try {
                            h.c(e.toString());
                            zVar.getClass();
                            z.b(request, e, yVar);
                            if (inputStream != null) {
                                try {
                                    inputStream.close();
                                } catch (IOException e14) {
                                    e = e14;
                                    h.c(e.toString());
                                    z.b(request, e, yVar);
                                    return;
                                }
                            }
                            if (fileOutputStream != null) {
                                fileOutputStream.close();
                                return;
                            }
                            return;
                        } catch (Throwable th2) {
                            th = th2;
                            if (inputStream != null) {
                            }
                            if (fileOutputStream != null) {
                            }
                            throw th;
                        }
                    }
                }
            } catch (Exception e15) {
                e = e15;
                fileOutputStream = null;
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = null;
            }
        } catch (Exception e16) {
            e = e16;
            fileOutputStream = null;
        } catch (Throwable th4) {
            th = th4;
            fileOutputStream = null;
        }
    }
}
