From 3f50232fae03c65d7d84a6ca1e2a7b83cefde6ae Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 23 Jun 2023 06:17:31 +0200 Subject: NIO NativeBuffer, {Element,Pointer}Buffer: Add limit, clear and flip; Arrange wrap/deref arguments equal; Add equal set of absolute get/set methods Completing API to simplify usage by generated code. All absolute get/set method check arguments itself and against limit(), allow to drop checks in generated code (size). --- src/java/com/jogamp/common/nio/AbstractBuffer.java | 47 +++++++++++++++++----- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'src/java/com/jogamp/common/nio/AbstractBuffer.java') diff --git a/src/java/com/jogamp/common/nio/AbstractBuffer.java b/src/java/com/jogamp/common/nio/AbstractBuffer.java index b31929b..a09a93e 100644 --- a/src/java/com/jogamp/common/nio/AbstractBuffer.java +++ b/src/java/com/jogamp/common/nio/AbstractBuffer.java @@ -31,12 +31,10 @@ */ package com.jogamp.common.nio; -import com.jogamp.common.os.*; - import java.nio.Buffer; import java.nio.ByteBuffer; -import java.nio.IntBuffer; -import java.nio.LongBuffer; + +import com.jogamp.common.os.Platform; /** * @author Sven Gothel @@ -50,6 +48,7 @@ public abstract class AbstractBuffer implements Native protected final Buffer buffer; protected final int elementSize; protected final int capacity; + protected int limit; protected int position; static { @@ -70,6 +69,7 @@ public abstract class AbstractBuffer implements Native this.buffer = buffer; this.elementSize = elementSize; this.capacity = capacity; + this.limit = capacity; this.position = 0; } @@ -81,7 +81,17 @@ public abstract class AbstractBuffer implements Native @Override public final int limit() { - return capacity; + return limit; + } + + @SuppressWarnings("unchecked") + @Override + public final B limit(final int newLim) { + if (0 > newLim || newLim >= capacity) { + throw new IllegalArgumentException("New limit "+newLim+" out of bounds [0 .. capacity " +capacity()+"]."); + } + limit = newLim; + return (B)this; } @Override @@ -94,11 +104,11 @@ public abstract class AbstractBuffer implements Native return position; } + @SuppressWarnings("unchecked") @Override public final B position(final int newPos) { - if (0 > newPos || newPos >= capacity) { - throw new IndexOutOfBoundsException("Sorry to interrupt, but the position "+newPos+" was out of bounds. " + - "My capacity is "+capacity()+"."); + if (0 > newPos || newPos > limit) { + throw new IllegalArgumentException("New position "+newPos+" out of bounds [0 .. limit " +limit()+"]."); } position = newPos; return (B)this; @@ -106,14 +116,31 @@ public abstract class AbstractBuffer implements Native @Override public final int remaining() { - return capacity - position; + return limit - position; } @Override public final boolean hasRemaining() { - return position < capacity; + return limit > position; + } + + @SuppressWarnings("unchecked") + @Override + public final B clear() { + limit = capacity; + position = 0; + return (B) this; + } + + @SuppressWarnings("unchecked") + @Override + public final B flip() { + limit = position; + position = 0; + return (B) this; } + @SuppressWarnings("unchecked") @Override public final B rewind() { position = 0; -- cgit v1.2.3