From fecd018fae78dede2e6744c1ebf4eb96d1a61f18 Mon Sep 17 00:00:00 2001 From: Stephen Gold Date: Mon, 24 Feb 2020 08:40:02 -0800 Subject: [PATCH] BufferUtils: resolve issue #1288 (rewrite isDirect()) (#1299) --- .../main/java/com/jme3/util/BufferUtils.java | 132 ++++++++---------- 1 file changed, 55 insertions(+), 77 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/util/BufferUtils.java b/jme3-core/src/main/java/com/jme3/util/BufferUtils.java index d4546b7e3..b5fdfa5d8 100644 --- a/jme3-core/src/main/java/com/jme3/util/BufferUtils.java +++ b/jme3-core/src/main/java/com/jme3/util/BufferUtils.java @@ -54,7 +54,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * BufferUtils is a helper class for generating nio buffers from * jME data classes such as Vectors and ColorRGBA. - * + * * @author Joshua Slack * @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 * purpose. Default is false. To print direct memory usage use * BufferUtils.printCurrentDirectMemory(StringBuilder store); - * + * * @param 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 * given buffer's limit. - * + * * @param buf * The buffer to clone * @return The cloned buffer @@ -141,7 +141,7 @@ public final class BufferUtils { * 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 * data[0].x, data[0].y, data[0].z, data[1].x... etc. - * + * * @param data * 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. * The FloatBuffer will be 4 * data.length long and contain the vector data. - * + * * @param data * 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. - * + * * @param data * 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 * number of Vector3f object data. - * + * * @param vertices * number of vertices that need to be held by the newly created * buffer @@ -265,7 +265,7 @@ public final class BufferUtils { * 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 * the right size. - * + * * @param buf * the buffer to first check and rewind * @param vertices @@ -285,7 +285,7 @@ public final class BufferUtils { /** * Sets the data contained in the given color into the FloatBuffer at the * specified index. - * + * * @param color * the data to insert * @param buf @@ -304,7 +304,7 @@ public final class BufferUtils { /** * Sets the data contained in the given quaternion into the FloatBuffer at * the specified index. - * + * * @param quat * the {@link Quaternion} to insert * @param buf @@ -343,7 +343,7 @@ public final class BufferUtils { /** * Sets the data contained in the given Vector3F into the FloatBuffer at the * specified index. - * + * * @param vector * the data to insert * @param buf @@ -369,7 +369,7 @@ public final class BufferUtils { /** * Updates the values of the given vector from the specified buffer at the * index provided. - * + * * @param vector * the vector to set data on * @param buf @@ -387,7 +387,7 @@ public final class BufferUtils { /** * Updates the values of the given vector from the specified buffer at the * index provided. - * + * * @param vector * the vector to set data on * @param buf @@ -405,7 +405,7 @@ public final class BufferUtils { /** * Generates a Vector3f array from the given FloatBuffer. - * + * * @param buff * the FloatBuffer to read from * @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 * values are in terms of vector number (eg, vector number 0 is positions * 0-2 in the FloatBuffer.) - * + * * @param buf * the buffer to copy from/to * @param fromPos @@ -438,7 +438,7 @@ public final class BufferUtils { /** * Normalize a Vector3f in-buffer. - * + * * @param buf * the buffer to find the Vector3f within * @param index @@ -456,7 +456,7 @@ public final class BufferUtils { /** * Add to a Vector3f in-buffer. - * + * * @param toAdd * the vector to add from * @param buf @@ -476,7 +476,7 @@ public final class BufferUtils { /** * Multiply and store a Vector3f in-buffer. - * + * * @param toMult * the vector to multiply against * @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 * buffer at the given data index. - * + * * @param check * the vector to check against - null will return false. * @param buf @@ -521,7 +521,7 @@ public final class BufferUtils { * 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 * data[0].x, data[0].y, data[1].x... etc. - * + * * @param data * 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 * number of Vector2f object data. - * + * * @param vertices * number of vertices that need to be held by the newly created * buffer @@ -559,7 +559,7 @@ public final class BufferUtils { * 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 * the right size. - * + * * @param buf * the buffer to first check and rewind * @param vertices @@ -579,7 +579,7 @@ public final class BufferUtils { /** * Sets the data contained in the given Vector2F into the FloatBuffer at the * specified index. - * + * * @param vector * the data to insert * @param buf @@ -595,7 +595,7 @@ public final class BufferUtils { /** * Updates the values of the given vector from the specified buffer at the * index provided. - * + * * @param vector * the vector to set data on * @param buf @@ -611,7 +611,7 @@ public final class BufferUtils { /** * Generates a Vector2f array from the given FloatBuffer. - * + * * @param buff * the FloatBuffer to read from * @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 * values are in terms of vector number (eg, vector number 0 is positions * 0-1 in the FloatBuffer.) - * + * * @param buf * the buffer to copy from/to * @param fromPos @@ -644,7 +644,7 @@ public final class BufferUtils { /** * Normalize a Vector2f in-buffer. - * + * * @param buf * the buffer to find the Vector2f within * @param index @@ -662,7 +662,7 @@ public final class BufferUtils { /** * Add to a Vector2f in-buffer. - * + * * @param toAdd * the vector to add from * @param buf @@ -682,7 +682,7 @@ public final class BufferUtils { /** * Multiply and store a Vector2f in-buffer. - * + * * @param toMult * the vector to multiply against * @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 * buffer at the given data index. - * + * * @param check * the vector to check against - null will return false. * @param buf @@ -727,7 +727,7 @@ public final class BufferUtils { * 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]... * etc. - * + * * @param data * 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 * contents. - * + * * @param buff * the IntBuffer to read from * @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 * contents. - * + * * @param buff * the FloatBuffer to read from * @return a new float array populated from the FloatBuffer @@ -785,7 +785,7 @@ public final class BufferUtils { //// -- GENERAL DOUBLE ROUTINES -- //// /** * Create a new DoubleBuffer of the specified size. - * + * * @param size * required number of double to store. * @return the new DoubleBuffer @@ -800,7 +800,7 @@ public final class BufferUtils { /** * 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. - * + * * @param buf * the buffer to first check and rewind * @param size @@ -823,7 +823,7 @@ public final class BufferUtils { * DoubleBuffer. The new DoubleBuffer is separate from the old one and * changes are not reflected across. If you want to reflect changes, * consider using Buffer.duplicate(). - * + * * @param buf * the DoubleBuffer to copy * @return the copy @@ -848,7 +848,7 @@ public final class BufferUtils { //// -- GENERAL FLOAT ROUTINES -- //// /** * Create a new FloatBuffer of the specified size. - * + * * @param size * required number of floats to store. * @return the new FloatBuffer @@ -862,7 +862,7 @@ public final class BufferUtils { /** * Copies floats from one position in the buffer to another. - * + * * @param buf * the buffer to copy from/to * @param fromPos @@ -885,7 +885,7 @@ public final class BufferUtils { * FloatBuffer. The new FloatBuffer is separate from the old one and changes * are not reflected across. If you want to reflect changes, consider using * Buffer.duplicate(). - * + * * @param buf * the FloatBuffer to copy * @return the copy @@ -910,7 +910,7 @@ public final class BufferUtils { //// -- GENERAL INT ROUTINES -- //// /** * Create a new IntBuffer of the specified size. - * + * * @param size * required number of ints to store. * @return the new IntBuffer @@ -925,7 +925,7 @@ public final class BufferUtils { /** * 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. - * + * * @param buf * the buffer to first check and rewind * @param size @@ -948,7 +948,7 @@ public final class BufferUtils { * The new IntBuffer is separate from the old one and changes are not * reflected across. If you want to reflect changes, consider using * Buffer.duplicate(). - * + * * @param buf * the IntBuffer to copy * @return the copy @@ -973,7 +973,7 @@ public final class BufferUtils { //// -- GENERAL BYTE ROUTINES -- //// /** * Create a new ByteBuffer of the specified size. - * + * * @param size * required number of ints to store. * @return the new IntBuffer @@ -988,7 +988,7 @@ public final class BufferUtils { /** * 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. - * + * * @param buf * the buffer to first check and rewind * @param size @@ -1030,7 +1030,7 @@ public final class BufferUtils { * The new ByteBuffer is separate from the old one and changes are not * reflected across. If you want to reflect changes, consider using * Buffer.duplicate(). - * + * * @param buf * the ByteBuffer to copy * @return the copy @@ -1055,7 +1055,7 @@ public final class BufferUtils { //// -- GENERAL SHORT ROUTINES -- //// /** * Create a new ShortBuffer of the specified size. - * + * * @param size * required number of shorts to store. * @return the new ShortBuffer @@ -1070,7 +1070,7 @@ public final class BufferUtils { /** * 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. - * + * * @param buf * the buffer to first check and rewind * @param size @@ -1104,7 +1104,7 @@ public final class BufferUtils { * ShortBuffer. The new ShortBuffer is separate from the old one and changes * are not reflected across. If you want to reflect changes, consider using * Buffer.duplicate(). - * + * * @param buf * the ShortBuffer to copy * @return the copy @@ -1130,7 +1130,7 @@ public final class BufferUtils { * Ensures there is at least the required number of entries * 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. - * + * * @param buffer * buffer that should be checked/copied (may be null) * @param required @@ -1277,36 +1277,14 @@ public final class BufferUtils { allocator.destroyDirectBuffer(toBeDestroyed); } - /* - * FIXME when java 1.5 supprt is dropped - replace calls to this method with - * Buffer.isDirect - * - * Buffer.isDirect() is only java 6. Java 5 only have this method on Buffer - * subclasses : FloatBuffer, IntBuffer, ShortBuffer, - * ByteBuffer,DoubleBuffer, LongBuffer. CharBuffer has been excluded as we - * don't use it. - * + /** + * Test whether the specified buffer is direct. + * + * @param buf the buffer to test (not null, unaffected) + * @return true if direct, otherwise false */ private static boolean isDirect(Buffer buf) { - if (buf instanceof FloatBuffer) { - 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()); + return buf.isDirect(); } private static class BufferInfo extends PhantomReference {