ByteBuffer cannot be extended because constructors have default visibility

Florian Weimer fw at deneb.enyo.de
Fri Apr 24 17:46:26 UTC 2020


* Luke Hutchison:

> This seems like an unnecessary restriction, based on the source of
> ByteBuffer. Can the ByteBuffer constructors please be made public?

Do you want to actually override any ByteBuffer methods?

It's currently part of the ByteBuffer interface contract that the
methods do not retain references to passed-in argument or, for the
reading operations, do not modify the objects that are passed in.
This avoids the need for defensive copies in callers.  With the
security manager fading away, it may eventually be possible to allow
subclasses without introducing vulnerabilities (because all code is
trusted), but I'm not sure if this is a good idea.

I expect that Panama will allow creation of direct byte buffers
directly from Java code (without a detour via JNI), but that may not
fit your use case because it still requires actual memory as a backing
storage.


More information about the nio-dev mailing list