package com.cm.common.utils;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import java.io.*;

/**
 * @ClassName: ResourceUtil
 * @Description: 资源工具
 * @Author: WangGeng
 * @Date: 2019-05-23 11:04
 * @Version: 1.0
 **/
public class ResourceUtil {

    /**
     * 资源输入流
     *
     * @param resourcePath
     * @return
     * @throws IOException
     */
    public static InputStream getResourceInputStream(String resourcePath) throws IOException {
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        return resourceLoader.getResource(resourcePath).getInputStream();
    }

    /**
     * 资源文件
     *
     * @param resourcePath
     * @return
     * @throws IOException
     */
    public static File getResourceFile(String resourcePath) throws IOException {
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        return resourceLoader.getResource(resourcePath).getFile();
    }

    /**
     * 输入流输出文本
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static String inputStreamToText(InputStream inputStream) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuffer resultStr = new StringBuffer();
        for (String line; (line = bufferedReader.readLine()) != null; ) {
            resultStr.append(line);
        }
        bufferedReader.close();
        return resultStr.toString();
    }

}
