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

  • SEARCH
  • Home
  • 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 7605537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:11:34+00:00 2026-05-31T00:11:34+00:00

I would like to obtain a few comments in regards to optimising my method

  • 0

I would like to obtain a few comments in regards to optimising my method created for writing of the whole content’s of StringBuilder to a ServletResponse.

I did it to avoid creation of gigantic Strings at a single go before passing it to the out.write() method. In my situation a StringBuilder’s content length, in some occasions, arrives at few million characters.

public static void writeResponse(ServletResponse response, StringBuilder sb) throws IOException {
    try (PrintWriter out = response.getWriter()) {
        int length = sb.length();
        //to avoid creation of gigantic strings we are writing substrings from the sb 
        int bufferSize = (response.getBufferSize() != 0? response.getBufferSize():10000);
        log.log(Level.INFO, "READY TO SEND To CLIENT, length of responseSB={0}", length);
        if (length <= bufferSize) {
            out.write(sb.toString());
        } else {
            int noWrites = length / bufferSize;
            for (int i = 0; i < noWrites; i++) {
                out.write(sb.substring(i * bufferSize, (i + 1) * bufferSize));
                log.log(Level.INFO, "SENDING To CLIENT, write no={0} of {1}", new Object[]{(i + 1), noWrites});
            }
            int rest = length % bufferSize;
            if (rest != 0) {
                out.write(sb.substring(length - rest, length));
            }
        }
    }
}

I want it to write a single (not chunked) message. Thus, I would like to know how accurately establish a response’s buffer size in relation to a number of characters (or a String’s length) it can fit?

At the moment, I am taking the current buffer size and using it as if it was expressing a number of characters it can fit, how to correctly evaluate the buffer size? Also I am not including the header size, how could I achieve it?

I would like to optimise its performance to maximum (so it works the fastest), any suggestion is much appreciated. Or maybe there is all together a better way of writing a gigantic StringBuilder content to ServletResponse?

  • 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-31T00:11:35+00:00Added an answer on May 31, 2026 at 12:11 am

    The fastest way is:

    out.write(sb.toString());
    

    If you want to save on memory, replace StringBuilder with PrintWriter and pass response.getWriter() around.

    Any optimization regarding buffer sizes will only make things slower. Without optimization, the cost is roughly: StringBuilder.toString() + out.write() which passed the long string to the container for chunking/sending.

    With your optimization, it looks like this: StringBuilder.toString() + substring() + out.write() + copying substring into send buffer + many calls to container to send the pieces.

    If you get rid of the builder, the number of calls to the container will stay the same (out.write() uses an internal buffer) but you won’t waste memory to keep data around.

    If you want to keep the StringBuilder, then find out how big the pages are and create a StringBuilder with a non-default size so it doesn’t have to extend it’s internal buffer all the time.

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

Sidebar

Related Questions

After highlighting text, I would like to obtain the paragraph in which the selected
I would like to be able to obtain all the parameter values from the
I would like to know the procedure to adopt to parse and obtain text
I would like to obtain a list of files from a git repository that
I would like to obtain the following kind of routing: HTTP POST message with
i have a few filters on a sprite. on mouse over i would like
I would like to obtain a BitBlt() screenshot of a control with big height
I would like to obtain a radial gradient effect on an image ( alpha
I would like to obtain button links with an image over the lines. I
Is it possible to obtain class summary at runtime in C#? I would like

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.