package com.neusoft.smart.pos.framework.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;


public class FileUtils {

    /**
     * 文件读取方法
     * @param path:文件路径
     * @return
     */
    public static InputStream readFile(String path){
        File file = new File(FileUtils.class.getResource(path).getPath());
        //InputStream，并关联对应的文件，创建对象的时候会出现异常
        InputStream instream = null;
        try {
            instream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return instream;
    }

    /**
     * 文件读取方法
     * @param file:文件对象
     * @return
     */
    public static InputStream readFile(File file){
        //创建FileInputStream对象，并关联对应的文件，创建对象的时候会出现异常
        InputStream instream = null;
        try {
            instream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return instream;
    }

    /**
     * 将输入流转为字符串方法
     * @param file
     * @return
     */
    public static byte[] convertFileToByteArray(MultipartFile file) {
        try {
            return convertInputStreamToByteArray(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将字符串转为输入流方法
     * @param value
     * @return
     */
    public static InputStream convertStringToInputStream(String value){
        ByteArrayInputStream stream = new ByteArrayInputStream(value.getBytes());
        return stream;
    }

    /**
     * 将字符数组转为输入流方法
     * @param value
     * @return
     */
    public static InputStream convertByteArrayToInputStream(byte[] value){
        ByteArrayInputStream stream = new ByteArrayInputStream(value);
        return stream;
    }

    /**
     * 将输入流转为字符串方法
     * @param stream
     * @return
     */
    public static String convertInputStreamToString(InputStream stream) {
        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try{
            while ((line = in.readLine()) != null){
                buffer.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return buffer.toString();
    }

    /**
     * 将输入流转为字符串方法
     * @param stream
     * @return
     */
    public static byte[] convertInputStreamToByteArray(InputStream stream) {
        byte[] content = null;
        try {
            content = new byte[stream.available()];
            stream.read(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

    /**
     * 将输入流转为字符串方法
     * @return
     */
    public static File convertByteArrayToFile(byte[] bytes) {
        File file = new File("");
        OutputStream output = null;
        try {
            output = new FileOutputStream(file);
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
            bufferedOutput.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

}
