RFR: 8361018: Eliminate unnecessary buffering and encoding conversion in BufferedWriter

Shaojin Wen swen at openjdk.org
Mon Jun 30 04:54:50 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. 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.

LATIN1 -> UTF16 -> UTF8

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.

-------------

Commit messages:
 - @Override & non-public
 - Update src/java.base/share/classes/java/io/BufferedWriter.java
 - final & private inner class
 - benchmark parameter charset
 - inner class
 - only UTF8
 - revert PrintStream
 - inner class
 - inner class
 - add benchmark
 - ... and 4 more: https://git.openjdk.org/jdk/compare/da7080ff...d40e3d65

Changes: https://git.openjdk.org/jdk/pull/26022/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=26022&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8361018
  Stats: 598 lines in 7 files changed: 474 ins; 83 del; 41 mod
  Patch: https://git.openjdk.org/jdk/pull/26022.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/26022/head:pull/26022

PR: https://git.openjdk.org/jdk/pull/26022


More information about the core-libs-dev mailing list