Top Description Constructors Methods
org.lwjgl

public final Class BufferUtils

extends Object
Class Inheritance
Imports
org.lwjgl.system.*, java.nio.*

This class makes it easy and safe to work with direct buffers. It is the recommended way to allocate memory to use with LWJGL.

Direct buffers

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.

Usage

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.

Arrays of pointers

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.

Memory management

Using NIO buffers for off-heap memory has some drawbacks:

An alternative API for allocating off-heap memory can be found in the org.lwjgl.system.MemoryUtil class. This has none of the above drawbacks, but requires allocated memory to be explictly freed when not used anymore.

Memory alignment

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.

Structs and arrays of structs

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.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ByteBuffer

Returns:

a ByteBuffer
createByteBuffer
(int
the capacity, in bytes
capacity
)

Allocates a direct native-ordered ByteBuffer with the specified capacity.

public static CharBuffer

Returns:

a CharBuffer
createCharBuffer
(int
the capacity, in chars
capacity
)

Allocates a direct native-order CharBuffer with the specified number of elements.

public static CLongBuffer

Returns:

a CLongBuffer
createCLongBuffer
(int
the capacity, in memory addresses
capacity
)

Allocates a CLongBuffer with the specified number of elements.

public static DoubleBuffer

Returns:

a DoubleBuffer
createDoubleBuffer
(int
the capacity, in doubles
capacity
)

Allocates a direct native-order DoubleBuffer with the specified number of elements.

public static FloatBuffer

Returns:

a FloatBuffer
createFloatBuffer
(int
the capacity, in floats
capacity
)

Allocates a direct native-order FloatBuffer with the specified number of elements.

public static IntBuffer

Returns:

an IntBuffer
createIntBuffer
(int
the capacity, in ints
capacity
)

Allocates a direct native-order IntBuffer with the specified number of elements.

public static LongBuffer

Returns:

a LongBuffer
createLongBuffer
(int
the capacity, in longs
capacity
)

Allocates a direct native-order LongBuffer with the specified number of elements.

public static PointerBuffer

Returns:

a PointerBuffer
createPointerBuffer
(int
the capacity, in memory addresses
capacity
)

Allocates a PointerBuffer with the specified number of elements.

public static ShortBuffer

Returns:

a ShortBuffer
createShortBuffer
(int
the capacity, in shorts
capacity
)

Allocates a direct native-order ShortBuffer with the specified number of elements.

pack-priv static int
getAllocationSize(int elements, int elementShift)

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<T>> void
zeroBuffer(T
the buffer to fill with zeros
buffer
)

Fills the specified buffer with zeros from the current position to the current limit.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

BufferUtilsback to summary
private BufferUtils()

Method Detail

createByteBufferback to summary
public static ByteBuffer createByteBuffer(int capacity)

Allocates a direct native-ordered ByteBuffer with the specified capacity.

Parameters
capacity:int

the capacity, in bytes

Returns:ByteBuffer

a ByteBuffer

createCharBufferback to summary
public static CharBuffer createCharBuffer(int capacity)

Allocates a direct native-order CharBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in chars

Returns:CharBuffer

a CharBuffer

createCLongBufferback to summary
public static CLongBuffer createCLongBuffer(int capacity)

Allocates a CLongBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in memory addresses

Returns:CLongBuffer

a CLongBuffer

createDoubleBufferback to summary
public static DoubleBuffer createDoubleBuffer(int capacity)

Allocates a direct native-order DoubleBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in doubles

Returns:DoubleBuffer

a DoubleBuffer

createFloatBufferback to summary
public static FloatBuffer createFloatBuffer(int capacity)

Allocates a direct native-order FloatBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in floats

Returns:FloatBuffer

a FloatBuffer

createIntBufferback to summary
public static IntBuffer createIntBuffer(int capacity)

Allocates a direct native-order IntBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in ints

Returns:IntBuffer

an IntBuffer

createLongBufferback to summary
public static LongBuffer createLongBuffer(int capacity)

Allocates a direct native-order LongBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in longs

Returns:LongBuffer

a LongBuffer

createPointerBufferback to summary
public static PointerBuffer createPointerBuffer(int capacity)

Allocates a PointerBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in memory addresses

Returns:PointerBuffer

a PointerBuffer

createShortBufferback to summary
public static ShortBuffer createShortBuffer(int capacity)

Allocates a direct native-order ShortBuffer with the specified number of elements.

Parameters
capacity:int

the capacity, in shorts

Returns:ShortBuffer

a ShortBuffer

getAllocationSizeback to summary
pack-priv static int getAllocationSize(int elements, int elementShift)
zeroBufferback to summary
public static void zeroBuffer(ByteBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:ByteBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static void zeroBuffer(ShortBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:ShortBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static void zeroBuffer(CharBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:CharBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static void zeroBuffer(IntBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:IntBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static void zeroBuffer(FloatBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:FloatBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static void zeroBuffer(LongBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:LongBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static void zeroBuffer(DoubleBuffer buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:DoubleBuffer

the buffer to fill with zeros

zeroBufferback to summary
public static <T extends CustomBuffer<T>> void zeroBuffer(T buffer)

Fills the specified buffer with zeros from the current position to the current limit.

Parameters
buffer:T

the buffer to fill with zeros