Class BufferWrapper

  • Direct Known Subclasses:
    BufferWrapper.AbstractBufferWrapper, BufferWrapper.EmptyBufferWrapper

    public abstract class BufferWrapper
    extends java.lang.Object
    BufferWrapper provides an interface for reading and writing primitive data to and from data buffers, without having to know the underlying data type. BufferWrapper may be backed by a primitive data buffer of any type.
    • Constructor Summary

      Constructors 
      Constructor Description
      BufferWrapper()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract BufferWrapper copyOf​(int newSize)
      Returns a copy of this buffer with the specified new size.
      static BufferWrapper emptyBufferWrapper()
      Returns the empty BufferWrapper.
      abstract java.nio.Buffer getBackingBuffer()
      Returns the buffer's backing data sture.
      abstract byte getByte​(int index)
      Returns the value at the specified index, cast to a byte.
      abstract void getByte​(int index, byte[] array, int offset, int length)
      Returns the sequence of values starting at the specified index and with the specified length, cast to bytes.
      abstract double getDouble​(int index)
      Returns the value at the specified index, cast to a double.
      abstract void getDouble​(int index, double[] array, int offset, int length)
      Returns the sequence of values starting at the specified index and with the specified length, cast to doubles.
      abstract float getFloat​(int index)
      Returns the value at the specified index, cast to a float.
      abstract void getFloat​(int index, float[] array, int offset, int length)
      Returns the sequence of values starting at the specified index and with the specified length, cast to floats.
      abstract int getGLDataType()
      Returns the OpenGL data type corresponding to the buffer's underlying data type (e.g.
      abstract int getInt​(int index)
      Returns the value at the specified index, cast to an int.
      abstract void getInt​(int index, int[] array, int offset, int length)
      Returns the sequence of values starting at the specified index and with the specified length, cast to ints.
      abstract short getShort​(int index)
      Returns the value at the specified index, cast to a short.
      abstract void getShort​(int index, short[] array, int offset, int length)
      Returns the sequence of values starting at the specified index and with the specified length, cast to shorts.
      abstract long getSizeInBytes()
      Returns the size of this buffer, in bytes.
      abstract BufferWrapper getSubBuffer​(int index, int length)
      Returns a new BufferWrapper which is a subsequence of this buffer.
      abstract int length()
      Returns the length of the buffer, in units of the underlying data type (e.g.
      abstract void putByte​(int index, byte value)
      Sets the value at the specified index as a byte.
      abstract void putByte​(int index, byte[] array, int offset, int length)
      Sets the sequence of values starting at the specified index and with the specified length, as bytes.
      abstract void putDouble​(int index, double value)
      Sets the value at the specified index as a double.
      abstract void putDouble​(int index, double[] array, int offset, int length)
      Sets the sequence of values starting at the specified index and with the specified length, as doubles.
      abstract void putFloat​(int index, float value)
      Sets the value at the specified index as a float.
      abstract void putFloat​(int index, float[] array, int offset, int length)
      Sets the sequence of values starting at the specified index and with the specified length, as floats.
      abstract void putInt​(int index, int value)
      Sets the value at the specified index as an int.
      abstract void putInt​(int index, int[] array, int offset, int length)
      Sets the sequence of values starting at the specified index and with the specified length, as ints.
      abstract void putShort​(int index, short value)
      Sets the value at the specified index as a short.
      abstract void putShort​(int index, short[] array, int offset, int length)
      Sets the sequence of values starting at the specified index and with the specified length, as ints.
      abstract void putSubBuffer​(int index, BufferWrapper buffer)
      Sets a subsequence of this buffer with the contents of the specified buffer.
      abstract void putSubBuffer​(int index, BufferWrapper buffer, int offset, int length)
      Sets a subsequence of this buffer with the contents of the specified buffer.
      static BufferWrapper wrap​(java.nio.ByteBuffer byteBuffer, AVList params)
      Wraps the specified ByteBuffer with a BufferWrapper according to the specified parameters.
      static BufferWrapper wrap​(java.nio.ByteBuffer byteBuffer, java.lang.Object dataType)
      Wraps the specified ByteBuffer with a BufferWrapper according to the specified primitive dataType.
      static BufferWrapper wrap​(java.nio.ByteBuffer byteBuffer, java.lang.Object dataType, java.lang.Object byteOrder)
      Wraps the specified ByteBuffer with a BufferWrapper according to the specified primitive dataType and byteOrder.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_BUFFER_WRAPPER

        protected static final BufferWrapper EMPTY_BUFFER_WRAPPER
    • Constructor Detail

      • BufferWrapper

        public BufferWrapper()
    • Method Detail

      • length

        public abstract int length()
        Returns the length of the buffer, in units of the underlying data type (e.g. bytes, shorts, ints, floats, doubles).
        Returns:
        the buffer's length.
      • getGLDataType

        public abstract int getGLDataType()
        Returns the OpenGL data type corresponding to the buffer's underlying data type (e.g. GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE).
        Returns:
        the buffer's OpenGL data type.
      • getSizeInBytes

        public abstract long getSizeInBytes()
        Returns the size of this buffer, in bytes.
        Returns:
        the buffer's size in bytes.
      • getByte

        public abstract byte getByte​(int index)
        Returns the value at the specified index, cast to a byte.
        Parameters:
        index - the index of the value to be returned.
        Returns:
        the byte at the specified index.
      • putByte

        public abstract void putByte​(int index,
                                     byte value)
        Sets the value at the specified index as a byte. The byte is cast to the underlying data type.
        Parameters:
        index - the index of the value to be returned.
        value - the byte value to be set.
      • getShort

        public abstract short getShort​(int index)
        Returns the value at the specified index, cast to a short.
        Parameters:
        index - the index of the value to be returned.
        Returns:
        the short at the specified index.
      • putShort

        public abstract void putShort​(int index,
                                      short value)
        Sets the value at the specified index as a short. The short is cast to the underlying data type.
        Parameters:
        index - the index of the value to be returned.
        value - the short value to be set.
      • getInt

        public abstract int getInt​(int index)
        Returns the value at the specified index, cast to an int.
        Parameters:
        index - the index of the value to be returned.
        Returns:
        the int at the specified index.
      • putInt

        public abstract void putInt​(int index,
                                    int value)
        Sets the value at the specified index as an int. The int is cast to the underlying data type.
        Parameters:
        index - the index of the value to be returned.
        value - the int value to be set.
      • getFloat

        public abstract float getFloat​(int index)
        Returns the value at the specified index, cast to a float.
        Parameters:
        index - the index of the value to be returned.
        Returns:
        the float at the specified index.
      • putFloat

        public abstract void putFloat​(int index,
                                      float value)
        Sets the value at the specified index as a float. The float is cast to the underlying data type.
        Parameters:
        index - the index of the value to be returned.
        value - the float value to be set.
      • getDouble

        public abstract double getDouble​(int index)
        Returns the value at the specified index, cast to a double.
        Parameters:
        index - the index of the value to be returned.
        Returns:
        the double at the specified index.
      • putDouble

        public abstract void putDouble​(int index,
                                       double value)
        Sets the value at the specified index as a double. The double is cast to the underlying data type.
        Parameters:
        index - the index of the value to be returned.
        value - the double value to be set.
      • getByte

        public abstract void getByte​(int index,
                                     byte[] array,
                                     int offset,
                                     int length)
        Returns the sequence of values starting at the specified index and with the specified length, cast to bytes.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to get.
      • putByte

        public abstract void putByte​(int index,
                                     byte[] array,
                                     int offset,
                                     int length)
        Sets the sequence of values starting at the specified index and with the specified length, as bytes. The bytes are cast to the underlying data type.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to put.
      • getShort

        public abstract void getShort​(int index,
                                      short[] array,
                                      int offset,
                                      int length)
        Returns the sequence of values starting at the specified index and with the specified length, cast to shorts.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to get.
      • putShort

        public abstract void putShort​(int index,
                                      short[] array,
                                      int offset,
                                      int length)
        Sets the sequence of values starting at the specified index and with the specified length, as ints. The ints are cast to the underlying data type.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to put.
      • getInt

        public abstract void getInt​(int index,
                                    int[] array,
                                    int offset,
                                    int length)
        Returns the sequence of values starting at the specified index and with the specified length, cast to ints.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to get.
      • putInt

        public abstract void putInt​(int index,
                                    int[] array,
                                    int offset,
                                    int length)
        Sets the sequence of values starting at the specified index and with the specified length, as ints. The ints are cast to the underlying data type.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to put.
      • getFloat

        public abstract void getFloat​(int index,
                                      float[] array,
                                      int offset,
                                      int length)
        Returns the sequence of values starting at the specified index and with the specified length, cast to floats.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to get.
      • putFloat

        public abstract void putFloat​(int index,
                                      float[] array,
                                      int offset,
                                      int length)
        Sets the sequence of values starting at the specified index and with the specified length, as floats. The floats are cast to the underlying data type.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to put.
      • getDouble

        public abstract void getDouble​(int index,
                                       double[] array,
                                       int offset,
                                       int length)
        Returns the sequence of values starting at the specified index and with the specified length, cast to doubles.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to get.
      • putDouble

        public abstract void putDouble​(int index,
                                       double[] array,
                                       int offset,
                                       int length)
        Sets the sequence of values starting at the specified index and with the specified length, as doubles. The doubles are cast to the underlying data type.
        Parameters:
        index - the buffer starting index.
        array - the array.
        offset - the array starting index.
        length - the number of values to put.
      • getSubBuffer

        public abstract BufferWrapper getSubBuffer​(int index,
                                                   int length)
        Returns a new BufferWrapper which is a subsequence of this buffer. The new buffer starts with the value at the specified index, and has the specified length. The two buffers share the same backing store, so changes to this buffer are reflected in the new buffer, and vice versa.
        Parameters:
        index - the new buffer's starting index.
        length - the new buffer's length.
        Returns:
        a subsequence of this buffer.
      • putSubBuffer

        public abstract void putSubBuffer​(int index,
                                          BufferWrapper buffer)
        Sets a subsequence of this buffer with the contents of the specified buffer. The subsequence to set starts with the value at the specified index, and has length equal to the specified buffer's length.
        Parameters:
        index - the starting index to set.
        buffer - the buffer.
      • putSubBuffer

        public abstract void putSubBuffer​(int index,
                                          BufferWrapper buffer,
                                          int offset,
                                          int length)
        Sets a subsequence of this buffer with the contents of the specified buffer. The subsequence to set starts with the value at the specified index, and has length equal to the specified length.
        Parameters:
        index - the starting index to set.
        buffer - the buffer.
        offset - the starting index to get from the buffer.
        length - the number of values to get from the buffer.
      • copyOf

        public abstract BufferWrapper copyOf​(int newSize)
        Returns a copy of this buffer with the specified new size. The new size must be greater than or equal to this buffer's size. If the new size is greater than this buffer's size, this returns a new buffer which is partially filled with the contents of this buffer. The returned buffer has the same backing buffer type, but its contents are independent from this VecBuffer.
        Parameters:
        newSize - the new buffer's size.
        Returns:
        the new buffer, with the specified size.
      • getBackingBuffer

        public abstract java.nio.Buffer getBackingBuffer()
        Returns the buffer's backing data sture. For the standard BufferWrapper types (ByteBufferWrapper, ShortBufferWrapper, IntBufferWrapper, FloatBufferWrapper, and DoubleBufferWrapper), this returns the backing Buffer.
        Returns:
        the backing data store.
      • emptyBufferWrapper

        public static BufferWrapper emptyBufferWrapper()
        Returns the empty BufferWrapper. The returned BufferWrapper is immutable and has no backing Buffer.
        Returns:
        the empty BufferWrapper.
      • wrap

        public static BufferWrapper wrap​(java.nio.ByteBuffer byteBuffer,
                                         java.lang.Object dataType,
                                         java.lang.Object byteOrder)
        Wraps the specified ByteBuffer with a BufferWrapper according to the specified primitive dataType and byteOrder. The dataType describes the primitive data type stored in the ByteBuffer: shorts, ints, floats, or doubles. The byteOrder describes the ByteBuffer's byte ordering. A null byteOrder indicates that the ByteBuffer's current byte ordering should be used.
        Parameters:
        byteBuffer - the buffer to wrap.
        dataType - the primitive data type stored in the ByteBuffer.
        byteOrder - the primitive byte ordering of the ByteBuffer, or null to use the ByteBuffer's current ordering.
        Returns:
        a new BufferWrapper backed by the specified byteBuffer.
        Throws:
        java.lang.IllegalArgumentException - if either the byteBuffer or the data type are null.
      • wrap

        public static BufferWrapper wrap​(java.nio.ByteBuffer byteBuffer,
                                         java.lang.Object dataType)
        Wraps the specified ByteBuffer with a BufferWrapper according to the specified primitive dataType. The dataType describes the primitive data type stored in the ByteBuffer: shorts, ints, floats, or doubles. This assumes the ByteBuffer's current byte ordering.
        Parameters:
        byteBuffer - the buffer to wrap.
        dataType - the primitive data type stored in the ByteBuffer.
        Returns:
        a new BufferWrapper backed by the specified byteBuffer.
        Throws:
        java.lang.IllegalArgumentException - if either the byteBuffer or the data type are null.
      • wrap

        public static BufferWrapper wrap​(java.nio.ByteBuffer byteBuffer,
                                         AVList params)
        Wraps the specified ByteBuffer with a BufferWrapper according to the specified parameters. The AVKey.DATA_TYPE parameter is required, and describes the primitive data type stored in the ByteBuffer: shorts, ints, floats, or doubles. The AVKey.BYTE_ORDER parameter is optional, and describes the ByteBuffer's byte ordering.
        Parameters:
        byteBuffer - the buffer to wrap.
        params - the parameters which describe how to interpret the buffer.
        Returns:
        a new BufferWrapper backed by the specified byteBuffer.
        Throws:
        java.lang.IllegalArgumentException - if either the byteBuffer or the parameters are null, or if AVKey.DATA_TYPE parameter is missing.