0

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

user366584
  • 1,016
  • 1
  • 16
  • 34
  • 2
    I don't understand. Either I should understand better, or you should explain better. – wvdz Apr 15 '15 at 14:11
  • Well I am looping to add My JSON Array data into StringBuffer to be used in Decrypted library but my StringBuffer heap is overflowing and only able to add 4063 bytes and not the original JSON Array traversing. – user366584 Apr 15 '15 at 14:27
  • The description you gave in the question is quite confusing please clearly mention what exactly is the problem ? – Umair Apr 15 '15 at 14:28
  • hope this modification help – user366584 Apr 15 '15 at 14:39
  • The length of the strings doesn't matter. It sounds like you're not getting the correct data, and so your program is failing; you will need to figure out what exactly is wrong with your data. – fadden Apr 15 '15 at 18:11
  • I have added original file for complete string in the specified link can you please help me that why my original String is not binding in StringBuilder – user366584 Apr 16 '15 at 09:25

1 Answers1

1

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();
  • No it is also not helping :( – user366584 Apr 15 '15 at 15:36
  • can you just place snippet here , which u replace now along with the printstacktrace, that will help! – Jivraj S Shekhawat Apr 15 '15 at 15:47
  • Please find PrintStack and logged files, which are placed in the shared linked on updated Question – user366584 Apr 15 '15 at 16:49
  • 1
    exception log is indicating that you are not using proper encrytion here, may be you are trying to decrpt bytearray from hexString. or also it may be the case u are trying to append String after cyper.dofinal ; which is not feasible when u are into append mode.. Trying using update method instead of dofinal and when the whole buffer is read , you can use cypher.doFinal(); morover change the tag to cryptography and add the code here. that would surely help.! – Jivraj S Shekhawat Apr 15 '15 at 18:26
  • Can you help in writing some sample code for more better understanding. – user366584 Apr 16 '15 at 08:31
  • try { JSONObject object = new JSONObject(response); JSONArray serverCustArr = object.getJSONArray("ResponseData"); for (int i = 0; i < serverCustArr.length(); i++) { String loclCryptStr = serverCustArr.getString(i); FileUtil.writeFile(activity, "EncryptFile.txt", loclCryptStr); } Log.d("TAG", FileUtil.readFile(this.activity, "EncryptFile.txt")); MyApplication.writeToFile(""); } catch (Exception e) { e.printStackTrace(); } – user366584 Apr 19 '15 at 10:53
  • Still Unable to decrypt as File is not completely writing. – user366584 Apr 19 '15 at 10:53