Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't a Reader read *directly* from a StringBuilder?

Tags:

java

java-io

I'm using an API that requires a Reader to read from, and this Reader should actually read from a (potentially very large) StringBuilder.

But using this:

new StringReader(stringBuilder.toString());

...will copy the internal StringBuilder's char array, which I want to avoid due to array size. Although, this char array is package-protected.

And no better luck with StringBuffer :(

Am I missing something?

Note: I can't use Java 7 at this time.

like image 283
xav Avatar asked Jun 17 '26 05:06

xav


1 Answers

StringBuilder implements CharSequence, so you can use CharSequenceReader from Apache commons-io.

like image 129
Chris Martin Avatar answered Jun 18 '26 18:06

Chris Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!