Request for Enhancement: java.io.Writer.of(Appendable) as an efficient alternative to java.io.StringWriter
Alan Bateman
alan.bateman at oracle.com
Wed May 7 06:54:33 UTC 2025
On 15/03/2025 17:58, Markus KARG wrote:
> Chen,
>
> thank you for sharing your opinion!
>
> Thinking about what you wrote about the "trifecta" complexity, I think
> it might be better to restart my idea from scratch:
>
> As explained in my original proposal
> (https://mail.openjdk.org/pipermail/core-libs-dev/2024-December/137807.html),
> the actual driver for my proposal was to provide a StringWriter
> alternative which solves two main problems: It shall prevent String
> copies, and it shall be non-synchronized.
>
> What comes into mind is: Writer.of(StringBuilder).
>
> While compared to Appendable this signature is much less flexible, it
> also makes less headaches, but solved in fact those 99% of cases that
> triggered this whole idea: It does not create String copies, and it is
> non-synchronized. What this writer would simply, simply would be
> routing all incoming "append" and "write" calls down to the provided
> string builder.
>
> Hence, kindly asking for comments on this updated idea: WDYT about
> Writer.of(StringBuilder)?
I think the main issue with Writer.of(SB) is that the usages would
requiring hopping between SB, Writer, then back to SB. That is, you
start accumulating characters in the SB, then wrap it with a Writer to
write/append characters with a different API, be careful to flush/close
so that all characters get to the SB, then back to the SB to append more
before creating the String. That seems a bit fragile and very different
to the decoration/wrapping when using reader/writers.
You've made it clear that your motivation is performance and avoiding
the synchronization and internal buffer of StringWriter. I think it
would be useful if you could expand a bit on the pain points. Maye this
is about rehabilitating StringWriter or maybe it's about making it
easier and more efficient to bridge between StringWriter and other
Appendables. Expanding more on the problem might help to give you some
suggestions and directions to explore and prototype.
-Alan
More information about the core-libs-dev
mailing list