package com.friday.executor.util;

import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.nio.file.Files;

/**
 * @Author YaoYao
 * @Date Created In 21:13 2023/8/7
 * @Description
 * @Modified By
 */
public class FileUtils {

    public static byte[] fileToBinArray(File file){
        try {
            InputStream fis = new FileInputStream(file);
            return FileCopyUtils.copyToByteArray(fis);
        }catch (Exception ex){
            throw new RuntimeException("transform file into bin Array error",ex);
        }
    }

    public static void writeToLocal(String destination, InputStream input) throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        input.close();
    }

    public static void copyFile(File source, File dest) throws IOException {
        Files.copy(source.toPath(), dest.toPath());
    }
}
