title: Using Apache Velocity with StringBuilders/CharSequences
body: <p>We are using Apache Velocity for dynamic templates. At the moment Velocity has following methods for evaluation/replacing:</p>
  
  <pre><code>public static boolean evaluate(Context context, Writer writer, String logTag, Reader reader)
  
  public static boolean evaluate(Context context, Writer out, String logTag, String instring)
  </code></pre>
  
  <p>We use these methods by providing <code>StringWriter</code> to write evaluation results. Our incoming data is coming in <code>StringBuilder</code> format so we use <code>StringBuilder.toString</code> and feed it as <code>instring</code>.</p>
  
  <p>The problem is that our templates are fairly large (can be megabytes, tens of Ms on rare cases), replacements occur very frequently and each replacement operation <strong>triples</strong> the amount of required memory (incoming data + <code>StringBuilder.toString()</code> which creates a new copy + outgoing data).</p>
  
  <p>I was wondering if there is a way to improve this. E.g. if I could find a way to provide a <code>Reader</code> and <code>Writer</code> on top of same <code>StringBuilder</code> instance that only uses extra memory for in/out differences, would that be a good approach? Has anybody done anything similar and could share any source for such a class? Or maybe there any better solutions to given problem?</p>