Java escape HTML - string replace slow?
<p>I have a Java application that makes heavy use of a large file, to read, process and give through to SolrEmbeddedServer (http://lucene.apache.org/solr/).</p>

<p>One of the functions does basic HTML escaping:</p>

<pre><code>private String htmlEscape(String input)
{
    return input.replace("&amp;", "&amp;amp;").replace("&gt;", "&amp;gt;").replace("&lt;", "&amp;lt;")
        .replace("'", "&amp;apos;").replaceAll("\"", "&amp;quot;");
}
</code></pre>

<p>While profiling the application, the program spends roughly 58% of the time in this function, a total of 47% in replace, and 11% in replaceAll.</p>

<p>Now, is the Java replace that slow, or am I on the right path and should I consider the program efficient enough to have its bottleneck in Java and not in my code? (Or am I replacing wrong?)</p>

<p>Thanks in advance!</p>
