package com.google.android.gms.internal.ads;

import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* JADX INFO: loaded from: classes2.dex */
public final class z40 extends w40 {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Set f19085f = Collections.synchronizedSet(new HashSet());

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final DecimalFormat f19086g = new DecimalFormat("#,###");

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public File f19087d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f19088e;

    /* JADX WARN: Code restructure failed: missing block: B:134:0x03d1, code lost:
    
        r30 = r2;
        r29 = r21;
     */
    /* JADX WARN: Code restructure failed: missing block: B:135:0x03d5, code lost:
    
        r10.close();
     */
    /* JADX WARN: Code restructure failed: missing block: B:136:0x03dd, code lost:
    
        if (q7.j.f(3) == false) goto L145;
     */
    /* JADX WARN: Code restructure failed: missing block: B:137:0x03df, code lost:
    
        r0 = r11.format((long) r4);
        r2 = new java.lang.StringBuilder((java.lang.String.valueOf(r0).length() + 22) + java.lang.String.valueOf(r33).length());
        r2.append("Preloaded ");
        r2.append(r0);
        r2.append(" bytes from ");
     */
    /* JADX WARN: Code restructure failed: missing block: B:138:0x0407, code lost:
    
        r1 = r33;
     */
    /* JADX WARN: Code restructure failed: missing block: B:139:0x0409, code lost:
    
        r2.append(r1);
        q7.j.a(r2.toString());
     */
    /* JADX WARN: Code restructure failed: missing block: B:141:0x0416, code lost:
    
        r0 = e;
     */
    /* JADX WARN: Code restructure failed: missing block: B:143:0x0419, code lost:
    
        r0 = e;
     */
    /* JADX WARN: Code restructure failed: missing block: B:145:0x041c, code lost:
    
        r1 = r33;
     */
    /* JADX WARN: Code restructure failed: missing block: B:146:0x041f, code lost:
    
        r14.setReadable(true, false);
     */
    /* JADX WARN: Code restructure failed: missing block: B:147:0x0426, code lost:
    
        if (r29.isFile() == false) goto L150;
     */
    /* JADX WARN: Code restructure failed: missing block: B:148:0x0428, code lost:
    
        r29.setLastModified(java.lang.System.currentTimeMillis());
     */
    /* JADX WARN: Code restructure failed: missing block: B:151:0x0434, code lost:
    
        r29.createNewFile();
     */
    /* JADX WARN: Code restructure failed: missing block: B:194:0x04d2, code lost:
    
        m7.n.C.f30469h.d("VideoStreamFullFileCache.preload", r0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:198:0x04e4, code lost:
    
        r5 = new java.lang.StringBuilder(java.lang.String.valueOf(r33).length() + 26);
        r5.append("Preload aborted for URL \"");
        r5.append(r1);
        r5.append("\"");
        q7.j.c(r5.toString());
     */
    /* JADX WARN: Code restructure failed: missing block: B:199:0x0506, code lost:
    
        new java.lang.StringBuilder(java.lang.String.valueOf(r33).length() + 25);
     */
    /* JADX WARN: Code restructure failed: missing block: B:204:0x0521, code lost:
    
        q7.j.d("Could not delete partial cache file at ".concat(java.lang.String.valueOf(r14.getAbsolutePath())));
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:194:0x04d2  */
    /* JADX WARN: Removed duplicated region for block: B:198:0x04e4  */
    /* JADX WARN: Removed duplicated region for block: B:199:0x0506  */
    /* JADX WARN: Removed duplicated region for block: B:202:0x051b  */
    /* JADX WARN: Type inference failed for: r11v12 */
    /* JADX WARN: Type inference failed for: r11v13 */
    /* JADX WARN: Type inference failed for: r11v14 */
    /* JADX WARN: Type inference failed for: r11v5, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r15v9, types: [int] */
    /* JADX WARN: Type inference failed for: r32v0, types: [com.google.android.gms.internal.ads.w40, com.google.android.gms.internal.ads.z40] */
    /* JADX WARN: Type inference failed for: r4v13 */
    /* JADX WARN: Type inference failed for: r4v24 */
    /* JADX WARN: Type inference failed for: r4v25, types: [int] */
    /* JADX WARN: Type inference failed for: r4v26 */
    /* JADX WARN: Type inference failed for: r4v3, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r4v33 */
    /* JADX WARN: Type inference failed for: r4v36 */
    /* JADX WARN: Unreachable blocks removed: 1, instructions: 1 */
    @Override // com.google.android.gms.internal.ads.w40
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean i(String str) {
        String str2;
        int i10;
        File file;
        String strConcat;
        String str3;
        String str4;
        String str5;
        String string;
        FileOutputStream fileOutputStream;
        ?? r11;
        ?? r42;
        File file2;
        af0 af0Var;
        FileOutputStream fileOutputStream2;
        Object obj;
        ?? r43;
        String str6;
        ReadableByteChannel readableByteChannel;
        ByteBuffer byteBuffer;
        p7.b0 b0Var;
        FileChannel fileChannel;
        File file3;
        String str7;
        int i11;
        String str8 = " at ";
        String str9 = "HTTP status code ";
        String str10 = "HTTP request failed. Code: ";
        if (this.f19087d == null) {
            zzq(str, null, "noCacheDir", null);
            return false;
        }
        while (true) {
            File file4 = this.f19087d;
            if (file4 == null) {
                str2 = str8;
                i10 = 0;
            } else {
                File[] fileArrListFiles = file4.listFiles();
                int length = fileArrListFiles.length;
                str2 = str8;
                int i12 = 0;
                int i13 = 0;
                while (i12 < length) {
                    File[] fileArr = fileArrListFiles;
                    int i14 = length;
                    if (!fileArrListFiles[i12].getName().endsWith(".done")) {
                        i13++;
                    }
                    i12++;
                    length = i14;
                    fileArrListFiles = fileArr;
                }
                i10 = i13;
            }
            tn tnVar = ao.f9093u;
            n7.y yVar = n7.y.f31093e;
            String str11 = str9;
            if (i10 > ((Integer) yVar.f31096c.b(tnVar)).intValue()) {
                File file5 = this.f19087d;
                if (file5 != null) {
                    File[] fileArrListFiles2 = file5.listFiles();
                    int length2 = fileArrListFiles2.length;
                    long j10 = Long.MAX_VALUE;
                    String str12 = str10;
                    int i15 = 0;
                    File file6 = null;
                    while (i15 < length2) {
                        File file7 = fileArrListFiles2[i15];
                        File[] fileArr2 = fileArrListFiles2;
                        int i16 = length2;
                        if (!file7.getName().endsWith(".done")) {
                            long jLastModified = file7.lastModified();
                            if (jLastModified < j10) {
                                j10 = jLastModified;
                                file6 = file7;
                            }
                        }
                        i15++;
                        fileArrListFiles2 = fileArr2;
                        length2 = i16;
                    }
                    if (file6 == null) {
                        break;
                    }
                    boolean zDelete = file6.delete();
                    File file8 = new File(g30.a(this.f19087d, String.valueOf(file6.getName()).concat(".done")));
                    if (file8.isFile()) {
                        zDelete &= file8.delete();
                    }
                    if (!zDelete) {
                        break;
                    }
                    str8 = str2;
                    str9 = str11;
                    str10 = str12;
                } else {
                    break;
                }
            } else {
                String str13 = str10;
                file = new File(g30.a(this.f19087d, q7.e.zzf(str)));
                File file9 = new File(g30.a(this.f19087d, String.valueOf(file.getName()).concat(".done")));
                if (file.isFile() && file9.isFile()) {
                    int length3 = (int) file.length();
                    q7.j.a("Stream cache hit at ".concat(String.valueOf(str)));
                    q7.e.f32222b.post(new l.w0((w40) this, str, file.getAbsolutePath(), length3));
                    return true;
                }
                String strValueOf = String.valueOf(this.f19087d.getAbsolutePath());
                String strValueOf2 = String.valueOf(str);
                Set set = f19085f;
                strConcat = strValueOf.concat(strValueOf2);
                synchronized (set) {
                    try {
                        if (set.contains(strConcat)) {
                            StringBuilder sb = new StringBuilder(String.valueOf(str).length() + 36);
                            sb.append("Stream cache already in progress at ");
                            sb.append(str);
                            q7.j.d(sb.toString());
                            zzq(str, file.getAbsolutePath(), "inProgress", null);
                            return false;
                        }
                        set.add(strConcat);
                        try {
                            file2 = file9;
                            af0Var = af0.f8677b;
                            str4 = IjkMediaPlayer.OnNativeInvokeListener.ARG_ERROR;
                        } catch (IOException | RuntimeException e10) {
                            e = e10;
                            str3 = str;
                            str4 = IjkMediaPlayer.OnNativeInvokeListener.ARG_ERROR;
                        }
                        try {
                            HttpURLConnection httpURLConnectionE = new ui1(af0Var, c10.f9691c, null).e(new y40(str, 0));
                            int responseCode = httpURLConnectionE.getResponseCode();
                            if (responseCode >= 400) {
                                str3 = str;
                                str5 = "badUrl";
                                try {
                                    String string2 = Integer.toString(responseCode);
                                    StringBuilder sb2 = new StringBuilder(String.valueOf(string2).length() + 27);
                                    sb2.append(str13);
                                    sb2.append(string2);
                                    string = sb2.toString();
                                    try {
                                        StringBuilder sb3 = new StringBuilder(String.valueOf(responseCode).length() + 21 + String.valueOf(str).length());
                                        sb3.append(str11);
                                        sb3.append(responseCode);
                                        sb3.append(str2);
                                        sb3.append(str3);
                                        throw new IOException(sb3.toString());
                                    } catch (IOException e11) {
                                        e = e11;
                                        fileOutputStream = null;
                                        r42 = string;
                                        r11 = str5;
                                        if (e instanceof RuntimeException) {
                                        }
                                        fileOutputStream.close();
                                        if (this.f19088e) {
                                        }
                                        if (file.exists()) {
                                        }
                                        zzq(str3, file.getAbsolutePath(), r11, r42);
                                        f19085f.remove(strConcat);
                                        return false;
                                    } catch (RuntimeException e12) {
                                        e = e12;
                                        fileOutputStream = null;
                                        r42 = string;
                                        r11 = str5;
                                        if (e instanceof RuntimeException) {
                                        }
                                        fileOutputStream.close();
                                        if (this.f19088e) {
                                        }
                                        if (file.exists()) {
                                        }
                                        zzq(str3, file.getAbsolutePath(), r11, r42);
                                        f19085f.remove(strConcat);
                                        return false;
                                    }
                                } catch (IOException e13) {
                                    e = e13;
                                    string = null;
                                    fileOutputStream = null;
                                    r42 = string;
                                    r11 = str5;
                                    if (e instanceof RuntimeException) {
                                    }
                                    fileOutputStream.close();
                                    if (this.f19088e) {
                                    }
                                    if (file.exists()) {
                                    }
                                    zzq(str3, file.getAbsolutePath(), r11, r42);
                                    f19085f.remove(strConcat);
                                    return false;
                                } catch (RuntimeException e14) {
                                    e = e14;
                                    string = null;
                                    fileOutputStream = null;
                                    r42 = string;
                                    r11 = str5;
                                    if (e instanceof RuntimeException) {
                                    }
                                    fileOutputStream.close();
                                    if (this.f19088e) {
                                    }
                                    if (file.exists()) {
                                    }
                                    zzq(str3, file.getAbsolutePath(), r11, r42);
                                    f19085f.remove(strConcat);
                                    return false;
                                }
                            }
                            int contentLength = httpURLConnectionE.getContentLength();
                            if (contentLength < 0) {
                                StringBuilder sb4 = new StringBuilder(String.valueOf(str).length() + 55);
                                sb4.append("Stream cache aborted, missing content-length header at ");
                                sb4.append(str);
                                q7.j.d(sb4.toString());
                                zzq(str, file.getAbsolutePath(), "contentLengthMissing", null);
                                set.remove(strConcat);
                                return false;
                            }
                            DecimalFormat decimalFormat = f19086g;
                            String str14 = decimalFormat.format(contentLength);
                            int iIntValue = ((Integer) yVar.f31096c.b(ao.f9109v)).intValue();
                            String str15 = "File too big for full file cache. Size: ";
                            if (contentLength > iIntValue) {
                                StringBuilder sb5 = new StringBuilder(String.valueOf(str14).length() + 33 + String.valueOf(str).length());
                                sb5.append("Content length ");
                                sb5.append(str14);
                                sb5.append(" exceeds limit at ");
                                sb5.append(str);
                                q7.j.d(sb5.toString());
                                StringBuilder sb6 = new StringBuilder(String.valueOf(str14).length() + 40);
                                sb6.append("File too big for full file cache. Size: ");
                                sb6.append(str14);
                                zzq(str, file.getAbsolutePath(), "sizeExceeded", sb6.toString());
                                set.remove(strConcat);
                                return false;
                            }
                            StringBuilder sb7 = new StringBuilder(String.valueOf(str14).length() + 20 + String.valueOf(str).length());
                            sb7.append("Caching ");
                            sb7.append(str14);
                            sb7.append(" bytes from ");
                            sb7.append(str);
                            q7.j.a(sb7.toString());
                            ReadableByteChannel readableByteChannelNewChannel = Channels.newChannel(httpURLConnectionE.getInputStream());
                            fileOutputStream2 = new FileOutputStream(file);
                            try {
                                FileChannel channel = fileOutputStream2.getChannel();
                                ByteBuffer byteBufferAllocate = ByteBuffer.allocate(ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES);
                                m7.n.C.f30472k.getClass();
                                long jCurrentTimeMillis = System.currentTimeMillis();
                                try {
                                    p7.b0 b0Var2 = new p7.b0(((Long) yVar.f31096c.b(ao.Z)).longValue());
                                    long jLongValue = ((Long) yVar.f31096c.b(ao.Y)).longValue();
                                    r43 = 0;
                                    while (true) {
                                        int i17 = readableByteChannelNewChannel.read(byteBufferAllocate);
                                        if (i17 < 0) {
                                            break;
                                        }
                                        ?? r15 = r43 + i17;
                                        try {
                                            try {
                                                if (r15 > iIntValue) {
                                                    String str16 = str15;
                                                    String string3 = Integer.toString(r15);
                                                    StringBuilder sb8 = new StringBuilder(String.valueOf(string3).length() + 40);
                                                    sb8.append(str16);
                                                    sb8.append(string3);
                                                    sb8.toString();
                                                    throw new IOException("stream cache file size limit exceeded");
                                                }
                                                try {
                                                    byteBufferAllocate.flip();
                                                    do {
                                                    } while (channel.write(byteBufferAllocate) > 0);
                                                    byteBufferAllocate.clear();
                                                } catch (IOException e15) {
                                                    e = e15;
                                                    str3 = str;
                                                    fileOutputStream = fileOutputStream2;
                                                    obj = str4;
                                                    r42 = 0;
                                                    r11 = obj;
                                                    if (e instanceof RuntimeException) {
                                                    }
                                                    try {
                                                        fileOutputStream.close();
                                                    } catch (IOException | NullPointerException unused) {
                                                    }
                                                    if (this.f19088e) {
                                                    }
                                                    if (file.exists()) {
                                                    }
                                                    zzq(str3, file.getAbsolutePath(), r11, r42);
                                                    f19085f.remove(strConcat);
                                                    return false;
                                                } catch (RuntimeException e16) {
                                                    e = e16;
                                                    str3 = str;
                                                    fileOutputStream = fileOutputStream2;
                                                    obj = str4;
                                                    r42 = 0;
                                                    r11 = obj;
                                                    if (e instanceof RuntimeException) {
                                                    }
                                                    fileOutputStream.close();
                                                    if (this.f19088e) {
                                                    }
                                                    if (file.exists()) {
                                                    }
                                                    zzq(str3, file.getAbsolutePath(), r11, r42);
                                                    f19085f.remove(strConcat);
                                                    return false;
                                                }
                                                try {
                                                    if (System.currentTimeMillis() - jCurrentTimeMillis > 1000 * jLongValue) {
                                                        String string4 = Long.toString(jLongValue);
                                                        StringBuilder sb9 = new StringBuilder(String.valueOf(string4).length() + 29);
                                                        sb9.append("Timeout exceeded. Limit: ");
                                                        sb9.append(string4);
                                                        sb9.append(" sec");
                                                        sb9.toString();
                                                        throw new IOException("stream cache time limit exceeded");
                                                    }
                                                    if (this.f19088e) {
                                                        throw new IOException("abort requested");
                                                    }
                                                    if (b0Var2.a()) {
                                                        readableByteChannel = readableByteChannelNewChannel;
                                                        b0Var = b0Var2;
                                                        str7 = str15;
                                                        fileChannel = channel;
                                                        file3 = file2;
                                                        i11 = iIntValue;
                                                        str6 = strConcat;
                                                        byteBuffer = byteBufferAllocate;
                                                        try {
                                                            q7.e.f32222b.post(new r40(this, str, file.getAbsolutePath(), r15, contentLength));
                                                        } catch (IOException e17) {
                                                            e = e17;
                                                            str3 = str;
                                                            strConcat = str6;
                                                            fileOutputStream = fileOutputStream2;
                                                            obj = str4;
                                                            r42 = 0;
                                                            r11 = obj;
                                                            if (e instanceof RuntimeException) {
                                                            }
                                                            fileOutputStream.close();
                                                            if (this.f19088e) {
                                                            }
                                                            if (file.exists()) {
                                                            }
                                                            zzq(str3, file.getAbsolutePath(), r11, r42);
                                                            f19085f.remove(strConcat);
                                                            return false;
                                                        } catch (RuntimeException e18) {
                                                            e = e18;
                                                            str3 = str;
                                                            strConcat = str6;
                                                            fileOutputStream = fileOutputStream2;
                                                            obj = str4;
                                                            r42 = 0;
                                                            r11 = obj;
                                                            if (e instanceof RuntimeException) {
                                                            }
                                                            fileOutputStream.close();
                                                            if (this.f19088e) {
                                                            }
                                                            if (file.exists()) {
                                                            }
                                                            zzq(str3, file.getAbsolutePath(), r11, r42);
                                                            f19085f.remove(strConcat);
                                                            return false;
                                                        }
                                                    } else {
                                                        readableByteChannel = readableByteChannelNewChannel;
                                                        str6 = strConcat;
                                                        byteBuffer = byteBufferAllocate;
                                                        b0Var = b0Var2;
                                                        fileChannel = channel;
                                                        file3 = file2;
                                                        str7 = str15;
                                                        i11 = iIntValue;
                                                    }
                                                    str15 = str7;
                                                    r43 = r15;
                                                    readableByteChannelNewChannel = readableByteChannel;
                                                    iIntValue = i11;
                                                    b0Var2 = b0Var;
                                                    channel = fileChannel;
                                                    file2 = file3;
                                                    strConcat = str6;
                                                    byteBufferAllocate = byteBuffer;
                                                } catch (IOException | RuntimeException e19) {
                                                    e = e19;
                                                    str6 = strConcat;
                                                    str3 = str;
                                                    strConcat = str6;
                                                    fileOutputStream = fileOutputStream2;
                                                    obj = str4;
                                                    r42 = 0;
                                                    r11 = obj;
                                                    if (e instanceof RuntimeException) {
                                                    }
                                                    fileOutputStream.close();
                                                    if (this.f19088e) {
                                                    }
                                                    if (file.exists()) {
                                                    }
                                                    zzq(str3, file.getAbsolutePath(), r11, r42);
                                                    f19085f.remove(strConcat);
                                                    return false;
                                                }
                                            } catch (IOException | RuntimeException e20) {
                                                e = e20;
                                                str3 = str;
                                                fileOutputStream = fileOutputStream2;
                                                strConcat = str6;
                                                obj = decimalFormat;
                                                r42 = 0;
                                                r11 = obj;
                                                if (e instanceof RuntimeException) {
                                                }
                                                fileOutputStream.close();
                                                if (this.f19088e) {
                                                }
                                                if (file.exists()) {
                                                }
                                                zzq(str3, file.getAbsolutePath(), r11, r42);
                                                f19085f.remove(strConcat);
                                                return false;
                                            }
                                        } catch (IOException | RuntimeException e21) {
                                            e = e21;
                                            str3 = str;
                                            r42 = r15;
                                            strConcat = str6;
                                            fileOutputStream = fileOutputStream2;
                                            r11 = decimalFormat;
                                            if (e instanceof RuntimeException) {
                                            }
                                            fileOutputStream.close();
                                            if (this.f19088e) {
                                            }
                                            if (file.exists()) {
                                            }
                                            zzq(str3, file.getAbsolutePath(), r11, r42);
                                            f19085f.remove(strConcat);
                                            return false;
                                        }
                                    }
                                } catch (IOException e22) {
                                    e = e22;
                                } catch (RuntimeException e23) {
                                    e = e23;
                                }
                            } catch (IOException | RuntimeException e24) {
                                e = e24;
                                str3 = str;
                            }
                        } catch (IOException e25) {
                            e = e25;
                            str3 = str;
                            str5 = str4;
                            string = null;
                            fileOutputStream = null;
                            r42 = string;
                            r11 = str5;
                            if (e instanceof RuntimeException) {
                            }
                            fileOutputStream.close();
                            if (this.f19088e) {
                            }
                            if (file.exists()) {
                            }
                            zzq(str3, file.getAbsolutePath(), r11, r42);
                            f19085f.remove(strConcat);
                            return false;
                        } catch (RuntimeException e26) {
                            e = e26;
                            str3 = str;
                            str5 = str4;
                            string = null;
                            fileOutputStream = null;
                            r42 = string;
                            r11 = str5;
                            if (e instanceof RuntimeException) {
                            }
                            fileOutputStream.close();
                            if (this.f19088e) {
                            }
                            if (file.exists()) {
                            }
                            zzq(str3, file.getAbsolutePath(), r11, r42);
                            f19085f.remove(strConcat);
                            return false;
                        }
                    } finally {
                    }
                }
            }
        }
        q7.j.d("Unable to expire stream cache");
        zzq(str, null, "expireFailed", null);
        return false;
        q7.e.f32222b.post(new l.w0((w40) this, str3, file.getAbsolutePath(), (int) r43));
        strConcat = str6;
        try {
            f19085f.remove(strConcat);
            return true;
        } catch (IOException e27) {
            e = e27;
            fileOutputStream = fileOutputStream2;
            obj = str4;
            r42 = 0;
            r11 = obj;
            if (e instanceof RuntimeException) {
            }
            fileOutputStream.close();
            if (this.f19088e) {
            }
            if (file.exists()) {
            }
            zzq(str3, file.getAbsolutePath(), r11, r42);
            f19085f.remove(strConcat);
            return false;
        } catch (RuntimeException e28) {
            e = e28;
            fileOutputStream = fileOutputStream2;
            obj = str4;
            r42 = 0;
            r11 = obj;
            if (e instanceof RuntimeException) {
            }
            fileOutputStream.close();
            if (this.f19088e) {
            }
            if (file.exists()) {
            }
            zzq(str3, file.getAbsolutePath(), r11, r42);
            f19085f.remove(strConcat);
            return false;
        }
    }

    @Override // com.google.android.gms.internal.ads.w40
    public final void q() {
        this.f19088e = true;
    }
}
