package org.jabref.logic.importer.util;

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

import org.jabref.logic.importer.ParseException;

import kong.unirest.json.JSONObject;

/**
 * Converts an {@link InputStream} into a {@link JSONObject}.
 */
public class JsonReader {

    public static JSONObject toJsonObject(InputStreamReader input) throws ParseException {
        BufferedReader streamReader = new BufferedReader(input);
        StringBuilder responseStrBuilder = new StringBuilder();

        try {
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            return new JSONObject(responseStrBuilder.toString());
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }

    public static JSONObject toJsonObject(InputStream input) throws ParseException {
        return toJsonObject(new InputStreamReader(input, StandardCharsets.UTF_8));
    }
}

package org.jabref.logic.importer.util;

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

import org.jabref.logic.importer.ParseException;

import kong.unirest.json.JSONObject;

/**
 * Converts an {@link InputStream} into a {@link JSONObject}.
 */
public class JsonReader {

    public static JSONObject toJsonObject(InputStreamReader input) throws ParseException {
        BufferedReader streamReader = new BufferedReader(input);
        StringBuilder responseStrBuilder = new StringBuilder();

        try {
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            if (responseStrBuilder.toString().isBlank()) {
                throw new ParseException("Empty input!");
            }
            return new JSONObject(responseStrBuilder.toString());
        } catch (IOException e) {
            throw new ParseException(e);
        }
    }

    public static JSONObject toJsonObject(InputStream input) throws ParseException {
        return toJsonObject(new InputStreamReader(input, StandardCharsets.UTF_8));
    }
}

throw parseException when input is empty