RFR: 8330748: ByteArrayOutputStream.writeTo(OutputStream) pins carrier [v2]

Jason Mehrens duke at openjdk.org
Tue Apr 23 17:38:31 UTC 2024


On Tue, 23 Apr 2024 16:08:13 GMT, Alan Bateman <alanb at openjdk.org> wrote:

>> src/java.base/share/classes/java/io/ByteArrayOutputStream.java line 164:
>> 
>>> 162:     public void writeTo(OutputStream out) throws IOException {
>>> 163:         if (Thread.currentThread().isVirtual()) {
>>> 164:             out.write(toByteArray());
>> 
>> Would it be better to avoid calling a public method `toByteArray` encase subclass is overriding it?
>
> A good question. The buf/count fields are protected so the subclass has direct access to the bytes. So while it could Arrays.copy the bytes, it doesn't help with a buggy subclass that is changing bytes while synchronization.

I was thinking more of a subclass that counted invocations to public methods or metering which would cause subclass to double the counts when calling via virtual thread.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18901#discussion_r1576637177


More information about the core-libs-dev mailing list