package net.northking.cctp.element.util;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author fengxiang.zhang
 * @since 1.0
 */
public class AtpUtils {

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return
     */
    public static String camelCaseName(String str) {
        String[] parts = StringUtils.split(str, "_");
        for (int i = 0; i < parts.length; i++) {
            parts[i] = StringUtils.capitalize(parts[i].toLowerCase());
        }
        return StringUtils.uncapitalize(StringUtils.join(parts));
    }

    public static String readWithLineFeed(InputStream inputStream, String charsetName) throws IOException {
        Assert.notNull(inputStream);
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        try {
            br = new BufferedReader(new InputStreamReader(inputStream, charsetName));
            String s;
            while ((s = br.readLine()) != null) {
                sb.append(s);
                sb.append("\n");
            }
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
            }
        }
        return sb.toString();
    }

}