GlueGen: src/java/com/jogamp/common/nio/AbstractBuffer.java Source File

GlueGen v2.6.0-rc-20250722 GlueGen, Native Binding Generator for Java™ (public API).

AbstractBuffer.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29/*
30 * Created on Saturday, March 27 2010 11:55
31 */
32 package com.jogamp.common.nio;
33
34import java.nio.Buffer;
35import java.nio.ByteBuffer;
36
37import com.jogamp.common.os.Platform;
38
39/**
40 * @author Sven Gothel
41 * @author Michael Bien
42 */
43@SuppressWarnings("rawtypes")
44public abstract class AbstractBuffer<B extends AbstractBuffer> implements NativeBuffer<B> {
45 /** Platform dependent pointer size in bytes, i.e. 32bit or 64bit wide, depending of the CPU pointer width. */
46 public static final int POINTER_SIZE;
47
48 protected final Buffer buffer;
49 protected final int elementSize;
50 protected final int capacity;
51 protected int limit;
52 protected int position;
53
54 static {
55 Platform.initSingleton(); // loads native gluegen_rt library
56 POINTER_SIZE = Platform.is32Bit() ? Buffers.SIZEOF_INT : Buffers.SIZEOF_LONG ;
57 }
58
59 /**
60 * capacity and elementSize should be match the equation w/ target buffer type
61 * <pre>
62 * capacity = elementSizeInBytes(buffer) * buffer.capacity() ) / elementSize
63 * </pre>
64 * @param buffer shall be in target format.
65 * @param elementSize the target element size in bytes.
66 * @param capacity the target capacity in elements of size <code>elementSize</code>.
67 */
68 protected AbstractBuffer(final Buffer buffer, final int elementSize, final int capacity) {
69 this.buffer = buffer;
70 this.elementSize = elementSize;
71 this.capacity = capacity;
72 this.limit = capacity;
73
74 this.position = 0;
75 }
76
77 @Override
78 public final int elementSize() {
79 return elementSize;
80 }
81
82 @Override
83 public final int limit() {
84 return limit;
85 }
86
87 @SuppressWarnings("unchecked")
88 @Override
89 public final B limit(final int newLim) {
90 if (0 > newLim || newLim >= capacity) {
91 throw new IllegalArgumentException("New limit "+newLim+" out of bounds [0 .. capacity " +capacity()+"].");
92 }
93 limit = newLim;
94 return (B)this;
95 }
96
97 @Override
98 public final int capacity() {
99 return capacity;
100 }
101
102 @Override
103 public final int position() {
104 return position;
105 }
106
107 @SuppressWarnings("unchecked")
108 @Override
109 public final B position(final int newPos) {
110 if (0 > newPos || newPos > limit) {
111 throw new IllegalArgumentException("New position "+newPos+" out of bounds [0 .. limit " +limit()+"].");
112 }
113 position = newPos;
114 return (B)this;
115 }
116
117 @Override
118 public final int remaining() {
119 return limit - position;
120 }
121
122 @Override
123 public final boolean hasRemaining() {
124 return limit > position;
125 }
126
127 @SuppressWarnings("unchecked")
128 @Override
129 public final B clear() {
130 limit = capacity;
131 position = 0;
132 return (B) this;
133 }
134
135 @SuppressWarnings("unchecked")
136 @Override
137 public final B flip() {
138 limit = position;
139 position = 0;
140 return (B) this;
141 }
142
143 @SuppressWarnings("unchecked")
144 @Override
145 public final B rewind() {
146 position = 0;
147 return (B) this;
148 }
149
150 @Override
151 public final Buffer getBuffer() {
152 return buffer;
153 }
154 @Override
155 public final boolean isDirect() {
156 return buffer.isDirect();
157 }
158 @Override
159 public long getDirectBufferAddress() {
160 if( isDirect() ) {
161 return Buffers.getDirectBufferAddressImpl(buffer);
162 } else {
163 return 0;
164 }
165 }
166 @Override
167 public void storeDirectAddress(final ByteBuffer directDest) {
168 final long addr = getDirectBufferAddress();
169 switch(POINTER_SIZE) {
170 case 4:
171 directDest.putInt(0, (int) ( addr & 0x00000000FFFFFFFFL ) );
172 break;
173 case 8:
174 directDest.putLong(0, addr);
175 break;
176 }
177 directDest.position(directDest.position()+POINTER_SIZE);
178 }
179
180 @Override
181 public void storeDirectAddress(final ByteBuffer directDest, final int destBytePos) {
182 final long addr = getDirectBufferAddress();
183 switch(POINTER_SIZE) {
184 case 4:
185 directDest.putInt(destBytePos, (int) ( addr & 0x00000000FFFFFFFFL ) );
186 break;
187 case 8:
188 directDest.putLong(destBytePos, addr);
189 break;
190 }
191 }
192
193 @Override
194 public final boolean hasArray() {
195 return buffer.hasArray();
196 }
197
198 @Override
199 public final int arrayOffset() {
200 if( hasArray() ) {
201 return buffer.arrayOffset();
202 } else {
203 return 0;
204 }
205 }
206
207 @Override
208 public Object array() throws UnsupportedOperationException {
209 return buffer.array();
210 }
211
212 protected String toSubString() {
213 return "[direct["+isDirect()+", addr 0x"+Long.toHexString(getDirectBufferAddress())+"], hasArray "+hasArray()+", capacity "+capacity+", position "+position+", elementSize "+elementSize+", buffer[capacity "+buffer.capacity()+", lim "+buffer.limit()+", pos "+buffer.position()+"]]";
214 }
215 @Override
216 public String toString() {
217 return "AbstractBuffer"+toSubString();
218 }
219}
com.jogamp.common.nio.AbstractBuffer
Definition: AbstractBuffer.java:44
com.jogamp.common.nio.AbstractBuffer.AbstractBuffer
AbstractBuffer(final Buffer buffer, final int elementSize, final int capacity)
capacity and elementSize should be match the equation w/ target buffer type
Definition: AbstractBuffer.java:68
com.jogamp.common.nio.AbstractBuffer.limit
final B limit(final int newLim)
Definition: AbstractBuffer.java:89
com.jogamp.common.nio.AbstractBuffer.isDirect
final boolean isDirect()
Definition: AbstractBuffer.java:155
com.jogamp.common.nio.AbstractBuffer.getBuffer
final Buffer getBuffer()
Definition: AbstractBuffer.java:151
com.jogamp.common.nio.AbstractBuffer.storeDirectAddress
void storeDirectAddress(final ByteBuffer directDest, final int destBytePos)
Definition: AbstractBuffer.java:181
com.jogamp.common.nio.AbstractBuffer.position
final int position()
Definition: AbstractBuffer.java:103
com.jogamp.common.nio.AbstractBuffer.position
final B position(final int newPos)
Definition: AbstractBuffer.java:109
com.jogamp.common.nio.AbstractBuffer.hasRemaining
final boolean hasRemaining()
Definition: AbstractBuffer.java:123
com.jogamp.common.nio.AbstractBuffer.toSubString
String toSubString()
Definition: AbstractBuffer.java:212
com.jogamp.common.nio.AbstractBuffer.position
int position
Definition: AbstractBuffer.java:52
com.jogamp.common.nio.AbstractBuffer.toString
String toString()
Definition: AbstractBuffer.java:216
com.jogamp.common.nio.AbstractBuffer.capacity
final int capacity()
Definition: AbstractBuffer.java:98
com.jogamp.common.nio.AbstractBuffer.storeDirectAddress
void storeDirectAddress(final ByteBuffer directDest)
Definition: AbstractBuffer.java:167
com.jogamp.common.nio.AbstractBuffer.capacity
final int capacity
Definition: AbstractBuffer.java:50
com.jogamp.common.nio.AbstractBuffer.buffer
final Buffer buffer
Definition: AbstractBuffer.java:48
com.jogamp.common.nio.AbstractBuffer.elementSize
final int elementSize()
Definition: AbstractBuffer.java:78
com.jogamp.common.nio.AbstractBuffer.POINTER_SIZE
static final int POINTER_SIZE
Platform dependent pointer size in bytes, i.e.
Definition: AbstractBuffer.java:46
com.jogamp.common.nio.AbstractBuffer.getDirectBufferAddress
long getDirectBufferAddress()
Definition: AbstractBuffer.java:159
com.jogamp.common.nio.AbstractBuffer.limit
final int limit()
Definition: AbstractBuffer.java:83
com.jogamp.common.nio.AbstractBuffer.limit
int limit
Definition: AbstractBuffer.java:51
com.jogamp.common.nio.AbstractBuffer.array
Object array()
Definition: AbstractBuffer.java:208
com.jogamp.common.nio.AbstractBuffer.hasArray
final boolean hasArray()
Definition: AbstractBuffer.java:194
com.jogamp.common.nio.AbstractBuffer.arrayOffset
final int arrayOffset()
Definition: AbstractBuffer.java:199
com.jogamp.common.nio.AbstractBuffer.remaining
final int remaining()
Definition: AbstractBuffer.java:118
com.jogamp.common.nio.AbstractBuffer.clear
final B clear()
Definition: AbstractBuffer.java:129
com.jogamp.common.nio.AbstractBuffer.elementSize
final int elementSize
Definition: AbstractBuffer.java:49
com.jogamp.common.nio.AbstractBuffer.rewind
final B rewind()
Definition: AbstractBuffer.java:145
com.jogamp.common.nio.AbstractBuffer.flip
final B flip()
Definition: AbstractBuffer.java:137
com.jogamp.common.nio.Buffers
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.initSingleton
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
com.jogamp.common.os.Platform.is32Bit
static boolean is32Bit()
Returns true if this JVM/ARCH is 32bit.
Definition: Platform.java:423
com.jogamp.common.nio.NativeBuffer
Hardware independent container for various kinds of buffers.
Definition: NativeBuffer.java:49