GlueGen v2.6.0-rc-20250712
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 */
32package 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
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}
AbstractBuffer(final Buffer buffer, final int elementSize, final int capacity)
capacity and elementSize should be match the equation w/ target buffer type
final B limit(final int newLim)
void storeDirectAddress(final ByteBuffer directDest, final int destBytePos)
final B position(final int newPos)
void storeDirectAddress(final ByteBuffer directDest)
static final int POINTER_SIZE
Platform dependent pointer size in bytes, i.e.
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
Utility class for querying platform specific properties.
Definition: Platform.java:58
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
static boolean is32Bit()
Returns true if this JVM/ARCH is 32bit.
Definition: Platform.java:423
Hardware independent container for various kinds of buffers.