Class BufferUtils


  • public final class BufferUtils
    extends java.lang.Object
    Class with static helper methods to increase the speed of array/direct buffer and direct buffer/direct buffer transfers
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clear​(java.nio.ByteBuffer buffer, int numBytes)
      Writes the specified number of zeros to the buffer.
      static void copy​(byte[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(char[] src, int srcOffset, int numElements, java.nio.Buffer dst)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(char[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(double[] src, int srcOffset, int numElements, java.nio.Buffer dst)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(double[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(float[] src, int srcOffset, int numElements, java.nio.Buffer dst)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(float[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(float[] src, java.nio.Buffer dst, int numFloats, int offset)
      Copies numFloats floats from src starting at offset to dst.
      static void copy​(int[] src, int srcOffset, int numElements, java.nio.Buffer dst)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(int[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(long[] src, int srcOffset, int numElements, java.nio.Buffer dst)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(long[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(short[] src, int srcOffset, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements.
      static void copy​(java.nio.Buffer src, java.nio.Buffer dst, int numElements)
      Copies the contents of src to dst, starting from the current position of src, copying numElements elements (using the data type of src, no matter the datatype of dst).
      static void disposeUnsafeByteBuffer​(java.nio.ByteBuffer buffer)  
      static long findFloats​(float[] vertex, int strideInBytes, float[] vertices, int numVertices)  
      static long findFloats​(float[] vertex, int strideInBytes, float[] vertices, int numVertices, float epsilon)  
      static long findFloats​(float[] vertex, int strideInBytes, java.nio.Buffer vertices, int numVertices)  
      static long findFloats​(float[] vertex, int strideInBytes, java.nio.Buffer vertices, int numVertices, float epsilon)  
      static long findFloats​(java.nio.Buffer vertex, int strideInBytes, float[] vertices, int numVertices)  
      static long findFloats​(java.nio.Buffer vertex, int strideInBytes, float[] vertices, int numVertices, float epsilon)  
      static long findFloats​(java.nio.Buffer vertex, int strideInBytes, java.nio.Buffer vertices, int numVertices)  
      static long findFloats​(java.nio.Buffer vertex, int strideInBytes, java.nio.Buffer vertices, int numVertices, float epsilon)  
      static int getAllocatedBytesUnsafe()  
      static long getUnsafeBufferAddress​(java.nio.Buffer buffer)
      Returns the address of the Buffer, it assumes it is an unsafe buffer.
      static boolean isUnsafeByteBuffer​(java.nio.ByteBuffer buffer)  
      static java.nio.ByteBuffer newByteBuffer​(int numBytes)  
      static java.nio.CharBuffer newCharBuffer​(int numChars)  
      static java.nio.DoubleBuffer newDoubleBuffer​(int numDoubles)  
      static java.nio.FloatBuffer newFloatBuffer​(int numFloats)  
      static java.nio.IntBuffer newIntBuffer​(int numInts)  
      static java.nio.LongBuffer newLongBuffer​(int numLongs)  
      static java.nio.ShortBuffer newShortBuffer​(int numShorts)  
      static java.nio.ByteBuffer newUnsafeByteBuffer​(int numBytes)
      Allocates a new direct ByteBuffer from native heap memory using the native byte order.
      static java.nio.ByteBuffer newUnsafeByteBuffer​(java.nio.ByteBuffer buffer)
      Registers the given ByteBuffer as an unsafe ByteBuffer.
      static void transform​(float[] data, int dimensions, int strideInBytes, int count, Matrix3 matrix)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(float[] data, int dimensions, int strideInBytes, int count, Matrix3 matrix, int offset)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(float[] data, int dimensions, int strideInBytes, int count, Matrix4 matrix)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(float[] data, int dimensions, int strideInBytes, int count, Matrix4 matrix, int offset)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(java.nio.Buffer data, int dimensions, int strideInBytes, int count, Matrix3 matrix)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(java.nio.Buffer data, int dimensions, int strideInBytes, int count, Matrix3 matrix, int offset)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(java.nio.Buffer data, int dimensions, int strideInBytes, int count, Matrix4 matrix)
      Multiply float vector components within the buffer with the specified matrix.
      static void transform​(java.nio.Buffer data, int dimensions, int strideInBytes, int count, Matrix4 matrix, int offset)
      Multiply float vector components within the buffer with the specified matrix.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • copy

        public static void copy​(float[] src,
                                java.nio.Buffer dst,
                                int numFloats,
                                int offset)
        Copies numFloats floats from src starting at offset to dst. Dst is assumed to be a direct Buffer. The method will crash if that is not the case. The position and limit of the buffer are ignored, the copy is placed at position 0 in the buffer. After the copying process the position of the buffer is set to 0 and its limit is set to numFloats * 4 if it is a ByteBuffer and numFloats if it is a FloatBuffer. In case the Buffer is neither a ByteBuffer nor a FloatBuffer the limit is not set. This is an expert method, use at your own risk.
        Parameters:
        src - the source array
        dst - the destination buffer, has to be a direct Buffer
        numFloats - the number of floats to copy
        offset - the offset in src to start copying from
      • copy

        public static void copy​(byte[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(short[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(char[] src,
                                int srcOffset,
                                int numElements,
                                java.nio.Buffer dst)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position and limit will stay the same. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        numElements - the number of elements to copy.
        dst - the destination Buffer, its position is used as an offset.
      • copy

        public static void copy​(int[] src,
                                int srcOffset,
                                int numElements,
                                java.nio.Buffer dst)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position and limit will stay the same. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        numElements - the number of elements to copy.
        dst - the destination Buffer, its position is used as an offset.
      • copy

        public static void copy​(long[] src,
                                int srcOffset,
                                int numElements,
                                java.nio.Buffer dst)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position and limit will stay the same. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        numElements - the number of elements to copy.
        dst - the destination Buffer, its position is used as an offset.
      • copy

        public static void copy​(float[] src,
                                int srcOffset,
                                int numElements,
                                java.nio.Buffer dst)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position and limit will stay the same. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        numElements - the number of elements to copy.
        dst - the destination Buffer, its position is used as an offset.
      • copy

        public static void copy​(double[] src,
                                int srcOffset,
                                int numElements,
                                java.nio.Buffer dst)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position and limit will stay the same. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        numElements - the number of elements to copy.
        dst - the destination Buffer, its position is used as an offset.
      • copy

        public static void copy​(char[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(int[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(long[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(float[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(double[] src,
                                int srcOffset,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from src[srcOffset], copying numElements elements. The Buffer instance's Buffer.position() is used to define the offset into the Buffer itself. The position will stay the same, the limit will be set to position + numElements. The Buffer must be a direct Buffer with native byte order. No error checking is performed.
        Parameters:
        src - the source array.
        srcOffset - the offset into the source array.
        dst - the destination Buffer, its position is used as an offset.
        numElements - the number of elements to copy.
      • copy

        public static void copy​(java.nio.Buffer src,
                                java.nio.Buffer dst,
                                int numElements)
        Copies the contents of src to dst, starting from the current position of src, copying numElements elements (using the data type of src, no matter the datatype of dst). The dst Buffer.position() is used as the writing offset. The position of both Buffers will stay the same. The limit of the src Buffer will stay the same. The limit of the dst Buffer will be set to dst.position() + numElements, where numElements are translated to the number of elements appropriate for the dst Buffer data type. The Buffers must be direct Buffers with native byte order. No error checking is performed.
        Parameters:
        src - the source Buffer.
        dst - the destination Buffer.
        numElements - the number of elements to copy.
      • transform

        public static void transform​(java.nio.Buffer data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix4 matrix)
        Multiply float vector components within the buffer with the specified matrix. The Buffer.position() is used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components of the vector (2 for xy, 3 for xyz or 4 for xyzw)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with
      • transform

        public static void transform​(float[] data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix4 matrix)
        Multiply float vector components within the buffer with the specified matrix. The Buffer.position() is used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components of the vector (2 for xy, 3 for xyz or 4 for xyzw)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with
      • transform

        public static void transform​(java.nio.Buffer data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix4 matrix,
                                     int offset)
        Multiply float vector components within the buffer with the specified matrix. The specified offset value is added to the Buffer.position() and used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components of the vector (2 for xy, 3 for xyz or 4 for xyzw)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with
        offset - The offset within the buffer (in bytes relative to the current position) to the vector
      • transform

        public static void transform​(float[] data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix4 matrix,
                                     int offset)
        Multiply float vector components within the buffer with the specified matrix. The specified offset value is added to the Buffer.position() and used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components of the vector (2 for xy, 3 for xyz or 4 for xyzw)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with
        offset - The offset within the buffer (in bytes relative to the current position) to the vector
      • transform

        public static void transform​(java.nio.Buffer data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix3 matrix)
        Multiply float vector components within the buffer with the specified matrix. The Buffer.position() is used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components (x, y, z) of the vector (2 for xy or 3 for xyz)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with
      • transform

        public static void transform​(float[] data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix3 matrix)
        Multiply float vector components within the buffer with the specified matrix. The Buffer.position() is used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components (x, y, z) of the vector (2 for xy or 3 for xyz)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with
      • transform

        public static void transform​(java.nio.Buffer data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix3 matrix,
                                     int offset)
        Multiply float vector components within the buffer with the specified matrix. The specified offset value is added to the Buffer.position() and used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components (x, y, z) of the vector (2 for xy or 3 for xyz)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with,
        offset - The offset within the buffer (in bytes relative to the current position) to the vector
      • transform

        public static void transform​(float[] data,
                                     int dimensions,
                                     int strideInBytes,
                                     int count,
                                     Matrix3 matrix,
                                     int offset)
        Multiply float vector components within the buffer with the specified matrix. The specified offset value is added to the Buffer.position() and used as the offset.
        Parameters:
        data - The buffer to transform.
        dimensions - The number of components (x, y, z) of the vector (2 for xy or 3 for xyz)
        strideInBytes - The offset between the first and the second vector to transform
        count - The number of vectors to transform
        matrix - The matrix to multiply the vector with,
        offset - The offset within the buffer (in bytes relative to the current position) to the vector
      • findFloats

        public static long findFloats​(java.nio.Buffer vertex,
                                      int strideInBytes,
                                      java.nio.Buffer vertices,
                                      int numVertices)
      • findFloats

        public static long findFloats​(float[] vertex,
                                      int strideInBytes,
                                      java.nio.Buffer vertices,
                                      int numVertices)
      • findFloats

        public static long findFloats​(java.nio.Buffer vertex,
                                      int strideInBytes,
                                      float[] vertices,
                                      int numVertices)
      • findFloats

        public static long findFloats​(float[] vertex,
                                      int strideInBytes,
                                      float[] vertices,
                                      int numVertices)
      • findFloats

        public static long findFloats​(java.nio.Buffer vertex,
                                      int strideInBytes,
                                      java.nio.Buffer vertices,
                                      int numVertices,
                                      float epsilon)
      • findFloats

        public static long findFloats​(float[] vertex,
                                      int strideInBytes,
                                      java.nio.Buffer vertices,
                                      int numVertices,
                                      float epsilon)
      • findFloats

        public static long findFloats​(java.nio.Buffer vertex,
                                      int strideInBytes,
                                      float[] vertices,
                                      int numVertices,
                                      float epsilon)
      • findFloats

        public static long findFloats​(float[] vertex,
                                      int strideInBytes,
                                      float[] vertices,
                                      int numVertices,
                                      float epsilon)
      • newFloatBuffer

        public static java.nio.FloatBuffer newFloatBuffer​(int numFloats)
      • newDoubleBuffer

        public static java.nio.DoubleBuffer newDoubleBuffer​(int numDoubles)
      • newByteBuffer

        public static java.nio.ByteBuffer newByteBuffer​(int numBytes)
      • newShortBuffer

        public static java.nio.ShortBuffer newShortBuffer​(int numShorts)
      • newCharBuffer

        public static java.nio.CharBuffer newCharBuffer​(int numChars)
      • newIntBuffer

        public static java.nio.IntBuffer newIntBuffer​(int numInts)
      • newLongBuffer

        public static java.nio.LongBuffer newLongBuffer​(int numLongs)
      • disposeUnsafeByteBuffer

        public static void disposeUnsafeByteBuffer​(java.nio.ByteBuffer buffer)
      • isUnsafeByteBuffer

        public static boolean isUnsafeByteBuffer​(java.nio.ByteBuffer buffer)
      • newUnsafeByteBuffer

        public static java.nio.ByteBuffer newUnsafeByteBuffer​(int numBytes)
        Allocates a new direct ByteBuffer from native heap memory using the native byte order. Needs to be disposed with disposeUnsafeByteBuffer(ByteBuffer).
      • getUnsafeBufferAddress

        public static long getUnsafeBufferAddress​(java.nio.Buffer buffer)
        Returns the address of the Buffer, it assumes it is an unsafe buffer.
        Parameters:
        buffer - The Buffer to ask the address for.
        Returns:
        the address of the Buffer.
      • newUnsafeByteBuffer

        public static java.nio.ByteBuffer newUnsafeByteBuffer​(java.nio.ByteBuffer buffer)
        Registers the given ByteBuffer as an unsafe ByteBuffer. The ByteBuffer must have been allocated in native code, pointing to a memory region allocated via malloc. Needs to be disposed with disposeUnsafeByteBuffer(ByteBuffer).
        Parameters:
        buffer - the ByteBuffer to register
        Returns:
        the ByteBuffer passed to the method
      • getAllocatedBytesUnsafe

        public static int getAllocatedBytesUnsafe()
        Returns:
        the number of bytes allocated with newUnsafeByteBuffer(int)
      • clear

        public static void clear​(java.nio.ByteBuffer buffer,
                                 int numBytes)
        Writes the specified number of zeros to the buffer. This is generally faster than reallocating a new buffer.