0

Why do we have isDirect() method in CharBuffer?

How can we allocate a direct CharBuffer if we don't have any respective (allocateDirect) methods in CharBuffer or Buffer ?

I may guess it is:

ByteBuffer.alocateDirect(100).asCharBuffer()

Is this so and is it the only (and recommended) way to do it?

user10777718
  • 723
  • 4
  • 16

1 Answers1

1

CharBuffer inherits that method from the Buffer interface, with many others.
CharBuffer has multiple sub-classes

enter image description here

For example the DirectCharBuffer(s) return true.
This

ByteBuffer.allocateDirect(20).asCharBuffer();

will return a DirectCharBuffer, which is a CharBuffer.

*U suffix for big-endian systems.
*S suffix for other kinds (little-endian).
*R suffix for read-only buffers.


final CharBuffer cb = ByteBuffer.allocateDirect(20).asCharBuffer();
final boolean direct = cb.isDirect();  // true

Is this so and is it the only (and recommended) way to do it?

Yes.


For reference, what is a direct buffer? See JavaDoc, section Direct vs. non-direct buffers.

LppEdd
  • 20,274
  • 11
  • 84
  • 139