Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7092887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:22:14+00:00 2026-05-28T08:22:14+00:00

i found a code but sometimes have error : StringBuilder strHeaders = new StringBuilder();

  • 0

i found a code but sometimes have error :

StringBuilder strHeaders = new StringBuilder();
char c;
while ((c = (char)stream.read()) != -1) {
    strHeaders.append(c);
    if (strHeaders.length() > 5 && (strHeaders.substring((strHeaders.length() - 4), strHeaders.length()).equals("\r\n\r\n"))) {
        // end of headers
        break;
    }
}

logcat

java.lang.OutOfMemoryError
    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:140)
    at java.lang.StringBuilder.append(StringBuilder.java:125)
    at myApp.activity.com.getFromPLS.retreiveMetadata(getFromPLS.java:98)
    at myApp.activity.com.getFromPLS.refreshMeta(getFromPLS.java:76)
    at myApp.activity.com.myApp$1.run(myApp.java:371)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

error on this line

strHeaders.append(c);

please any one can help me to fix this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:22:14+00:00Added an answer on May 28, 2026 at 8:22 am

    Since char is an unsigned type, your while loop condition will never be satisfied, even after stream.read() starts returning -1. If you don’t find a "\r\n\r\n" sequence, the loop will never end. Write the loop like this:

    StringBuilder strHeaders = new StringBuilder();
    int c;
    while ((c = stream.read()) != -1) { // DO NOT cast to char here!
        strHeaders.append((char) c);
        final int len = strHeaders.length();
        if (len > 5 && (strHeaders.substring(len - 4)).equals("\r\n\r\n"))) {
            // end of headers
            break;
        }
    }
    

    Note that many servers will incorrectly return "\n\n" as the blank line that signals the end of the header. Some may also return "\r\r". Unless you control the server output and are guaranteed that it will see "\r\n\r\n", your blank-line detection method needs to be written in a more robust manner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found some code snippet in the internet. But it is missing some
Getting Expected ',' or '{' but found '#44559' error. My code looks like this:
VS 2008 I have this code snippet I found on a VB website. But
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
I found a code samples here: http://sturla.simnet.is/post/2008/09/22/Enable-proxy-in-IE.aspx But it's very complex and involves manipulating
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
I found the following code somewhere, but I am not understanding the code properly.
FindBugs has found a potential bug in my code. But it is not a
I found a lots of ways to call objective-c code from javascript, but I
I've found syntax highlighters that highlight pre-existing code, but I'd like to do it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.