Eliminate unnecessary buffering and encoding conversion in BufferedWriter

wenshao shaojin.wensj at alibaba-inc.com
Sun Jun 29 16:03:18 UTC 2025


BufferedWriter -> OutputStreamWriter -> StreamEncoder
In this call chain, BufferedWriter has a char[] buffer, and StreamEncoder has a ByteBuffer. There are two layers of cache here, or the BufferedWriter layer can be removed. 
LATIN1 (byte[]) -> UTF16 (char[]) -> UTF8 (byte[])
And when charset is UTF8, if the content of write(String) is LATIN1, a conversion from LATIN1 to UTF16 and then to LATIN1 will occur here.
We can improve BufferedWriter. When the parameter Writer instanceof OutputStreamWriter is passed in, remove the cache and call it directly. In addition, improve write(String) in StreamEncoder to avoid unnecessary encoding conversion.
-
Shaojin Wen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250630/fec03be7/attachment.htm>


More information about the core-libs-dev mailing list