Search code examples
androidarraysarrayliststringbuilderstringbuffer

How to concatenate long String in StringBuffer for Android


I am facing one problem in StringBuffer concatination for appending large characters of String from JSONArray. My data is huge and it is coming in log after iteration of 205 indexes of Array properly but when I am appending each row String in StringBuffer or StringBuilder from JSONArray, so it is taking on 4063 characters only not appending all characters present in JSON Array but iteration doesn't break and goes till complete 204 rows.

                String outputFinal = null;

                try {

                    StringBuilder cryptedString = new StringBuilder(1000000);

                    JSONObject object = new JSONObject(response);

                    JSONArray serverCustArr = object.getJSONArray("ServerData");

                    Log.d("TAG", "SurverCust Arr "+serverCustArr.length());

                    for (int i = 0; i < serverCustArr.length(); i++) {

                        String loclCryptStr = serverCustArr.getString(i);

                        Log.d("TAG", "Loop Count : "+i);

                        cryptedString.append(loclCryptStr);
                    }

                    Log.d("TAG", "Output :"+cryptedString.toString());


                    CryptLib _crypt = new CryptLib();

                    String key = this.preference.getEncryptionKey();

                    String iv = this.preference.getEncryptionIV();

                    outputFinal = _crypt.decrypt(cryptedString.toString(), key,iv); //decrypt
                    System.out.println("decrypted text=" + outputFinal);

              } catch (Exception e) {

                e.printStackTrace();
              } 

My JSONArray contacts 119797 characters in 205 and after iteration for appending in StringBuffer, I have to decrypt it with library that takes string for decryption. But StringBuffer is not having complete data of 119797 characters.

And Exception is because string is not complete, I am enclosing files on link below for reference and also using cross platform CryptLib uses AES 256 for encryption easily find on Github

3 Files With Original and Logged text


Solution

  • Dont use StringBuffer , instead use StringBuilder ..here's the detailed Explaination

    http://stackoverflow.com/questions/11908665/max-size-for-string-buffer
    

    Hope this helps. :)

    EDIT

    this is the code that i used to read whole string ...

    public void parseLongString(String sourceFile, String path) {
            String sourceString = "";
            try {
                BufferedReader br = new BufferedReader(new FileReader(sourceFile));
                // use this for getting Keys Listing as Input
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
    
                while (line != null) {
                    sb.append(line);
                    line = br.readLine();
                }
                br.close();
                sourceString = sb.toString();
                sourceString = sourceString.toUpperCase();
    
                System.out.println(sourceString.length());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            File file = new File(path);
            FileWriter fileWriter = null;
            BufferedWriter bufferFileWriter = null;
            try {
                fileWriter = new FileWriter(file, true);
                bufferFileWriter = new BufferedWriter(fileWriter);
            } catch (IOException e1) {
                System.out.println(" IOException");
                e1.printStackTrace();
            }
            try {
                fileWriter.append(sourceString);
                bufferFileWriter.close();
                fileWriter.close();
            } catch (IOException e) {
                System.out.println("IOException");
                e.printStackTrace();
            }
    
        }
    

    and this is outPut file where i am just converting it to uppercase .

    https://www.dropbox.com/s/yecq0wfeao672hu/RealTextCypher%20copy_replaced.txt?dl=0
    

    hope this helps!

    EDIT 2

    If u are still looking for something ..you can also try STRINGWRITER syntax would be

    StringWriter writer = new StringWriter();
            try {
                IOUtils.copy(request.getInputStream(), writer, "UTF-8");
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            String theString = writer.toString();