GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
StructAccessor.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40package com.jogamp.common.nio;
41
42import java.nio.*;
43
44/**
45 * @author Kenneth Russel, Sven Gothel, Michael Bien, et al.
46 */
47public class StructAccessor {
48
49 private final ByteBuffer bb;
50
51 /** Create a new instance. The {@link ByteBuffer} will be {@link ByteBuffer#rewind()} and native-order to be used with native code set. */
52 public StructAccessor(final ByteBuffer bb) {
53 this.bb = bb.order(ByteOrder.nativeOrder());
54 this.bb.rewind();
55 }
56
57 /** Return the underlying native direct ByteBuffer */
58 public final ByteBuffer getBuffer() {
59 return bb;
60 }
61
62 /** Returns the native address of the underlying native ByteBuffer. */
63 public final long getDirectBufferAddress() {
64 return Buffers.getDirectBufferAddressImpl(bb);
65 }
66
67 /**
68 * Returns a slice of the current ByteBuffer starting at the
69 * specified byte offset and extending the specified number of
70 * bytes. Note that this method is not thread-safe with respect to
71 * the other methods in this class.
72 */
73 public final ByteBuffer slice(final int byteOffset, final int byteLength) {
74 bb.position(byteOffset);
75 bb.limit(byteOffset + byteLength);
76 final ByteBuffer newBuf = bb.slice().order(bb.order()); // slice and duplicate may change byte order
77 bb.position(0);
78 bb.limit(bb.capacity());
79 return newBuf;
80 }
81
82 /** Retrieves the byte at the specified byteOffset. */
83 public final byte getByteAt(final int byteOffset) {
84 return bb.get(byteOffset);
85 }
86
87 /** Puts a byte at the specified byteOffset. */
88 public final void setByteAt(final int byteOffset, final byte v) {
89 bb.put(byteOffset, v);
90 }
91
92 /** Retrieves the boolean at the specified byteOffset. */
93 public final boolean getBooleanAt(final int byteOffset) {
94 return (byte)0 != bb.get(byteOffset);
95 }
96
97 /** Puts a boolean at the specified byteOffset. */
98 public final void setBooleanAt(final int byteOffset, final boolean v) {
99 bb.put(byteOffset, v?(byte)1:(byte)0);
100 }
101
102 /** Retrieves the char at the specified byteOffset. */
103 public final char getCharAt(final int byteOffset) {
104 return bb.getChar(byteOffset);
105 }
106
107 /** Puts a char at the specified byteOffset. */
108 public final void setCharAt(final int byteOffset, final char v) {
109 bb.putChar(byteOffset, v);
110 }
111
112 /** Retrieves the short at the specified byteOffset. */
113 public final short getShortAt(final int byteOffset) {
114 return bb.getShort(byteOffset);
115 }
116
117 /** Puts a short at the specified byteOffset. */
118 public final void setShortAt(final int byteOffset, final short v) {
119 bb.putShort(byteOffset, v);
120 }
121
122 /** Retrieves the int at the specified byteOffset. */
123 public final int getIntAt(final int byteOffset) {
124 return bb.getInt(byteOffset);
125 }
126
127 /** Puts a int at the specified byteOffset. */
128 public final void setIntAt(final int byteOffset, final int v) {
129 bb.putInt(byteOffset, v);
130 }
131
132 /** Retrieves the int at the specified byteOffset. */
133 public final int getIntAt(final int byteOffset, final int nativeSizeInBytes) {
134 switch(nativeSizeInBytes) {
135 case 2:
136 return bb.getShort(byteOffset) & 0x0000FFFF ;
137 case 4:
138 return bb.getInt(byteOffset);
139 case 8:
140 return (int) ( bb.getLong(byteOffset) & 0x00000000FFFFFFFFL ) ;
141 default:
142 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
143 }
144 }
145
146 /** Puts a int at the specified byteOffset. */
147 public final void setIntAt(final int byteOffset, final int v, final int nativeSizeInBytes) {
148 switch(nativeSizeInBytes) {
149 case 2:
150 bb.putShort(byteOffset, (short) ( v & 0x0000FFFF ) );
151 break;
152 case 4:
153 bb.putInt(byteOffset, v);
154 break;
155 case 8:
156 bb.putLong(byteOffset, v & 0x00000000FFFFFFFFL );
157 break;
158 default:
159 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
160 }
161 }
162
163 /** Retrieves the float at the specified byteOffset. */
164 public final float getFloatAt(final int byteOffset) {
165 return bb.getFloat(byteOffset);
166 }
167
168 /** Puts a float at the specified byteOffset. */
169 public final void setFloatAt(final int byteOffset, final float v) {
170 bb.putFloat(byteOffset, v);
171 }
172
173 /** Retrieves the double at the specified byteOffset. */
174 public final double getDoubleAt(final int byteOffset) {
175 return bb.getDouble(byteOffset);
176 }
177
178 /** Puts a double at the specified byteOffset. */
179 public final void setDoubleAt(final int byteOffset, final double v) {
180 bb.putDouble(byteOffset, v);
181 }
182
183 /** Retrieves the long at the specified byteOffset. */
184 public final long getLongAt(final int byteOffset) {
185 return bb.getLong(byteOffset);
186 }
187
188 /** Puts a long at the specified byteOffset. */
189 public final void setLongAt(final int byteOffset, final long v) {
190 bb.putLong(byteOffset, v);
191 }
192
193 /** Retrieves the long at the specified byteOffset. */
194 public final long getLongAt(final int byteOffset, final int nativeSizeInBytes) {
195 switch(nativeSizeInBytes) {
196 case 4:
197 return bb.getInt(byteOffset) & 0x00000000FFFFFFFFL;
198 case 8:
199 return bb.getLong(byteOffset);
200 default:
201 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
202 }
203 }
204
205 /** Puts a long at the specified byteOffset. */
206 public final void setLongAt(final int byteOffset, final long v, final int nativeSizeInBytes) {
207 switch(nativeSizeInBytes) {
208 case 4:
209 bb.putInt(byteOffset, (int) ( v & 0x00000000FFFFFFFFL ) );
210 break;
211 case 8:
212 bb.putLong(byteOffset, v);
213 break;
214 default:
215 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
216 }
217 }
218
219 public final void setBytesAt(int byteOffset, final byte[] v) {
220 for (int i = 0; i < v.length; i++) {
221 bb.put(byteOffset++, v[i]);
222 }
223 }
224
225 public final byte[] getBytesAt(int byteOffset, final byte[] v) {
226 for (int i = 0; i < v.length; i++) {
227 v[i] = bb.get(byteOffset++);
228 }
229 return v;
230 }
231
232 public final void setBooleansAt(int byteOffset, final boolean[] v) {
233 for (int i = 0; i < v.length; i++) {
234 bb.put(byteOffset++, v[i]?(byte)1:(byte)0);
235 }
236 }
237
238 public final boolean[] getBooleansAt(int byteOffset, final boolean[] v) {
239 for (int i = 0; i < v.length; i++) {
240 v[i] = (byte)0 != bb.get(byteOffset++);
241 }
242 return v;
243 }
244
245 public final void setCharsAt(int byteOffset, final char[] v) {
246 for (int i = 0; i < v.length; i++, byteOffset+=2) {
247 bb.putChar(byteOffset, v[i]);
248 }
249 }
250
251 public final char[] getCharsAt(int byteOffset, final char[] v) {
252 for (int i = 0; i < v.length; i++, byteOffset+=2) {
253 v[i] = bb.getChar(byteOffset);
254 }
255 return v;
256 }
257
258 public final void setShortsAt(int byteOffset, final short[] v) {
259 for (int i = 0; i < v.length; i++, byteOffset+=2) {
260 bb.putShort(byteOffset, v[i]);
261 }
262 }
263
264 public final short[] getShortsAt(int byteOffset, final short[] v) {
265 for (int i = 0; i < v.length; i++, byteOffset+=2) {
266 v[i] = bb.getShort(byteOffset);
267 }
268 return v;
269 }
270
271 public final void setIntsAt(int byteOffset, final int[] v) {
272 for (int i = 0; i < v.length; i++, byteOffset+=4) {
273 bb.putInt(byteOffset, v[i]);
274 }
275 }
276
277 public final int[] getIntsAt(int byteOffset, final int[] v) {
278 for (int i = 0; i < v.length; i++, byteOffset+=4) {
279 v[i] = bb.getInt(byteOffset);
280 }
281 return v;
282 }
283
284 public final void setFloatsAt(int byteOffset, final float[] v) {
285 for (int i = 0; i < v.length; i++, byteOffset+=4) {
286 bb.putFloat(byteOffset, v[i]);
287 }
288 }
289
290 public final float[] getFloatsAt(int byteOffset, final float[] v) {
291 for (int i = 0; i < v.length; i++, byteOffset+=4) {
292 v[i] = bb.getFloat(byteOffset);
293 }
294 return v;
295 }
296
297 public final void setDoublesAt(int byteOffset, final double[] v) {
298 for (int i = 0; i < v.length; i++, byteOffset+=8) {
299 bb.putDouble(byteOffset, v[i]);
300 }
301 }
302
303 public final double[] getDoublesAt(int byteOffset, final double[] v) {
304 for (int i = 0; i < v.length; i++, byteOffset+=8) {
305 v[i] = bb.getDouble(byteOffset);
306 }
307 return v;
308 }
309
310 public final void setLongsAt(int byteOffset, final long[] v) {
311 for (int i = 0; i < v.length; i++, byteOffset+=8) {
312 bb.putLong(byteOffset, v[i]);
313 }
314 }
315
316 public final long[] getLongsAt(int byteOffset, final long[] v) {
317 for (int i = 0; i < v.length; i++, byteOffset+=8) {
318 v[i] = bb.getLong(byteOffset);
319 }
320 return v;
321 }
322}
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
final long[] getLongsAt(int byteOffset, final long[] v)
StructAccessor(final ByteBuffer bb)
Create a new instance.
final void setLongAt(final int byteOffset, final long v)
Puts a long at the specified byteOffset.
final byte[] getBytesAt(int byteOffset, final byte[] v)
final long getDirectBufferAddress()
Returns the native address of the underlying native ByteBuffer.
final int getIntAt(final int byteOffset)
Retrieves the int at the specified byteOffset.
final void setCharAt(final int byteOffset, final char v)
Puts a char at the specified byteOffset.
final double getDoubleAt(final int byteOffset)
Retrieves the double at the specified byteOffset.
final long getLongAt(final int byteOffset, final int nativeSizeInBytes)
Retrieves the long at the specified byteOffset.
final void setIntAt(final int byteOffset, final int v, final int nativeSizeInBytes)
Puts a int at the specified byteOffset.
final float[] getFloatsAt(int byteOffset, final float[] v)
final int getIntAt(final int byteOffset, final int nativeSizeInBytes)
Retrieves the int at the specified byteOffset.
final short[] getShortsAt(int byteOffset, final short[] v)
final long getLongAt(final int byteOffset)
Retrieves the long at the specified byteOffset.
final void setFloatAt(final int byteOffset, final float v)
Puts a float at the specified byteOffset.
final void setShortAt(final int byteOffset, final short v)
Puts a short at the specified byteOffset.
final void setDoubleAt(final int byteOffset, final double v)
Puts a double at the specified byteOffset.
final void setFloatsAt(int byteOffset, final float[] v)
final void setCharsAt(int byteOffset, final char[] v)
final byte getByteAt(final int byteOffset)
Retrieves the byte at the specified byteOffset.
final char[] getCharsAt(int byteOffset, final char[] v)
final int[] getIntsAt(int byteOffset, final int[] v)
final ByteBuffer getBuffer()
Return the underlying native direct ByteBuffer.
final void setIntsAt(int byteOffset, final int[] v)
final float getFloatAt(final int byteOffset)
Retrieves the float at the specified byteOffset.
final char getCharAt(final int byteOffset)
Retrieves the char at the specified byteOffset.
final void setShortsAt(int byteOffset, final short[] v)
final void setIntAt(final int byteOffset, final int v)
Puts a int at the specified byteOffset.
final void setBooleansAt(int byteOffset, final boolean[] v)
final double[] getDoublesAt(int byteOffset, final double[] v)
final void setBytesAt(int byteOffset, final byte[] v)
final short getShortAt(final int byteOffset)
Retrieves the short at the specified byteOffset.
final void setByteAt(final int byteOffset, final byte v)
Puts a byte at the specified byteOffset.
final ByteBuffer slice(final int byteOffset, final int byteLength)
Returns a slice of the current ByteBuffer starting at the specified byte offset and extending the spe...
final void setLongAt(final int byteOffset, final long v, final int nativeSizeInBytes)
Puts a long at the specified byteOffset.
final void setDoublesAt(int byteOffset, final double[] v)
final boolean[] getBooleansAt(int byteOffset, final boolean[] v)
final boolean getBooleanAt(final int byteOffset)
Retrieves the boolean at the specified byteOffset.
final void setBooleanAt(final int byteOffset, final boolean v)
Puts a boolean at the specified byteOffset.
final void setLongsAt(int byteOffset, final long[] v)