From c28d5c8d0d83670a548671a1d0da55e3447ea0c7 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Tue, 25 Jun 2013 09:31:04 +0200
Subject: Refine commit 5e01e993aeba4e95fc8aa6e75b3e295011e27bbb, skip
 Buffers.sizeOfBufferElem(..) call.

---
 src/java/com/jogamp/common/nio/AbstractBuffer.java | 25 +++++++++++++---------
 src/java/com/jogamp/common/nio/PointerBuffer.java  | 15 ++++++-------
 2 files changed, 22 insertions(+), 18 deletions(-)

(limited to 'src/java/com/jogamp/common')

diff --git a/src/java/com/jogamp/common/nio/AbstractBuffer.java b/src/java/com/jogamp/common/nio/AbstractBuffer.java
index 2624254..4afff2a 100644
--- a/src/java/com/jogamp/common/nio/AbstractBuffer.java
+++ b/src/java/com/jogamp/common/nio/AbstractBuffer.java
@@ -42,9 +42,9 @@ import java.nio.Buffer;
 @SuppressWarnings("rawtypes")
 public abstract class AbstractBuffer<B extends AbstractBuffer> implements NativeBuffer<B> {
 
-    protected final int elementSize;
     protected final Buffer buffer;
-    protected int capacity;
+    protected final int elementSize;
+    protected final int capacity;
     protected int position;
 
     static {
@@ -52,15 +52,20 @@ public abstract class AbstractBuffer<B extends AbstractBuffer> implements Native
     }
 
     /** 
-     * @param buffer expected in target format
-     * @param elementSize the target element size in bytes
+     * capacity and elementSize should be match the equation w/ target buffer type
+     * <pre>
+     *    capacity = elementSizeInBytes(buffer) * buffer.capacity() ) / elementSize
+     * </pre>
+     * @param buffer shall be in target format.
+     * @param elementSize the target element size in bytes.
+     * @param capacity the target capacity in elements of size <code>elementSize</code>.
      */
-    protected AbstractBuffer(Buffer buffer, int elementSize) {
-        this.elementSize = elementSize;
+    protected AbstractBuffer(Buffer buffer, int elementSize, int capacity) {
         this.buffer = buffer;
-
-        capacity = ( Buffers.sizeOfBufferElem(buffer) * buffer.capacity() ) / elementSize ;
-        position = 0;
+        this.elementSize = elementSize;
+        this.capacity = capacity;
+        
+        this.position = 0;
     }
 
     public final int elementSize() {
@@ -114,7 +119,7 @@ public abstract class AbstractBuffer<B extends AbstractBuffer> implements Native
     }
 
     public final int arrayOffset() {
-        if(hasArray()) {
+        if( hasArray() ) {
             return buffer.arrayOffset();
         } else {
             return 0;
diff --git a/src/java/com/jogamp/common/nio/PointerBuffer.java b/src/java/com/jogamp/common/nio/PointerBuffer.java
index f05c6ad..df1c4b1 100644
--- a/src/java/com/jogamp/common/nio/PointerBuffer.java
+++ b/src/java/com/jogamp/common/nio/PointerBuffer.java
@@ -57,18 +57,18 @@ public class PointerBuffer extends AbstractBuffer<PointerBuffer> {
     }
 
     /** no backup array, use for direct usage only */
-    PointerBuffer(ByteBuffer bb) {
-        super(Platform.is32Bit() ? bb.asIntBuffer() : bb.asLongBuffer(), ELEMENT_SIZE);
+    static PointerBuffer create(ByteBuffer bb) {
+        return Platform.is32Bit() ? new PointerBuffer( bb.asIntBuffer() ) : new PointerBuffer( bb.asLongBuffer() );
     }
     
     /** supports backup array */
     PointerBuffer(IntBuffer b) {
-        super(b, ELEMENT_SIZE);
+        super(b, ELEMENT_SIZE, b.capacity());
     }
     
     /** supports backup array */
     PointerBuffer(LongBuffer b) {
-        super(b, ELEMENT_SIZE);
+        super(b, ELEMENT_SIZE, b.capacity());
     }
     
     private final void validateDataMap() {
@@ -89,16 +89,16 @@ public class PointerBuffer extends AbstractBuffer<PointerBuffer> {
 
     /** Returns a direct PointerBuffer in native order, w/o backup array */
     public static PointerBuffer allocateDirect(int size) {
-        return new PointerBuffer(Buffers.newDirectByteBuffer(ELEMENT_SIZE * size));
+        return create(Buffers.newDirectByteBuffer(ELEMENT_SIZE * size));
     }
 
     public static PointerBuffer wrap(ByteBuffer src) {
-        return new PointerBuffer(src);
+        return create(src);
     }
 
     /**
      * @return new PointerBuffer sharing the same buffer data of this instance (identity),
-     *         but having independent position, limit and capacity.
+     *         but having an independent position.
      */
     public final PointerBuffer duplicate() {
         PointerBuffer npb;
@@ -110,7 +110,6 @@ public class PointerBuffer extends AbstractBuffer<PointerBuffer> {
         if(null != dataMap) {
             npb.dataMap = (LongObjectHashMap) dataMap.clone();
         }
-        npb.capacity = capacity;
         npb.position = position;
         return npb;
     }
-- 
cgit v1.2.3