RFR: 8306308: (ch) Writer created by Channels::newWriter may lose data
Daniel Jeliński
djelinski at openjdk.org
Tue Oct 17 06:41:24 UTC 2023
On Mon, 16 Oct 2023 21:20:17 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:
> Change `Channels.newWriter` to return a `Writer` created by first wrapping the `WritableByteChannel` in an `OutputStream` using `Channels.newOutputStream` and then wrapping the result in an `OutputStreamWriter`. With this change in place, the problems reported in the issue do not occur.
Yes, that's what I had in mind.
Note that this was the only place where `StreamEncoder.forEncoder` was used, which in turn was the only place where `StreamEncoder(WritableByteChannel ch, CharsetEncoder enc, int mbc)` was used, which in turn was the only place where `StreamEncoder#ch` was set, so this PR has potential to make the code much simpler.
src/java.base/share/classes/java/nio/channels/Channels.java line 544:
> 542: {
> 543: Objects.requireNonNull(ch, "ch");
> 544: return StreamEncoder.forEncoder(ch, enc.reset(), minBufferCap);
You could avoid one level of indirection by using `StreamEncoder.forOutputStreamWriter` instead of `new OutputStreamWriter` here. `OutputStreamWriter` is a pass-through wrapper for `StreamEncoder.forOutputStreamWriter` (except for [JDK-6856817](https://bugs.openjdk.org/browse/JDK-6856817) / dc93a924ee4d3b53f0271af577148f8a53eedb57, but that's another story)
-------------
PR Review: https://git.openjdk.org/jdk/pull/16207#pullrequestreview-1681459462
PR Review Comment: https://git.openjdk.org/jdk/pull/16207#discussion_r1361569579
More information about the nio-dev
mailing list