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 3279940
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:38:56+00:00 2026-05-17T19:38:56+00:00

I have an embedded device which runs Java applications which can among other things

  • 0

I have an embedded device which runs Java applications which can among other things serve up XHTML web pages (I could write the pages as something other than XHTML, but I’m aiming for that for now).

When a request for a web page handled by my application is received a method is called in my code with all the information on the request including an output stream to display the page.

On one of my pages I would like to display a (log) file, which can be up to 1 MB in size.

I can display this file unescaped using the following code:

final PrintWriter writer; // Is initialized to a PrintWriter writing to the output stream.
final FileInputStream fis = new FileInputStream(file);
final InputStreamReader inputStreamReader = new InputStreamReader(fis);
try {
    writer.println("<div id=\"log\" style=\"white-space: pre-wrap; word-wrap: break-word\">");
    writer.println("    <pre>");
    int length;
    char[] buffer = new char[1024];
    while ((length = inputStreamReader.read(buffer)) != -1) {
        writer.write(buffer, 0, length);
    }
    writer.println("    </pre>");
    writer.println("</div>");
} finally {
    if (inputStreamReader != null) {
        inputStreamReader.close();
    }
}

This works reasonably well, and displays the entire file within a second or two (an acceptable timeframe).

This file can (and in practice, does) contain characters which are invalid XHTML, most commonly <>. So I need to find a way to escape these characters.

The first thing I tried was a CDATA section, but as documented here they do not display correctly in IE8.

The second thing I tried was a method like the following:

// Based on code: https://stackoverflow.com/questions/439298/best-way-to-encode-text-data-for-xml-in-java/440296#440296
// Modified to write directly to the stream to avoid creating extra objects.
private static void writeXmlEscaped(PrintWriter writer, char[] buffer, int offset, int length) {
    for (int i = offset; i < length; i++) {
        char ch = buffer[i];

        boolean controlCharacter = ch < 32;
        boolean unicodeButNotAscii = ch > 126;
        boolean characterWithSpecialMeaningInXML = ch == '<' || ch == '&' || ch == '>';

        if (characterWithSpecialMeaningInXML || unicodeButNotAscii || controlCharacter) {
            writer.write("&#" + (int) ch + ";");
        } else {
            writer.write(ch);
        }
    }
}

This correctly escapes the characters (I was going to expand it to escape HTML invalid characters if needed), but the web page then takes 15+ seconds to display and other resources on the page (images, css stylesheet) intermittently fail to load (I believe due to the requests for them timing out because the processor is pegged).

I’ve tried using a BufferedWriter in front of the PrintWriter as well as changing the buffer size (both for reading the file and for the BufferedWriter) in various ways, with no improvement.

Is there a way to escape all XHTML invalid characters that does not require iterating over every single character in the stream? Failing that is there a way to speed up my code enough to display these files within a couple seconds?

I’ll consider reducing the size of the log files if I have to, but I was hoping to make them at least 250-500 KB in size (with 1 MB being ideal).

I already have a method to simply download the log files, but I would like to display them in browser as well for simple troubleshooting/perusal.

If there’s a way to set the headers so that IE8/Firefox will simply display the file in browser as a text file I would consider that as an alternative (and have an entire page dedicated to the file with no XHTML of any kind).


EDIT:

After making the change suggested by Cameron Skinner and performance testing it looks like the escaped writing takes about 1.5-2x as long as the block-written version. It’s not nothing, but I’m probably not going to be able to get a huge speedup by messing with it.

I may just need to reduce the max size of the log file.

  • 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-17T19:38:56+00:00Added an answer on May 17, 2026 at 7:38 pm

    One small change that will (well, might) significantly increase the speed is to change

    writer.write("&#" + (int) ch + ";");
    

    to

    writer.write("&#");
    writer.write((int)ch);
    writer.write(";");
    

    String concatenation is extremely expensive as Java allocates a new temporary string buffer for each + operator, so you are generating two temporary buffers each time there is a character that needs replacing.

    EDIT: One of the comments on another answer is highly relevant: find where the slow bit is first. I’d suggest testing logs that have no characters to be escaped and many characters to be escaped.

    I think you should make the suggested change anyway because it costs you only a few seconds of your time.

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

Sidebar

Related Questions

I have been tasked to write a device driver for an embedded device which
We have made a device which can act as an USB host or slave
We have an embedded device that needs to interact with an enterprise software system.
I have an embedded PowerPoint presentation in an Excel workbook. How can I edit
We have a number of embedded systems requiring r/w access to the filesystem which
I'm having an embedded device which keeps a list of inner tables. I would
I'm in charge of updating an existing java app for an embedded device (a
My application needs to communicate with a embedded device which is about 1MHz clock
I have embedded a Python interpreter in a C program. Suppose the C program
I have an embedded webserver that has a total of 2 Megs of space

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.