package com.emato.ich.utils;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

public class ResourceUtils {

    private static final String TAG = ResourceUtils.class.getName();

    public static String readStringFromAssert(String s, AssetManager am) {

//        s = "file://android_assets/" + s;
//
//        File file = new File(s);

        int len = 0;
        StringBuilder stringBuffer = new StringBuilder();
        InputStream is = null;
        try {
            is = am.open(s);
            byte[] bytes = new byte[1024];
            while ((len = (is.read(bytes))) != -1) {
                stringBuffer.append(new String(bytes, 0, len));
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            Log.e(TAG, "readStringFromAssert: 读取json文件失败! ", e);
            LoggingUtils.sendErrorLog("业务异常: 读取json文件失败! ", e);
        } catch (Exception e) {
            Log.e(TAG, "readStringFromAssert: 读取json文件未知错误! ", e);
            LoggingUtils.sendErrorLog("业务异常: 读取json文件未知错误! ", e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, "readStringFromAssert: 关闭流错误! ", e);
                    LoggingUtils.sendErrorLog("业务异常: 关闭流错误! ", e);
                }
            }
        }


        return null;
    }
}
