BufferUtils: resolve issue #1288 (rewrite isDirect()) (#1299)

v3.3
Stephen Gold 5 years ago committed by Paul Speed
parent b102eabb56
commit fecd018fae
  1. 132
      jme3-core/src/main/java/com/jme3/util/BufferUtils.java

@ -54,7 +54,7 @@ import java.util.concurrent.ConcurrentHashMap;
/** /**
* <code>BufferUtils</code> is a helper class for generating nio buffers from * <code>BufferUtils</code> is a helper class for generating nio buffers from
* jME data classes such as Vectors and ColorRGBA. * jME data classes such as Vectors and ColorRGBA.
* *
* @author Joshua Slack * @author Joshua Slack
* @version $Id: BufferUtils.java,v 1.16 2007/10/29 16:56:18 nca Exp $ * @version $Id: BufferUtils.java,v 1.16 2007/10/29 16:56:18 nca Exp $
*/ */
@ -74,7 +74,7 @@ public final class BufferUtils {
* Set it to true if you want to enable direct memory tracking for debugging * Set it to true if you want to enable direct memory tracking for debugging
* purpose. Default is false. To print direct memory usage use * purpose. Default is false. To print direct memory usage use
* BufferUtils.printCurrentDirectMemory(StringBuilder store); * BufferUtils.printCurrentDirectMemory(StringBuilder store);
* *
* @param enabled * @param enabled
*/ */
public static void setTrackDirectMemoryEnabled(boolean enabled) { public static void setTrackDirectMemoryEnabled(boolean enabled) {
@ -84,7 +84,7 @@ public final class BufferUtils {
/** /**
* Creates a clone of the given buffer. The clone's capacity is equal to the * Creates a clone of the given buffer. The clone's capacity is equal to the
* given buffer's limit. * given buffer's limit.
* *
* @param buf * @param buf
* The buffer to clone * The buffer to clone
* @return The cloned buffer * @return The cloned buffer
@ -141,7 +141,7 @@ public final class BufferUtils {
* Generate a new FloatBuffer using the given array of Vector3f objects. The * Generate a new FloatBuffer using the given array of Vector3f objects. The
* FloatBuffer will be 3 * data.length long and contain the vector data as * FloatBuffer will be 3 * data.length long and contain the vector data as
* data[0].x, data[0].y, data[0].z, data[1].x... etc. * data[0].x, data[0].y, data[0].z, data[1].x... etc.
* *
* @param data * @param data
* array of Vector3f objects to place into a new FloatBuffer * array of Vector3f objects to place into a new FloatBuffer
*/ */
@ -164,7 +164,7 @@ public final class BufferUtils {
/** /**
* Generate a new FloatBuffer using the given array of Quaternion objects. * Generate a new FloatBuffer using the given array of Quaternion objects.
* The FloatBuffer will be 4 * data.length long and contain the vector data. * The FloatBuffer will be 4 * data.length long and contain the vector data.
* *
* @param data * @param data
* array of Quaternion objects to place into a new FloatBuffer * array of Quaternion objects to place into a new FloatBuffer
*/ */
@ -232,7 +232,7 @@ public final class BufferUtils {
/** /**
* Generate a new FloatBuffer using the given array of float primitives. * Generate a new FloatBuffer using the given array of float primitives.
* *
* @param data * @param data
* array of float primitives to place into a new FloatBuffer * array of float primitives to place into a new FloatBuffer
*/ */
@ -250,7 +250,7 @@ public final class BufferUtils {
/** /**
* Create a new FloatBuffer of an appropriate size to hold the specified * Create a new FloatBuffer of an appropriate size to hold the specified
* number of Vector3f object data. * number of Vector3f object data.
* *
* @param vertices * @param vertices
* number of vertices that need to be held by the newly created * number of vertices that need to be held by the newly created
* buffer * buffer
@ -265,7 +265,7 @@ public final class BufferUtils {
* Create a new FloatBuffer of an appropriate size to hold the specified * Create a new FloatBuffer of an appropriate size to hold the specified
* number of Vector3f object data only if the given buffer if not already * number of Vector3f object data only if the given buffer if not already
* the right size. * the right size.
* *
* @param buf * @param buf
* the buffer to first check and rewind * the buffer to first check and rewind
* @param vertices * @param vertices
@ -285,7 +285,7 @@ public final class BufferUtils {
/** /**
* Sets the data contained in the given color into the FloatBuffer at the * Sets the data contained in the given color into the FloatBuffer at the
* specified index. * specified index.
* *
* @param color * @param color
* the data to insert * the data to insert
* @param buf * @param buf
@ -304,7 +304,7 @@ public final class BufferUtils {
/** /**
* Sets the data contained in the given quaternion into the FloatBuffer at * Sets the data contained in the given quaternion into the FloatBuffer at
* the specified index. * the specified index.
* *
* @param quat * @param quat
* the {@link Quaternion} to insert * the {@link Quaternion} to insert
* @param buf * @param buf
@ -343,7 +343,7 @@ public final class BufferUtils {
/** /**
* Sets the data contained in the given Vector3F into the FloatBuffer at the * Sets the data contained in the given Vector3F into the FloatBuffer at the
* specified index. * specified index.
* *
* @param vector * @param vector
* the data to insert * the data to insert
* @param buf * @param buf
@ -369,7 +369,7 @@ public final class BufferUtils {
/** /**
* Updates the values of the given vector from the specified buffer at the * Updates the values of the given vector from the specified buffer at the
* index provided. * index provided.
* *
* @param vector * @param vector
* the vector to set data on * the vector to set data on
* @param buf * @param buf
@ -387,7 +387,7 @@ public final class BufferUtils {
/** /**
* Updates the values of the given vector from the specified buffer at the * Updates the values of the given vector from the specified buffer at the
* index provided. * index provided.
* *
* @param vector * @param vector
* the vector to set data on * the vector to set data on
* @param buf * @param buf
@ -405,7 +405,7 @@ public final class BufferUtils {
/** /**
* Generates a Vector3f array from the given FloatBuffer. * Generates a Vector3f array from the given FloatBuffer.
* *
* @param buff * @param buff
* the FloatBuffer to read from * the FloatBuffer to read from
* @return a newly generated array of Vector3f objects * @return a newly generated array of Vector3f objects
@ -424,7 +424,7 @@ public final class BufferUtils {
* Copies a Vector3f from one position in the buffer to another. The index * Copies a Vector3f from one position in the buffer to another. The index
* values are in terms of vector number (eg, vector number 0 is positions * values are in terms of vector number (eg, vector number 0 is positions
* 0-2 in the FloatBuffer.) * 0-2 in the FloatBuffer.)
* *
* @param buf * @param buf
* the buffer to copy from/to * the buffer to copy from/to
* @param fromPos * @param fromPos
@ -438,7 +438,7 @@ public final class BufferUtils {
/** /**
* Normalize a Vector3f in-buffer. * Normalize a Vector3f in-buffer.
* *
* @param buf * @param buf
* the buffer to find the Vector3f within * the buffer to find the Vector3f within
* @param index * @param index
@ -456,7 +456,7 @@ public final class BufferUtils {
/** /**
* Add to a Vector3f in-buffer. * Add to a Vector3f in-buffer.
* *
* @param toAdd * @param toAdd
* the vector to add from * the vector to add from
* @param buf * @param buf
@ -476,7 +476,7 @@ public final class BufferUtils {
/** /**
* Multiply and store a Vector3f in-buffer. * Multiply and store a Vector3f in-buffer.
* *
* @param toMult * @param toMult
* the vector to multiply against * the vector to multiply against
* @param buf * @param buf
@ -497,7 +497,7 @@ public final class BufferUtils {
/** /**
* Checks to see if the given Vector3f is equals to the data stored in the * Checks to see if the given Vector3f is equals to the data stored in the
* buffer at the given data index. * buffer at the given data index.
* *
* @param check * @param check
* the vector to check against - null will return false. * the vector to check against - null will return false.
* @param buf * @param buf
@ -521,7 +521,7 @@ public final class BufferUtils {
* Generate a new FloatBuffer using the given array of Vector2f objects. The * Generate a new FloatBuffer using the given array of Vector2f objects. The
* FloatBuffer will be 2 * data.length long and contain the vector data as * FloatBuffer will be 2 * data.length long and contain the vector data as
* data[0].x, data[0].y, data[1].x... etc. * data[0].x, data[0].y, data[1].x... etc.
* *
* @param data * @param data
* array of Vector2f objects to place into a new FloatBuffer * array of Vector2f objects to place into a new FloatBuffer
*/ */
@ -544,7 +544,7 @@ public final class BufferUtils {
/** /**
* Create a new FloatBuffer of an appropriate size to hold the specified * Create a new FloatBuffer of an appropriate size to hold the specified
* number of Vector2f object data. * number of Vector2f object data.
* *
* @param vertices * @param vertices
* number of vertices that need to be held by the newly created * number of vertices that need to be held by the newly created
* buffer * buffer
@ -559,7 +559,7 @@ public final class BufferUtils {
* Create a new FloatBuffer of an appropriate size to hold the specified * Create a new FloatBuffer of an appropriate size to hold the specified
* number of Vector2f object data only if the given buffer if not already * number of Vector2f object data only if the given buffer if not already
* the right size. * the right size.
* *
* @param buf * @param buf
* the buffer to first check and rewind * the buffer to first check and rewind
* @param vertices * @param vertices
@ -579,7 +579,7 @@ public final class BufferUtils {
/** /**
* Sets the data contained in the given Vector2F into the FloatBuffer at the * Sets the data contained in the given Vector2F into the FloatBuffer at the
* specified index. * specified index.
* *
* @param vector * @param vector
* the data to insert * the data to insert
* @param buf * @param buf
@ -595,7 +595,7 @@ public final class BufferUtils {
/** /**
* Updates the values of the given vector from the specified buffer at the * Updates the values of the given vector from the specified buffer at the
* index provided. * index provided.
* *
* @param vector * @param vector
* the vector to set data on * the vector to set data on
* @param buf * @param buf
@ -611,7 +611,7 @@ public final class BufferUtils {
/** /**
* Generates a Vector2f array from the given FloatBuffer. * Generates a Vector2f array from the given FloatBuffer.
* *
* @param buff * @param buff
* the FloatBuffer to read from * the FloatBuffer to read from
* @return a newly generated array of Vector2f objects * @return a newly generated array of Vector2f objects
@ -630,7 +630,7 @@ public final class BufferUtils {
* Copies a Vector2f from one position in the buffer to another. The index * Copies a Vector2f from one position in the buffer to another. The index
* values are in terms of vector number (eg, vector number 0 is positions * values are in terms of vector number (eg, vector number 0 is positions
* 0-1 in the FloatBuffer.) * 0-1 in the FloatBuffer.)
* *
* @param buf * @param buf
* the buffer to copy from/to * the buffer to copy from/to
* @param fromPos * @param fromPos
@ -644,7 +644,7 @@ public final class BufferUtils {
/** /**
* Normalize a Vector2f in-buffer. * Normalize a Vector2f in-buffer.
* *
* @param buf * @param buf
* the buffer to find the Vector2f within * the buffer to find the Vector2f within
* @param index * @param index
@ -662,7 +662,7 @@ public final class BufferUtils {
/** /**
* Add to a Vector2f in-buffer. * Add to a Vector2f in-buffer.
* *
* @param toAdd * @param toAdd
* the vector to add from * the vector to add from
* @param buf * @param buf
@ -682,7 +682,7 @@ public final class BufferUtils {
/** /**
* Multiply and store a Vector2f in-buffer. * Multiply and store a Vector2f in-buffer.
* *
* @param toMult * @param toMult
* the vector to multiply against * the vector to multiply against
* @param buf * @param buf
@ -703,7 +703,7 @@ public final class BufferUtils {
/** /**
* Checks to see if the given Vector2f is equals to the data stored in the * Checks to see if the given Vector2f is equals to the data stored in the
* buffer at the given data index. * buffer at the given data index.
* *
* @param check * @param check
* the vector to check against - null will return false. * the vector to check against - null will return false.
* @param buf * @param buf
@ -727,7 +727,7 @@ public final class BufferUtils {
* Generate a new IntBuffer using the given array of ints. The IntBuffer * Generate a new IntBuffer using the given array of ints. The IntBuffer
* will be data.length long and contain the int data as data[0], data[1]... * will be data.length long and contain the int data as data[0], data[1]...
* etc. * etc.
* *
* @param data * @param data
* array of ints to place into a new IntBuffer * array of ints to place into a new IntBuffer
*/ */
@ -745,7 +745,7 @@ public final class BufferUtils {
/** /**
* Create a new int[] array and populate it with the given IntBuffer's * Create a new int[] array and populate it with the given IntBuffer's
* contents. * contents.
* *
* @param buff * @param buff
* the IntBuffer to read from * the IntBuffer to read from
* @return a new int array populated from the IntBuffer * @return a new int array populated from the IntBuffer
@ -765,7 +765,7 @@ public final class BufferUtils {
/** /**
* Create a new float[] array and populate it with the given FloatBuffer's * Create a new float[] array and populate it with the given FloatBuffer's
* contents. * contents.
* *
* @param buff * @param buff
* the FloatBuffer to read from * the FloatBuffer to read from
* @return a new float array populated from the FloatBuffer * @return a new float array populated from the FloatBuffer
@ -785,7 +785,7 @@ public final class BufferUtils {
//// -- GENERAL DOUBLE ROUTINES -- //// //// -- GENERAL DOUBLE ROUTINES -- ////
/** /**
* Create a new DoubleBuffer of the specified size. * Create a new DoubleBuffer of the specified size.
* *
* @param size * @param size
* required number of double to store. * required number of double to store.
* @return the new DoubleBuffer * @return the new DoubleBuffer
@ -800,7 +800,7 @@ public final class BufferUtils {
/** /**
* Create a new DoubleBuffer of an appropriate size to hold the specified * Create a new DoubleBuffer of an appropriate size to hold the specified
* number of doubles only if the given buffer if not already the right size. * number of doubles only if the given buffer if not already the right size.
* *
* @param buf * @param buf
* the buffer to first check and rewind * the buffer to first check and rewind
* @param size * @param size
@ -823,7 +823,7 @@ public final class BufferUtils {
* DoubleBuffer. The new DoubleBuffer is separate from the old one and * DoubleBuffer. The new DoubleBuffer is separate from the old one and
* changes are not reflected across. If you want to reflect changes, * changes are not reflected across. If you want to reflect changes,
* consider using Buffer.duplicate(). * consider using Buffer.duplicate().
* *
* @param buf * @param buf
* the DoubleBuffer to copy * the DoubleBuffer to copy
* @return the copy * @return the copy
@ -848,7 +848,7 @@ public final class BufferUtils {
//// -- GENERAL FLOAT ROUTINES -- //// //// -- GENERAL FLOAT ROUTINES -- ////
/** /**
* Create a new FloatBuffer of the specified size. * Create a new FloatBuffer of the specified size.
* *
* @param size * @param size
* required number of floats to store. * required number of floats to store.
* @return the new FloatBuffer * @return the new FloatBuffer
@ -862,7 +862,7 @@ public final class BufferUtils {
/** /**
* Copies floats from one position in the buffer to another. * Copies floats from one position in the buffer to another.
* *
* @param buf * @param buf
* the buffer to copy from/to * the buffer to copy from/to
* @param fromPos * @param fromPos
@ -885,7 +885,7 @@ public final class BufferUtils {
* FloatBuffer. The new FloatBuffer is separate from the old one and changes * FloatBuffer. The new FloatBuffer is separate from the old one and changes
* are not reflected across. If you want to reflect changes, consider using * are not reflected across. If you want to reflect changes, consider using
* Buffer.duplicate(). * Buffer.duplicate().
* *
* @param buf * @param buf
* the FloatBuffer to copy * the FloatBuffer to copy
* @return the copy * @return the copy
@ -910,7 +910,7 @@ public final class BufferUtils {
//// -- GENERAL INT ROUTINES -- //// //// -- GENERAL INT ROUTINES -- ////
/** /**
* Create a new IntBuffer of the specified size. * Create a new IntBuffer of the specified size.
* *
* @param size * @param size
* required number of ints to store. * required number of ints to store.
* @return the new IntBuffer * @return the new IntBuffer
@ -925,7 +925,7 @@ public final class BufferUtils {
/** /**
* Create a new IntBuffer of an appropriate size to hold the specified * Create a new IntBuffer of an appropriate size to hold the specified
* number of ints only if the given buffer if not already the right size. * number of ints only if the given buffer if not already the right size.
* *
* @param buf * @param buf
* the buffer to first check and rewind * the buffer to first check and rewind
* @param size * @param size
@ -948,7 +948,7 @@ public final class BufferUtils {
* The new IntBuffer is separate from the old one and changes are not * The new IntBuffer is separate from the old one and changes are not
* reflected across. If you want to reflect changes, consider using * reflected across. If you want to reflect changes, consider using
* Buffer.duplicate(). * Buffer.duplicate().
* *
* @param buf * @param buf
* the IntBuffer to copy * the IntBuffer to copy
* @return the copy * @return the copy
@ -973,7 +973,7 @@ public final class BufferUtils {
//// -- GENERAL BYTE ROUTINES -- //// //// -- GENERAL BYTE ROUTINES -- ////
/** /**
* Create a new ByteBuffer of the specified size. * Create a new ByteBuffer of the specified size.
* *
* @param size * @param size
* required number of ints to store. * required number of ints to store.
* @return the new IntBuffer * @return the new IntBuffer
@ -988,7 +988,7 @@ public final class BufferUtils {
/** /**
* Create a new ByteBuffer of an appropriate size to hold the specified * Create a new ByteBuffer of an appropriate size to hold the specified
* number of ints only if the given buffer if not already the right size. * number of ints only if the given buffer if not already the right size.
* *
* @param buf * @param buf
* the buffer to first check and rewind * the buffer to first check and rewind
* @param size * @param size
@ -1030,7 +1030,7 @@ public final class BufferUtils {
* The new ByteBuffer is separate from the old one and changes are not * The new ByteBuffer is separate from the old one and changes are not
* reflected across. If you want to reflect changes, consider using * reflected across. If you want to reflect changes, consider using
* Buffer.duplicate(). * Buffer.duplicate().
* *
* @param buf * @param buf
* the ByteBuffer to copy * the ByteBuffer to copy
* @return the copy * @return the copy
@ -1055,7 +1055,7 @@ public final class BufferUtils {
//// -- GENERAL SHORT ROUTINES -- //// //// -- GENERAL SHORT ROUTINES -- ////
/** /**
* Create a new ShortBuffer of the specified size. * Create a new ShortBuffer of the specified size.
* *
* @param size * @param size
* required number of shorts to store. * required number of shorts to store.
* @return the new ShortBuffer * @return the new ShortBuffer
@ -1070,7 +1070,7 @@ public final class BufferUtils {
/** /**
* Create a new ShortBuffer of an appropriate size to hold the specified * Create a new ShortBuffer of an appropriate size to hold the specified
* number of shorts only if the given buffer if not already the right size. * number of shorts only if the given buffer if not already the right size.
* *
* @param buf * @param buf
* the buffer to first check and rewind * the buffer to first check and rewind
* @param size * @param size
@ -1104,7 +1104,7 @@ public final class BufferUtils {
* ShortBuffer. The new ShortBuffer is separate from the old one and changes * ShortBuffer. The new ShortBuffer is separate from the old one and changes
* are not reflected across. If you want to reflect changes, consider using * are not reflected across. If you want to reflect changes, consider using
* Buffer.duplicate(). * Buffer.duplicate().
* *
* @param buf * @param buf
* the ShortBuffer to copy * the ShortBuffer to copy
* @return the copy * @return the copy
@ -1130,7 +1130,7 @@ public final class BufferUtils {
* Ensures there is at least the <code>required</code> number of entries * Ensures there is at least the <code>required</code> number of entries
* left after the current position of the buffer. If the buffer is too small * left after the current position of the buffer. If the buffer is too small
* a larger one is created and the old one copied to the new buffer. * a larger one is created and the old one copied to the new buffer.
* *
* @param buffer * @param buffer
* buffer that should be checked/copied (may be null) * buffer that should be checked/copied (may be null)
* @param required * @param required
@ -1277,36 +1277,14 @@ public final class BufferUtils {
allocator.destroyDirectBuffer(toBeDestroyed); allocator.destroyDirectBuffer(toBeDestroyed);
} }
/* /**
* FIXME when java 1.5 supprt is dropped - replace calls to this method with * Test whether the specified buffer is direct.
* Buffer.isDirect *
* * @param buf the buffer to test (not null, unaffected)
* Buffer.isDirect() is only java 6. Java 5 only have this method on Buffer * @return true if direct, otherwise false
* subclasses : FloatBuffer, IntBuffer, ShortBuffer,
* ByteBuffer,DoubleBuffer, LongBuffer. CharBuffer has been excluded as we
* don't use it.
*
*/ */
private static boolean isDirect(Buffer buf) { private static boolean isDirect(Buffer buf) {
if (buf instanceof FloatBuffer) { return buf.isDirect();
return ((FloatBuffer) buf).isDirect();
}
if (buf instanceof IntBuffer) {
return ((IntBuffer) buf).isDirect();
}
if (buf instanceof ShortBuffer) {
return ((ShortBuffer) buf).isDirect();
}
if (buf instanceof ByteBuffer) {
return ((ByteBuffer) buf).isDirect();
}
if (buf instanceof DoubleBuffer) {
return ((DoubleBuffer) buf).isDirect();
}
if (buf instanceof LongBuffer) {
return ((LongBuffer) buf).isDirect();
}
throw new UnsupportedOperationException(" BufferUtils.isDirect was called on " + buf.getClass().getName());
} }
private static class BufferInfo extends PhantomReference<Buffer> { private static class BufferInfo extends PhantomReference<Buffer> {

Loading…
Cancel
Save