XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.5.1
    • 1.6.0
    • modules

    Description

      I am experiencing 100% CPU on InputStream.read() on 30Kb json data from JSONDataSource.
      Using InputStream.read() is highly inefficient since we don't know the size of the JSON data;
      a reasonably-sized char buffer should be used to cut down on CPU and to lower the method calls on StringBuilder.append().

      Note also that regardless of the solution, there must be a charset conversion between an InputStream and a Reader (or a String object for that matter), and that the cast between int and char in the old code will corrupt an InputStream containing multibyte characters. Since JSON data is usually sent in UTF-8, we could hardcode the conversion in the InputStreamReader to overcome problems on Windows:

      BufferedReader in = new BufferedReader(new InputStreamReader(jsonInputStream,"UTF-8"));
      StringBuilder sb = new StringBuilder(512);
      char[] tempBuf = new char[512];
      int readLen = -1;

      while( (readLen = in.read(tempBuf)) != -1 )
      sb.append(tempBuf,0, readLen);
      tempBuf = null;

      Attachments

        Activity

          People

            isurues Isuru Suriarachchi
            wilypop Jean Marc
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: