package com.android.common.utils;

import android.content.res.AssetManager;
import android.text.TextUtils;
import android.util.Log;

import com.orhanobut.logger.Logger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author 910040
 * @version 1.0
 * @date 2018/11/8
 */
public class IOUtils {

    /**
     * 将Io流转换成String文本
     *
     * @param in 输入流
     * @return String
     */
    public static String inputStream2String(InputStream in) {
        String result = "";
        try {
            byte[] bytes = new byte[in.available()];
            in.read(bytes, 0, bytes.length);
            result = new String(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 读取目录下的文件流信息
     *
     * @param filePath 文件存储位置
     * @return InputStream
     */
    public static InputStream getIsFromFilePath(String filePath) {
        InputStream inputStream = null;
        if (!TextUtils.isEmpty(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                try {
                    inputStream = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return inputStream;
    }

    /**
     * 读取assert文件流信息
     *
     * @return InputStream
     */
    public static InputStream getIsFromAssert(String fileName) {
        AssetManager am;
        InputStream inStream = null;
        am = AppUtils.getAppContext().getAssets();
        try {
            inStream = am.open(fileName);
        } catch (IOException e) {
            Logger.d(e);
        }
        return inStream;
    }

    /**
     * 读取Raw/configure文件流信息
     *
     * @return InputStream
     */
    public static InputStream getIsFromRaw(int resId) {
        InputStream inStream;
        inStream = AppUtils.getAppContext()
                .getResources().openRawResource(resId);
        return inStream;
    }

    /**
     * 拷贝文件到本地
     *
     * @param inStream  输入流
     * @param outStream 输出流
     */
    public static void copyFile(InputStream inStream, OutputStream outStream) {
        if (inStream == null || outStream == null) {
            Log.e("jglee", "inStream is null or outStream is null");
            return;
        }
        BufferedInputStream bufferInput = new BufferedInputStream(inStream);
        BufferedOutputStream bufferOutput = new BufferedOutputStream(outStream);
        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = bufferInput.read(buffer)) != -1) {
                bufferOutput.write(buffer, 0, length);
            }
            bufferOutput.flush();
        } catch (IOException e) {
            Logger.d("copyFile--->" + e.getMessage());
        } finally {
            closeIo(inStream);
            closeIo(outStream);
            closeIo(bufferInput);
            closeIo(bufferOutput);
        }
    }


    /**
     * 关闭IO流
     * 注意需要在finally里关闭
     *
     * @param closeable
     */
    public static void closeIo(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
