Make java.nio.ByteBuffer a sealed class

Sebastian Stenzel sebastian.stenzel at gmail.com
Fri Jul 23 10:59:57 UTC 2021


Any thoughts on this?

> On 8. Jul 2021, at 16:54, Sebastian Stenzel <sebastian.stenzel at gmail.com> wrote:
> 
> A while back, when working on JDK-8264110, we had a discussion about the nature of ByteBuffers, where I asked whether we could eventually make this a sealed class. [1]
> 
> The idea is, that basically there are only two kinds of ByteBuffers:
> - HeapByteBuffers
> - MappedByteBuffer → DirectByteBuffer
> 
> While the former is backed by an array, the latter isn't, which leads to branching such as `if (buffer.hasArray())`, which could be simplified, if we knew for sure to deal with either of the two kinds.
> 
> Now, that sealed classes are stable with JDK 17, I'd like to ask for your approval to make ByteBuffer a sealed class, so we can rely on an exhaustive list of ByteBuffer "flavours" at compile time.
> 
> This is a requirement for follow-up tasks, I'd like to work on, such as JDK-8264341.
> 
> 
> [1] https://github.com/openjdk/jdk/pull/3217#discussion_r602528233



More information about the nio-dev mailing list