package o;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.Iterator;

/* renamed from: o.ny, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public abstract class AbstractC2008ny extends AbstractC2461sm0 {
    public static void H(File file, File file2) throws IOException {
        if (!file.exists()) {
            throw new C0490Sx(file, null, "The source file doesn't exist.");
        }
        if (file2.exists() && !file2.delete()) {
            throw new C0490Sx(file, file2, "Tried to overwrite the destination, but failed to delete it.");
        }
        if (file.isDirectory()) {
            if (!file2.mkdirs()) {
                throw new C1723ky(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 {
                byte[] bArr = new byte[8192];
                for (int i = fileInputStream.read(bArr); i >= 0; i = fileInputStream.read(bArr)) {
                    fileOutputStream.write(bArr, 0, i);
                }
                fileOutputStream.close();
                fileInputStream.close();
            } finally {
            }
        } finally {
        }
    }

    public static ArrayList I(File file) throws IOException {
        Charset charset = AbstractC2355rh.a;
        ArrayList arrayList = new ArrayList();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
        try {
            Iterator it = new C3028yl(new C0164Gi(1, bufferedReader)).iterator();
            while (it.hasNext()) {
                arrayList.add((String) it.next());
            }
            bufferedReader.close();
            return arrayList;
        } finally {
        }
    }

    public static String J(File file, Charset charset) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), charset);
        try {
            String strO = As0.O(inputStreamReader);
            inputStreamReader.close();
            return strO;
        } finally {
        }
    }

    public static File K(File file) {
        int length;
        int iV;
        File file2 = new File("image_cache");
        String path = file2.getPath();
        char c = File.separatorChar;
        boolean z = false;
        int iV2 = Za0.V(path, c, 0, 4);
        if (iV2 != 0) {
            length = (iV2 <= 0 || path.charAt(iV2 + (-1)) != ':') ? (iV2 == -1 && path.length() > 0 && AbstractC0940cn0.q(path.charAt(Za0.S(path)), ':', false)) ? path.length() : 0 : iV2 + 1;
        } else if (path.length() <= 1 || path.charAt(1) != c || (iV = Za0.V(path, c, 2, 4)) < 0) {
            length = 1;
        } else {
            int iV3 = Za0.V(path, c, iV + 1, 4);
            length = iV3 >= 0 ? iV3 + 1 : path.length();
        }
        if (length > 0) {
            return file2;
        }
        String string = file.toString();
        if (!(string.length() == 0)) {
            if (string.length() > 0 && AbstractC0940cn0.q(string.charAt(Za0.S(string)), c, false)) {
                z = true;
            }
            if (!z) {
                return new File(string + c + file2);
            }
        }
        return new File(string + file2);
    }

    public static void L(File file, String str, Charset charset) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        try {
            N(fileOutputStream, str, charset);
            fileOutputStream.close();
        } finally {
        }
    }

    public static final void N(FileOutputStream fileOutputStream, String str, Charset charset) throws IOException {
        if (str.length() < 16384) {
            fileOutputStream.write(str.getBytes(charset));
            return;
        }
        CharsetEncoder charsetEncoderNewEncoder = charset.newEncoder();
        CodingErrorAction codingErrorAction = CodingErrorAction.REPLACE;
        CharsetEncoder charsetEncoderOnUnmappableCharacter = charsetEncoderNewEncoder.onMalformedInput(codingErrorAction).onUnmappableCharacter(codingErrorAction);
        CharBuffer charBufferAllocate = CharBuffer.allocate(8192);
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(8192 * ((int) Math.ceil(charsetEncoderOnUnmappableCharacter.maxBytesPerChar())));
        int i = 0;
        int i2 = 0;
        while (i < str.length()) {
            int iMin = Math.min(8192 - i2, str.length() - i);
            int i3 = i + iMin;
            str.getChars(i, i3, charBufferAllocate.array(), i2);
            charBufferAllocate.limit(iMin + i2);
            i2 = 1;
            if (!charsetEncoderOnUnmappableCharacter.encode(charBufferAllocate, byteBufferAllocate, i3 == str.length()).isUnderflow()) {
                throw new IllegalStateException("Check failed.");
            }
            fileOutputStream.write(byteBufferAllocate.array(), 0, byteBufferAllocate.position());
            if (charBufferAllocate.position() != charBufferAllocate.limit()) {
                charBufferAllocate.put(0, charBufferAllocate.get());
            } else {
                i2 = 0;
            }
            charBufferAllocate.clear();
            byteBufferAllocate.clear();
            i = i3;
        }
    }
}
