package v5;

import A6.k;
import F5.C0115b;
import P6.g;
import a6.r;
import android.content.Context;
import android.net.Uri;
import com.google.android.gms.internal.measurement.C1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import k7.E;

/* JADX INFO: renamed from: v5.f, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1680f {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f18111a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final k f18112b = new k(new C5.b(16));

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final k f18113c = new k(new C0115b(13, this));

    public C1680f(Context context) {
        this.f18111a = context;
    }

    public static File c(String str, InputStream inputStream) throws IllegalAccessException, IOException, InvocationTargetException {
        if (inputStream == null) {
            return null;
        }
        File file = new File(str);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        try {
            try {
                byte[] bArr = new byte[1024];
                while (true) {
                    int i = inputStream.read(bArr);
                    if (i == -1) {
                        fileOutputStream.flush();
                        inputStream.close();
                        fileOutputStream.close();
                        return file;
                    }
                    fileOutputStream.write(bArr, 0, i);
                }
            } finally {
            }
        } finally {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Serializable a(String str, String str2, r rVar, G6.c cVar) throws Throwable {
        C1677c c1677c;
        C1680f c1680f;
        if (cVar instanceof C1677c) {
            c1677c = (C1677c) cVar;
            int i = c1677c.f18104x;
            if ((i & Integer.MIN_VALUE) != 0) {
                c1677c.f18104x = i - Integer.MIN_VALUE;
            } else {
                c1677c = new C1677c(this, cVar);
            }
        }
        Object objA = c1677c.f18102v;
        F6.a aVar = F6.a.f2580q;
        int i5 = c1677c.f18104x;
        try {
            if (i5 == 0) {
                C1.N(objA);
                ((C1679e) this.f18112b.getValue()).f18110a = rVar;
                InterfaceC1676b interfaceC1676b = (InterfaceC1676b) this.f18113c.getValue();
                c1677c.f18100t = str2;
                c1677c.f18101u = this;
                c1677c.f18104x = 1;
                objA = interfaceC1676b.a(str, c1677c);
                if (objA == aVar) {
                    return aVar;
                }
                c1680f = this;
            } else {
                if (i5 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                c1680f = c1677c.f18101u;
                str2 = c1677c.f18100t;
                C1.N(objA);
            }
            InputStream inputStreamI0 = ((E) objA).g().i0();
            c1680f.getClass();
            File fileC = c(str2, inputStreamI0);
            g.b(fileC);
            return fileC;
        } catch (Throwable th) {
            return C1.j(th);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object b(String str, Uri uri, r rVar, G6.c cVar) throws Throwable {
        C1678d c1678d;
        C1680f c1680f;
        if (cVar instanceof C1678d) {
            c1678d = (C1678d) cVar;
            int i = c1678d.f18109x;
            if ((i & Integer.MIN_VALUE) != 0) {
                c1678d.f18109x = i - Integer.MIN_VALUE;
            } else {
                c1678d = new C1678d(this, cVar);
            }
        }
        Object objA = c1678d.f18107v;
        F6.a aVar = F6.a.f2580q;
        int i5 = c1678d.f18109x;
        try {
            if (i5 == 0) {
                C1.N(objA);
                ((C1679e) this.f18112b.getValue()).f18110a = rVar;
                InterfaceC1676b interfaceC1676b = (InterfaceC1676b) this.f18113c.getValue();
                c1678d.f18105t = uri;
                c1678d.f18106u = this;
                c1678d.f18109x = 1;
                objA = interfaceC1676b.a(str, c1678d);
                if (objA == aVar) {
                    return aVar;
                }
                c1680f = this;
            } else {
                if (i5 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                c1680f = c1678d.f18106u;
                uri = c1678d.f18105t;
                C1.N(objA);
            }
            Uri uriD = c1680f.d(uri, ((E) objA).g().i0());
            g.b(uriD);
            return uriD;
        } catch (Throwable th) {
            return C1.j(th);
        }
    }

    public final Uri d(Uri uri, InputStream inputStream) throws IllegalAccessException, IOException, InvocationTargetException {
        OutputStream outputStreamOpenOutputStream;
        byte[] bArr;
        if (inputStream == null || uri == null || (outputStreamOpenOutputStream = this.f18111a.getContentResolver().openOutputStream(uri)) == null) {
            return null;
        }
        try {
            try {
                bArr = new byte[1024];
            } finally {
            }
        } finally {
        }
        while (true) {
            int i = inputStream.read(bArr);
            if (i == -1) {
                outputStreamOpenOutputStream.flush();
                inputStream.close();
                outputStreamOpenOutputStream.close();
                return uri;
            }
            outputStreamOpenOutputStream.write(bArr, 0, i);
        }
    }
}
