package thyyxxk.webserver.utils;

import com.alibaba.fastjson.JSONObject;

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

public class StreamWork {

    public static JSONObject start(HttpURLConnection conn) throws Exception {
        InputStream inputStream = conn.getInputStream();
        JSONObject obj = JSONObject.parseObject(readInputStream(inputStream));
        conn.disconnect();
        return obj;
    }

    public static String readInputStream(InputStream inputStream) throws Exception {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String str;
        StringBuilder builder = new StringBuilder();
        while ((str = bufferedReader.readLine()) != null) {
            builder.append(str);
        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        return builder.toString();
    }
}
