Using Apache Velocity with StringBuilders/CharSequences
Posted
by
mindas
on Stack Overflow
See other posts from Stack Overflow
or by mindas
Published on 2011-03-07T12:20:57Z
Indexed on
2011/03/07
16:10 UTC
Read the original article
Hit count: 251
We are using Apache Velocity for dynamic templates. At the moment Velocity has following methods for evaluation/replacing:
public static boolean evaluate(Context context, Writer writer, String logTag, Reader reader)
public static boolean evaluate(Context context, Writer out, String logTag, String instring)
We use these methods by providing StringWriter
to write evaluation results. Our incoming data is coming in StringBuilder
format so we use StringBuilder.toString
and feed it as instring
.
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 triples the amount of required memory (incoming data + StringBuilder.toString()
which creates a new copy + outgoing data).
I was wondering if there is a way to improve this. E.g. if I could find a way to provide a Reader
and Writer
on top of same StringBuilder
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?
© Stack Overflow or respective owner