Both Writer and CharsetEncoder are designed for char[]. Converting 
BufferedWriter to use byte[] value + byte coder like StringBuilder will also 
require redundant encoding conversion when using LATIN1 String, and the 
performance will not be good.
------------------------------------------------------------------
发件人:Brett Okken <brett.okken...@gmail.com>
发送时间:2025年6月30日(周一) 11:39
收件人:"温绍锦(高铁)"<shaojin.we...@alibaba-inc.com>
抄 送:"core-libs-dev"<core-libs-dev@openjdk.org>
主 题:Re: Eliminate unnecessary buffering and encoding conversion in 
BufferedWriter
Maybe another option would be to implement BufferedWriter with a StringBuilder 
rather than a char[]. This would remove the force to utf-16
On Sun, Jun 29, 2025 at 10:36 PM Brett Okken <brett.okken...@gmail.com 
<mailto:brett.okken...@gmail.com >> wrote:
Is StreamEncoder buffering content to only write to the underlying OutputStream 
when some threshold is hit? While the layers of conversions are unfortunate, it 
seems there could be negative performance implications of having many extremely 
small writes (such as 1 character/byte) at a time to the underlying 
OutputStream.
Presumably this is a common pattern, as it is recommended:
https://github.com/openjdk/jdk/blob/4dd1b3a6100f9e379c7cee3c699d63d0d01144a7/src/java.base/share/classes/java/io/OutputStreamWriter.java#L45
 
<https://github.com/openjdk/jdk/blob/4dd1b3a6100f9e379c7cee3c699d63d0d01144a7/src/java.base/share/classes/java/io/OutputStreamWriter.java#L45
 >
On Sun, Jun 29, 2025 at 11:04 AM wenshao <shaojin.we...@alibaba-inc.com 
<mailto:shaojin.we...@alibaba-inc.com >> wrote:
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

Reply via email to