This class makes it easy and safe to work with direct buffers. It is the recommended way to allocate memory to use with LWJGL.
LWJGL requires that all NIO buffers passed to it are direct buffers. Direct buffers essentially wrap an address that points to off-heap memory, i.e. a native pointer. This is the only way LWJGL can safely pass data from Java code to native code, and vice-versa, without a performance penalty. It does not support on-heap Java arrays (or plain NIO buffers, which wrap them) because arrays may be moved around in memory by the JVM's garbage collector while native code is accessing them. In addition, Java arrays have an unspecified layout, i.e. they are not necessarily contiguous in memory.
When a direct buffer is passed as an argument to an LWJGL method, no data is copied. Instead, the current buffer position is added to the buffer's memory
address and the resulting value is passed to native code. The native code interprets that value as a pointer and reads or copies from it as necessary. LWJGL
will often also use the current buffer limit (via Buffer#remaining()) to automatically pass length/maxlength arguments. This means that, just like
other APIs that use NIO buffers, the current Buffer#position() and Buffer#limit() at the time of the call is very important. Contrary to
other APIs, LWJGL never modifies the current position, it will be the same value before and after the call.
In addition to the standard NIO buffer classes, LWJGL provides a PointerBuffer class for storing pointer data in an architecture independent way.
It is used in bindings for pointer-to-pointers arguments, usually to provide arrays of data (input parameter) or to store returned pointer values (output
parameter). Also, there's the CLongBuffer class which is similar to PointerBuffer, but for C long data.
Using NIO buffers for off-heap memory has some drawbacks:
Integer#MAX_VALUE bytes cannot be allocated.An alternative API for allocating off-heap memory can be found in the org. class. This has none of the above drawbacks,
but requires allocated memory to be explictly freed when not used anymore.
Allocations done via this class have a guaranteed alignment of 8 bytes. If higher alignment values are required, use the explicit memory management API or pad the requested memory with extra bytes and align manually.
Java does not support struct value types, so LWJGL requires struct values that are backed by off-heap memory. Each struct type defined in a binding
has a corresponding class in LWJGL that can be used to access its members. Each struct class also has a Buffer inner class that can be used to
access (packed) arrays of struct values. Both struct and struct buffer classes may be backed by direct ByteBuffers allocated from this class, but it
is highly recommended to use explicit memory management for performance.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static ByteBuffer | Returns: aByteBufferthe capacity, in bytes capacity)Allocates a direct native-ordered |
| public static CharBuffer | Returns: aCharBufferthe capacity, in chars capacity)Allocates a direct native-order |
| public static CLongBuffer | Returns: aCLongBufferthe capacity, in memory addresses capacity)Allocates a |
| public static DoubleBuffer | Returns: aDoubleBufferthe capacity, in doubles capacity)Allocates a direct native-order |
| public static FloatBuffer | Returns: a FloatBufferthe capacity, in floats capacity)Allocates a direct native-order |
| public static IntBuffer | Returns: anIntBufferthe capacity, in ints capacity)Allocates a direct native-order |
| public static LongBuffer | Returns: aLongBufferthe capacity, in longs capacity)Allocates a direct native-order |
| public static PointerBuffer | Returns: aPointerBufferthe capacity, in memory addresses capacity)Allocates a |
| public static ShortBuffer | Returns: aShortBufferthe capacity, in shorts capacity)Allocates a direct native-order |
| pack-priv static int | |
| public static void | zeroBuffer(ByteBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static void | zeroBuffer(ShortBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static void | zeroBuffer(CharBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static void | zeroBuffer(IntBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static void | zeroBuffer(FloatBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static void | zeroBuffer(LongBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static void | zeroBuffer(DoubleBuffer
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| public static <T extends CustomBuffer | zeroBuffer(T
the buffer to fill with zeros buffer)Fills the specified buffer with zeros from the current position to the current limit. |
| BufferUtils | back to summary |
|---|---|
| private BufferUtils() | |
| createByteBuffer | back to summary |
|---|---|
| public static ByteBuffer createByteBuffer(int capacity) Allocates a direct native-ordered
| |
| createCharBuffer | back to summary |
|---|---|
| public static CharBuffer createCharBuffer(int capacity) Allocates a direct native-order
| |
| createCLongBuffer | back to summary |
|---|---|
| public static CLongBuffer createCLongBuffer(int capacity) Allocates a
| |
| createDoubleBuffer | back to summary |
|---|---|
| public static DoubleBuffer createDoubleBuffer(int capacity) Allocates a direct native-order
| |
| createFloatBuffer | back to summary |
|---|---|
| public static FloatBuffer createFloatBuffer(int capacity) Allocates a direct native-order
| |
| createIntBuffer | back to summary |
|---|---|
| public static IntBuffer createIntBuffer(int capacity) Allocates a direct native-order
| |
| createLongBuffer | back to summary |
|---|---|
| public static LongBuffer createLongBuffer(int capacity) Allocates a direct native-order
| |
| createPointerBuffer | back to summary |
|---|---|
| public static PointerBuffer createPointerBuffer(int capacity) Allocates a
| |
| createShortBuffer | back to summary |
|---|---|
| public static ShortBuffer createShortBuffer(int capacity) Allocates a direct native-order
| |
| getAllocationSize | back to summary |
|---|---|
| pack-priv static int getAllocationSize(int elements, int elementShift) | |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(ByteBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(ShortBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(CharBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(IntBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(FloatBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(LongBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static void zeroBuffer(DoubleBuffer buffer) Fills the specified buffer with zeros from the current position to the current limit.
| |
| zeroBuffer | back to summary |
|---|---|
| public static <T extends CustomBuffer Fills the specified buffer with zeros from the current position to the current limit.
| |