package com.qmth.boot.data.upgrade.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class ResourceFileHelper {

    private static final Logger log = LoggerFactory.getLogger(ResourceFileHelper.class);

    public static String readContent(String resourcePath) {
        try {
            InputStream is = ResourceFileHelper.class.getClassLoader().getResourceAsStream(resourcePath);
            if (is == null) {
                throw new RuntimeException(resourcePath + " file is not exist!");
            }

            return ResourceFileHelper.readContent(is);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String readContent(InputStream is) {
        try (InputStreamReader isReader = new InputStreamReader(is, StandardCharsets.UTF_8);
             BufferedReader bfReader = new BufferedReader(isReader);) {
            StringBuilder result = new StringBuilder();

            String line;
            while ((line = bfReader.readLine()) != null) {
                result.append(line).append("\n");
            }
            return result.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
