Class WWBufferUtil


  • public class WWBufferUtil
    extends java.lang.Object
    A collection of useful Buffer methods, all static.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int SIZEOF_CHAR
      The size of a char primitive type, in bytes.
      static int SIZEOF_DOUBLE
      The size of a double primitive type, in bytes.
      static int SIZEOF_FLOAT
      The size of a float primitive type, in bytes.
      static int SIZEOF_INT
      The size of a int primitive type, in bytes.
      static int SIZEOF_SHORT
      The size of a short primitive type, in bytes.
    • Constructor Summary

      Constructors 
      Constructor Description
      WWBufferUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double[] computeExtremeValues​(BufferWrapper buffer)
      Returns the minimum and maximum floating point values in the specified buffer.
      static double[] computeExtremeValues​(BufferWrapper buffer, double missingDataSignal)
      Returns the minimum and maximum floating point values in the specified buffer.
      static java.nio.FloatBuffer copyArrayToBuffer​(Vec4[] array, java.nio.FloatBuffer buffer)
      Copies a specified array of vertices to a specified vertex buffer.
      static java.nio.ByteBuffer copyOf​(java.nio.ByteBuffer buffer, int newSize)
      Returns a copy of the specified buffer, with the specified new size.
      static java.nio.CharBuffer copyOf​(java.nio.CharBuffer buffer, int newSize)
      Returns a copy of the specified buffer, with the specified new size.
      static java.nio.DoubleBuffer copyOf​(java.nio.DoubleBuffer buffer, int newSize)
      Returns a copy of the specified buffer, with the specified new size.
      static java.nio.FloatBuffer copyOf​(java.nio.FloatBuffer buffer, int newSize)
      Returns a copy of the specified buffer, with the specified new size.
      static java.nio.IntBuffer copyOf​(java.nio.IntBuffer buffer, int newSize)
      Returns a copy of the specified buffer, with the specified new size.
      static java.nio.ShortBuffer copyOf​(java.nio.ShortBuffer buffer, int newSize)
      Returns a copy of the specified buffer, with the specified new size.
      static java.nio.ByteBuffer newByteBuffer​(int size, boolean allocateDirect)
      Allocates a new direct ByteBuffer of the specified size, in chars.
      static BufferWrapper newByteBufferWrapper​(int size, boolean allocateDirect)
      Allocates a new BufferWrapper of the specified size, in bytes.
      static java.nio.CharBuffer newCharBuffer​(int size, boolean allocateDirect)
      Allocates a new direct CharBuffer of the specified size, in chars.
      protected static java.nio.ByteBuffer newDirectByteBuffer​(int size)  
      static java.nio.DoubleBuffer newDoubleBuffer​(int size, boolean allocateDirect)
      Allocates a new direct DoubleBuffer of the specified size, in chars.
      static BufferWrapper newDoubleBufferWrapper​(int size, boolean allocateDirect)
      Allocates a new BufferWrapper of the specified size, in doubles.
      static java.nio.FloatBuffer newFloatBuffer​(int size, boolean allocateDirect)
      Allocates a new direct FloatBuffer of the specified size, in chars.
      static BufferWrapper newFloatBufferWrapper​(int size, boolean allocateDirect)
      Allocates a new BufferWrapper of the specified size, in floats.
      static java.nio.IntBuffer newIntBuffer​(int size, boolean allocateDirect)
      Allocates a new direct IntBuffer of the specified size, in chars.
      static BufferWrapper newIntBufferWrapper​(int size, boolean allocateDirect)
      Allocates a new BufferWrapper of the specified size, in ints.
      static java.nio.ShortBuffer newShortBuffer​(int size, boolean allocateDirect)
      Allocates a new direct ShortBuffer of the specified size, in chars.
      static BufferWrapper newShortBufferWrapper​(int size, boolean allocateDirect)
      Allocates a new BufferWrapper of the specified size, in shorts.
      static int sizeOfPrimitiveType​(java.lang.Object dataType)
      Returns the size in bytes of the specified primitive data type, or -1 if the specified type is unrecognized.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SIZEOF_SHORT

        public static final int SIZEOF_SHORT
        The size of a short primitive type, in bytes.
        See Also:
        Constant Field Values
      • SIZEOF_INT

        public static final int SIZEOF_INT
        The size of a int primitive type, in bytes.
        See Also:
        Constant Field Values
      • SIZEOF_FLOAT

        public static final int SIZEOF_FLOAT
        The size of a float primitive type, in bytes.
        See Also:
        Constant Field Values
      • SIZEOF_DOUBLE

        public static final int SIZEOF_DOUBLE
        The size of a double primitive type, in bytes.
        See Also:
        Constant Field Values
      • SIZEOF_CHAR

        public static final int SIZEOF_CHAR
        The size of a char primitive type, in bytes.
        See Also:
        Constant Field Values
    • Constructor Detail

      • WWBufferUtil

        public WWBufferUtil()
    • Method Detail

      • newByteBuffer

        public static java.nio.ByteBuffer newByteBuffer​(int size,
                                                        boolean allocateDirect)
        Allocates a new direct ByteBuffer of the specified size, in chars.
        Parameters:
        size - the new ByteBuffer's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new buffer.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newShortBuffer

        public static java.nio.ShortBuffer newShortBuffer​(int size,
                                                          boolean allocateDirect)
        Allocates a new direct ShortBuffer of the specified size, in chars.
        Parameters:
        size - the new ShortBuffer's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new buffer.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newIntBuffer

        public static java.nio.IntBuffer newIntBuffer​(int size,
                                                      boolean allocateDirect)
        Allocates a new direct IntBuffer of the specified size, in chars.
        Parameters:
        size - the new IntBuffer's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new buffer.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newFloatBuffer

        public static java.nio.FloatBuffer newFloatBuffer​(int size,
                                                          boolean allocateDirect)
        Allocates a new direct FloatBuffer of the specified size, in chars.
        Parameters:
        size - the new FloatBuffer's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new buffer.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newDoubleBuffer

        public static java.nio.DoubleBuffer newDoubleBuffer​(int size,
                                                            boolean allocateDirect)
        Allocates a new direct DoubleBuffer of the specified size, in chars.
        Parameters:
        size - the new DoubleBuffer's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new buffer.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newCharBuffer

        public static java.nio.CharBuffer newCharBuffer​(int size,
                                                        boolean allocateDirect)
        Allocates a new direct CharBuffer of the specified size, in chars.
        Parameters:
        size - the new CharBuffer's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new buffer.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newByteBufferWrapper

        public static BufferWrapper newByteBufferWrapper​(int size,
                                                         boolean allocateDirect)
        Allocates a new BufferWrapper of the specified size, in bytes. The BufferWrapper is backed by a Buffer of bytes.
        Parameters:
        size - the new BufferWrapper's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new BufferWrapper.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newShortBufferWrapper

        public static BufferWrapper newShortBufferWrapper​(int size,
                                                          boolean allocateDirect)
        Allocates a new BufferWrapper of the specified size, in shorts. The BufferWrapper is backed by a Buffer of shorts.
        Parameters:
        size - the new BufferWrapper's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new BufferWrapper.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newIntBufferWrapper

        public static BufferWrapper newIntBufferWrapper​(int size,
                                                        boolean allocateDirect)
        Allocates a new BufferWrapper of the specified size, in ints. The BufferWrapper is backed by a Buffer of ints.
        Parameters:
        size - the new BufferWrapper's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new BufferWrapper.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newFloatBufferWrapper

        public static BufferWrapper newFloatBufferWrapper​(int size,
                                                          boolean allocateDirect)
        Allocates a new BufferWrapper of the specified size, in floats. The BufferWrapper is backed by a Buffer of floats.
        Parameters:
        size - the new BufferWrapper's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new BufferWrapper.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • newDoubleBufferWrapper

        public static BufferWrapper newDoubleBufferWrapper​(int size,
                                                           boolean allocateDirect)
        Allocates a new BufferWrapper of the specified size, in doubles. The BufferWrapper is backed by a Buffer of doubles.
        Parameters:
        size - the new BufferWrapper's size.
        allocateDirect - true to allocate and return a direct buffer, false to allocate and return a non-direct buffer.
        Returns:
        the new BufferWrapper.
        Throws:
        java.lang.IllegalArgumentException - if size is negative.
      • copyOf

        public static java.nio.ByteBuffer copyOf​(java.nio.ByteBuffer buffer,
                                                 int newSize)
        Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new buffer which is partially filled with the contents of the specified buffer.The returned buffer is a direct ByteBuffer if and only if the specified buffer is direct.
        Parameters:
        buffer - the buffer to copy.
        newSize - the new buffer's size, in bytes.
        Returns:
        the new buffer, with the specified size.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null, if the new size is negative, or if the new size is less than the buffer's remaing elements.
      • copyOf

        public static java.nio.CharBuffer copyOf​(java.nio.CharBuffer buffer,
                                                 int newSize)
        Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new buffer which is partially filled with the contents of the specified buffer. The returned buffer is a backed by a direct ByteBuffer if and only if the specified buffer is direct.
        Parameters:
        buffer - the buffer to copy.
        newSize - the new buffer's size, in chars.
        Returns:
        the new buffer, with the specified size.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null, if the new size is negative, or if the new size is less than the buffer's remaing elements.
      • copyOf

        public static java.nio.ShortBuffer copyOf​(java.nio.ShortBuffer buffer,
                                                  int newSize)
        Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new buffer which is partially filled with the contents of the specified buffer. The returned buffer is a backed by a direct ByteBuffer if and only if the specified buffer is direct.
        Parameters:
        buffer - the buffer to copy.
        newSize - the new buffer's size, in shorts.
        Returns:
        the new buffer, with the specified size.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null, if the new size is negative, or if the new size is less than the buffer's remaing elements.
      • copyOf

        public static java.nio.IntBuffer copyOf​(java.nio.IntBuffer buffer,
                                                int newSize)
        Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new buffer which is partially filled with the contents of the specified buffer.The returned buffer is a backed by a direct ByteBuffer if and only if the specified buffer is direct.
        Parameters:
        buffer - the buffer to copy.
        newSize - the new buffer's size, in ints.
        Returns:
        the new buffer, with the specified size.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null, if the new size is negative, or if the new size is less than the buffer's remaing elements.
      • copyOf

        public static java.nio.FloatBuffer copyOf​(java.nio.FloatBuffer buffer,
                                                  int newSize)
        Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new buffer which is partially filled with the contents of the specified buffer. The returned buffer is a backed by a direct ByteBuffer if and only if the specified buffer is direct.
        Parameters:
        buffer - the buffer to copy.
        newSize - the new buffer's size, in floats.
        Returns:
        the new buffer, with the specified size.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null, if the new size is negative, or if the new size is less than the buffer's remaing elements.
      • copyOf

        public static java.nio.DoubleBuffer copyOf​(java.nio.DoubleBuffer buffer,
                                                   int newSize)
        Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new buffer which is partially filled with the contents of the specified buffer. The returned buffer is a backed by a direct ByteBuffer if and only if the specified buffer is direct.
        Parameters:
        buffer - the buffer to copy.
        newSize - the new buffer's size, in doubles.
        Returns:
        the new buffer, with the specified size.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null, if the new size is negative, or if the new size is less than the buffer's remaing elements.
      • sizeOfPrimitiveType

        public static int sizeOfPrimitiveType​(java.lang.Object dataType)
        Returns the size in bytes of the specified primitive data type, or -1 if the specified type is unrecognized. Recognized primitive types are as follows:
        Parameters:
        dataType - the primitive data type.
        Returns:
        the size of the primitive data type, in bytes.
        Throws:
        java.lang.IllegalArgumentException - if the data type is null.
      • computeExtremeValues

        public static double[] computeExtremeValues​(BufferWrapper buffer,
                                                    double missingDataSignal)
        Returns the minimum and maximum floating point values in the specified buffer. Values equivalent to the specified missingDataSignal are ignored. This returns null if the buffer is empty or contains only missing values.
        Parameters:
        buffer - the buffer to search for the minimum and maximum values.
        missingDataSignal - the number indicating a specific floating point value to ignore.
        Returns:
        an array containing the minimum value in index 0 and the maximum value in index 1, or null if the buffer is empty or contains only missing values.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null.
      • computeExtremeValues

        public static double[] computeExtremeValues​(BufferWrapper buffer)
        Returns the minimum and maximum floating point values in the specified buffer. Values equivalent to Double.NaN are ignored. This returns null if the buffer is empty or contains only NaN values.
        Parameters:
        buffer - the buffer to search for the minimum and maximum values.
        Returns:
        an array containing the minimum value in index 0 and the maximum value in index 1, or null if the buffer is empty or contains only NaN values.
        Throws:
        java.lang.IllegalArgumentException - if the buffer is null.
      • newDirectByteBuffer

        protected static java.nio.ByteBuffer newDirectByteBuffer​(int size)
      • copyArrayToBuffer

        public static java.nio.FloatBuffer copyArrayToBuffer​(Vec4[] array,
                                                             java.nio.FloatBuffer buffer)
        Copies a specified array of vertices to a specified vertex buffer. This method calls FloatBuffer.flip() prior to returning.
        Parameters:
        array - the vertices to copy.
        buffer - the buffer to copy the vertices to. Must have enough remaining space to hold the vertices.
        Returns:
        the buffer specified as input, with its limit incremented by the number of vertices copied, and its position set to 0.