package H4;

import S4.p;
import a.AbstractC0872a;
import e4.C;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import kotlin.jvm.internal.o;
import x4.AbstractC3250p;

/* JADX INFO: loaded from: classes3.dex */
public abstract class d extends C {
    public static void p0(File file, File file2) throws IOException {
        if (!file.exists()) {
            throw new b(file, null, "The source file doesn't exist.");
        }
        if (file2.exists() && !file2.delete()) {
            throw new b(file, file2, "Tried to overwrite the destination, but failed to delete it.");
        }
        if (file.isDirectory()) {
            if (!file2.mkdirs()) {
                throw new c(file, file2, "Failed to create target directory.");
            }
            return;
        }
        File parentFile = file2.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file2);
            try {
                AbstractC0872a.y(fileInputStream, fileOutputStream, 8192);
                fileOutputStream.close();
                fileInputStream.close();
            } finally {
            }
        } finally {
        }
    }

    public static byte[] q0(File file) throws IOException {
        o.h(file, "<this>");
        FileInputStream fileInputStream = new FileInputStream(file);
        try {
            long length = file.length();
            if (length > 2147483647L) {
                throw new OutOfMemoryError("File " + file + " is too big (" + length + " bytes) to fit in memory.");
            }
            int i6 = (int) length;
            byte[] bArrCopyOf = new byte[i6];
            int i7 = i6;
            int i8 = 0;
            while (i7 > 0) {
                int i9 = fileInputStream.read(bArrCopyOf, i8, i7);
                if (i9 < 0) {
                    break;
                }
                i7 -= i9;
                i8 += i9;
            }
            if (i7 > 0) {
                bArrCopyOf = Arrays.copyOf(bArrCopyOf, i8);
                o.g(bArrCopyOf, "copyOf(...)");
            } else {
                int i10 = fileInputStream.read();
                if (i10 != -1) {
                    a aVar = new a(8193);
                    aVar.write(i10);
                    AbstractC0872a.y(fileInputStream, aVar, 8192);
                    int size = aVar.size() + i6;
                    if (size < 0) {
                        throw new OutOfMemoryError("File " + file + " is too big to fit in memory.");
                    }
                    byte[] bArrA = aVar.a();
                    bArrCopyOf = Arrays.copyOf(bArrCopyOf, size);
                    o.g(bArrCopyOf, "copyOf(...)");
                    AbstractC3250p.y(bArrA, i6, 0, bArrCopyOf, aVar.size());
                }
            }
            fileInputStream.close();
            return bArrCopyOf;
        } catch (Throwable th) {
            try {
                throw th;
            } catch (Throwable th2) {
                android.support.v4.media.session.b.j(fileInputStream, th);
                throw th2;
            }
        }
    }

    public static File r0(File file, String relative) {
        int length;
        int iN0;
        o.h(file, "<this>");
        o.h(relative, "relative");
        File file2 = new File(relative);
        String path = file2.getPath();
        o.g(path, "getPath(...)");
        char c6 = File.separatorChar;
        int iN02 = p.N0(path, c6, 0, 4);
        if (iN02 != 0) {
            length = (iN02 <= 0 || path.charAt(iN02 + (-1)) != ':') ? (iN02 == -1 && p.J0(path, ':')) ? path.length() : 0 : iN02 + 1;
        } else if (path.length() <= 1 || path.charAt(1) != c6 || (iN0 = p.N0(path, c6, 2, 4)) < 0) {
            length = 1;
        } else {
            int iN03 = p.N0(path, c6, iN0 + 1, 4);
            length = iN03 >= 0 ? iN03 + 1 : path.length();
        }
        if (length > 0) {
            return file2;
        }
        String string = file.toString();
        o.g(string, "toString(...)");
        if ((string.length() == 0) || p.J0(string, c6)) {
            return new File(string + file2);
        }
        return new File(string + c6 + file2);
    }
}
