0

I am getting a very large STRING for server.

Background: Actually I am converting an image into Base69 string and storing it on my server, than I am calling a service to get back the String. And I am trying to store the String into a String variable like this

String pic="";
if (payload.substring(i, i+10).equals("<picture >")) {
  for (int j=i+10;j<payload.length();j++) {
    if (payload.substring(j, j+5).equals("</pic")) {
      user.setCountry(country);
      user.setDOB(dob);
      user.setUsername(uname);
      user.setPic(pic);
      users.add(user);
      country="";
      uname="";
      pic="";
      dob="";
      i=j+5;
      break;
    } else {
      pic = pic + payload.substring(j, j+1);
    }

But the issue is that after sometime my program crashes and I did not get complete String into the variable pic

Is there a way to handle very long strings coming from the server? I mean to say how can I get the same Base64 String in my variable pic completely so that I decode it back to the picture.

Robert
  • 10,403
  • 14
  • 67
  • 117
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55
  • are there errors/exceptions being thrown? – Sionnach733 Apr 11 '14 at 13:40
  • You could use StringBulder like it says in your tags, not a String as it says in your code. But I don't see any reason to build up 'pic' at all. Just compute its start and end indexes, and use 'payload.substring(start, end+1)' to create it when you find the end: which in turn you should do with indexOf() rather than your own loop. – user207421 Apr 11 '14 at 13:41
  • You do not need to convert to base64 to store in database. You can store image as blob. Then you can return it as byte array. You can also convert it to base64 before sending it. – amit Apr 11 '14 at 13:51
  • @BobMalooga sorry, its Base64. – Hassaan Rabbani Apr 11 '14 at 14:10
  • @mangusta when i am sending the base64 string i am storing the whole string into one string variable. so when i am getting it back from server i am trying same, than why do i need to increase the heap size? just asking – Hassaan Rabbani Apr 11 '14 at 14:12
  • @EJP good point, let me do it your way – Hassaan Rabbani Apr 11 '14 at 14:12
  • Are you trying to write a makeshift XML parser? Don't. research javax.xml.parsers – Bex Apr 11 '14 at 14:16
  • parsing is not an issue, i just want to store a very large string – Hassaan Rabbani Apr 11 '14 at 14:17
  • @HassaanRabbani sorry, this part "storing it on my server" of your question ran out of my attention – mangusta Apr 12 '14 at 06:28

2 Answers2

1

Find the end of what you want with something like:

int endPos = payload.indexOf("</pic>", currentPos);

and then do one substring that take the full picture data:

String pic = payload.substring(currentPos, endPos);
Ebbe M. Pedersen
  • 7,250
  • 3
  • 27
  • 47
0

Apart from the fact that the code in the if statement seems unrelated to the string at hand, you have String concatenation in a loop in your else statement, which kills performance.

Another point of concern is your abuse of substring(), see Time complexity of Java's substring() (Maybe consider converting the string to an Array or stream?)

Community
  • 1
  • 1
Ordous
  • 3,844
  • 15
  • 25