1

I am using post man to send the JSon request. Then I get the inputStream using the getInputStream().

    InputStream inputStream = request.getInputStream();

I have a JSon request with 2032 character and it might increase based on the scenarios. I tried few suggestions for the similar kind of issue, but using all I would be able to read only 1011 character.

Below are the ways which I tried.

Declarations:

    BufferedReader bufferedReader = null;
    StringBuilder stringBuilder = new StringBuilder();
    // stringBuilder.ensureCapacity(1048576);
    JSONObject jObj = null;
    InputStream inputStream = request.getInputStream();

1)

        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        char[] charBuffer = new char[1048576];
        int bytesRead = -1;
        while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
            stringBuilder.append(charBuffer, 0, bytesRead);
        }

2)

        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;
        inputStream.close();

3)

        String line;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

4)

        stringBuilder.ensureCapacity(1048576);
        BoundedInputStream boundedInputStream = new BoundedInputStream(inputStream);
        bufferedReader = new BufferedReader(new InputStreamReader(boundedInputStream, "UTF-8"));

        // StringBuilder builder= new StringBuilder();
        StringBuilderWriter bufferedwriter = new StringBuilderWriter(stringBuilder);
        IOUtils.copy(bufferedReader, bufferedwriter);

5)

         bufferedReader = request.getReader();
         char[] charBuffer = new char[1048576];
         int bytesRead = -1;
         while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
         stringBuilder.append(charBuffer, 0, bytesRead);
         }

Final Consumption: Used the second variation result was my latest try

            // jObj = new JSONObject(stringBuilder.toString());
            // jObj = new JSONObject(bufferedwriter.toString());
            jObj = new JSONObject(result.toString());

Note: I was just verifying by increasing the char capacity to 1048576 to see if that would solve. But increasing that also have no effect on the inputstream.

Could anyone of you please advise me on how to read large Json input. Also let me know if I am doing it wrong.

Thanks in advance.

1 Answers1

1

You seem to want to convert the JSON into a String. With Java 8 this has become a bit simpler.

    // (1)
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
        // (2)
        String json = reader.lines().collect(Collectors.joining("\n"));
        // do something with `json`...

    }

Explained:

  1. Create a BufferedReader from the input stream. Using "try-with-resources" means, that the reader will be automatically closed when leaving the try {} block.
  2. The BufferedReader has a method lines() which returns a Stream<String>. You can simply join all Strings using the joining collector.
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
  • Is Java 8 means JDK 8? Will this simple approach only available for JDK 8 ? or will it work in JDK 7 ?. Because my project is based on JDK 7 / JRE 7. Since I have to integrate the Java front end to ABAP back end using HCI Connect. And HCI mainly supports JVM 7. So I couldn't use JDK 8. But I will try the above approach and update you about the outcome. Thank you very much for the valuable inputs. – Santosh Kumar J Mar 13 '16 at 18:08
  • Hi Moritz, I tried your solution and I see this don't work in JDK 7. We don't have the method lines() in BufferedReader, instead can get method readline(). Can you advise me some other alternative to achieve in Java 7. Sorry, I am unable to Vote your answer, since I don't have 15 reputation points. – Santosh Kumar J Mar 14 '16 at 02:56