jaulib v1.3.0
Jau Support Library (C++, Java, ..)
StructAccessor.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Author: Kenneth Bradley Russell
4 * Copyright (c) 2021 Gothel Software e.K.
5 * Copyright (c) 2015 Gothel Software e.K.
6 * Copyright (c) 2015 JogAmp Community.
7 * Copyright (c) 2003 Sun Microsystems.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 */
28package org.jau.io;
29
30import java.nio.ByteBuffer;
31import java.nio.ByteOrder;
32
33public class StructAccessor {
34
35 private final ByteBuffer bb;
36
37 public StructAccessor(final ByteBuffer bb) {
38 // Setting of byte order is concession to native code which needs
39 // to instantiate these
40 this.bb = bb.order(ByteOrder.nativeOrder());
41 }
42
43 public final ByteBuffer getBuffer() {
44 return bb;
45 }
46
47 /**
48 * Returns a slice of the current ByteBuffer starting at the
49 * specified byte offset and extending the specified number of
50 * bytes. Note that this method is not thread-safe with respect to
51 * the other methods in this class.
52 */
53 public final ByteBuffer slice(final int byteOffset, final int byteLength) {
54 bb.position(byteOffset);
55 bb.limit(byteOffset + byteLength);
56 final ByteBuffer newBuf = bb.slice().order(bb.order()); // slice and duplicate may change byte order
57 bb.position(0);
58 bb.limit(bb.capacity());
59 return newBuf;
60 }
61
62 /** Retrieves the byte at the specified byteOffset. */
63 public final byte getByteAt(final int byteOffset) {
64 return bb.get(byteOffset);
65 }
66
67 /** Puts a byte at the specified byteOffset. */
68 public final void setByteAt(final int byteOffset, final byte v) {
69 bb.put(byteOffset, v);
70 }
71
72 /** Retrieves the boolean at the specified byteOffset. */
73 public final boolean getBooleanAt(final int byteOffset) {
74 return (byte)0 != bb.get(byteOffset);
75 }
76
77 /** Puts a boolean at the specified byteOffset. */
78 public final void setBooleanAt(final int byteOffset, final boolean v) {
79 bb.put(byteOffset, v?(byte)1:(byte)0);
80 }
81
82 /** Retrieves the char at the specified byteOffset. */
83 public final char getCharAt(final int byteOffset) {
84 return bb.getChar(byteOffset);
85 }
86
87 /** Puts a char at the specified byteOffset. */
88 public final void setCharAt(final int byteOffset, final char v) {
89 bb.putChar(byteOffset, v);
90 }
91
92 /** Retrieves the short at the specified byteOffset. */
93 public final short getShortAt(final int byteOffset) {
94 return bb.getShort(byteOffset);
95 }
96
97 /** Puts a short at the specified byteOffset. */
98 public final void setShortAt(final int byteOffset, final short v) {
99 bb.putShort(byteOffset, v);
100 }
101
102 /** Retrieves the int at the specified byteOffset. */
103 public final int getIntAt(final int byteOffset) {
104 return bb.getInt(byteOffset);
105 }
106
107 /** Puts a int at the specified byteOffset. */
108 public final void setIntAt(final int byteOffset, final int v) {
109 bb.putInt(byteOffset, v);
110 }
111
112 /** Retrieves the int at the specified byteOffset. */
113 public final int getIntAt(final int byteOffset, final int nativeSizeInBytes) {
114 switch(nativeSizeInBytes) {
115 case 2:
116 return bb.getShort(byteOffset) & 0x0000FFFF ;
117 case 4:
118 return bb.getInt(byteOffset);
119 case 8:
120 return (int) ( bb.getLong(byteOffset) & 0x00000000FFFFFFFFL ) ;
121 default:
122 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
123 }
124 }
125
126 /** Puts a int at the specified byteOffset. */
127 public final void setIntAt(final int byteOffset, final int v, final int nativeSizeInBytes) {
128 switch(nativeSizeInBytes) {
129 case 2:
130 bb.putShort(byteOffset, (short) ( v & 0x0000FFFF ) );
131 break;
132 case 4:
133 bb.putInt(byteOffset, v);
134 break;
135 case 8:
136 bb.putLong(byteOffset, v & 0x00000000FFFFFFFFL );
137 break;
138 default:
139 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
140 }
141 }
142
143 /** Retrieves the float at the specified byteOffset. */
144 public final float getFloatAt(final int byteOffset) {
145 return bb.getFloat(byteOffset);
146 }
147
148 /** Puts a float at the specified byteOffset. */
149 public final void setFloatAt(final int byteOffset, final float v) {
150 bb.putFloat(byteOffset, v);
151 }
152
153 /** Retrieves the double at the specified byteOffset. */
154 public final double getDoubleAt(final int byteOffset) {
155 return bb.getDouble(byteOffset);
156 }
157
158 /** Puts a double at the specified byteOffset. */
159 public final void setDoubleAt(final int byteOffset, final double v) {
160 bb.putDouble(byteOffset, v);
161 }
162
163 /** Retrieves the long at the specified byteOffset. */
164 public final long getLongAt(final int byteOffset) {
165 return bb.getLong(byteOffset);
166 }
167
168 /** Puts a long at the specified byteOffset. */
169 public final void setLongAt(final int byteOffset, final long v) {
170 bb.putLong(byteOffset, v);
171 }
172
173 /** Retrieves the long at the specified byteOffset. */
174 public final long getLongAt(final int byteOffset, final int nativeSizeInBytes) {
175 switch(nativeSizeInBytes) {
176 case 4:
177 return bb.getInt(byteOffset) & 0x00000000FFFFFFFFL;
178 case 8:
179 return bb.getLong(byteOffset);
180 default:
181 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
182 }
183 }
184
185 /** Puts a long at the specified byteOffset. */
186 public final void setLongAt(final int byteOffset, final long v, final int nativeSizeInBytes) {
187 switch(nativeSizeInBytes) {
188 case 4:
189 bb.putInt(byteOffset, (int) ( v & 0x00000000FFFFFFFFL ) );
190 break;
191 case 8:
192 bb.putLong(byteOffset, v);
193 break;
194 default:
195 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
196 }
197 }
198
199 public final void setBytesAt(int byteOffset, final byte[] v) {
200 for (int i = 0; i < v.length; i++) {
201 bb.put(byteOffset++, v[i]);
202 }
203 }
204
205 public final byte[] getBytesAt(int byteOffset, final byte[] v) {
206 for (int i = 0; i < v.length; i++) {
207 v[i] = bb.get(byteOffset++);
208 }
209 return v;
210 }
211
212 public final void setBooleansAt(int byteOffset, final boolean[] v) {
213 for (int i = 0; i < v.length; i++) {
214 bb.put(byteOffset++, v[i]?(byte)1:(byte)0);
215 }
216 }
217
218 public final boolean[] getBooleansAt(int byteOffset, final boolean[] v) {
219 for (int i = 0; i < v.length; i++) {
220 v[i] = (byte)0 != bb.get(byteOffset++);
221 }
222 return v;
223 }
224
225 public final void setCharsAt(int byteOffset, final char[] v) {
226 for (int i = 0; i < v.length; i++, byteOffset+=2) {
227 bb.putChar(byteOffset, v[i]);
228 }
229 }
230
231 public final char[] getCharsAt(int byteOffset, final char[] v) {
232 for (int i = 0; i < v.length; i++, byteOffset+=2) {
233 v[i] = bb.getChar(byteOffset);
234 }
235 return v;
236 }
237
238 public final void setShortsAt(int byteOffset, final short[] v) {
239 for (int i = 0; i < v.length; i++, byteOffset+=2) {
240 bb.putShort(byteOffset, v[i]);
241 }
242 }
243
244 public final short[] getShortsAt(int byteOffset, final short[] v) {
245 for (int i = 0; i < v.length; i++, byteOffset+=2) {
246 v[i] = bb.getShort(byteOffset);
247 }
248 return v;
249 }
250
251 public final void setIntsAt(int byteOffset, final int[] v) {
252 for (int i = 0; i < v.length; i++, byteOffset+=4) {
253 bb.putInt(byteOffset, v[i]);
254 }
255 }
256
257 public final int[] getIntsAt(int byteOffset, final int[] v) {
258 for (int i = 0; i < v.length; i++, byteOffset+=4) {
259 v[i] = bb.getInt(byteOffset);
260 }
261 return v;
262 }
263
264 public final void setFloatsAt(int byteOffset, final float[] v) {
265 for (int i = 0; i < v.length; i++, byteOffset+=4) {
266 bb.putFloat(byteOffset, v[i]);
267 }
268 }
269
270 public final float[] getFloatsAt(int byteOffset, final float[] v) {
271 for (int i = 0; i < v.length; i++, byteOffset+=4) {
272 v[i] = bb.getFloat(byteOffset);
273 }
274 return v;
275 }
276
277 public final void setDoublesAt(int byteOffset, final double[] v) {
278 for (int i = 0; i < v.length; i++, byteOffset+=8) {
279 bb.putDouble(byteOffset, v[i]);
280 }
281 }
282
283 public final double[] getDoublesAt(int byteOffset, final double[] v) {
284 for (int i = 0; i < v.length; i++, byteOffset+=8) {
285 v[i] = bb.getDouble(byteOffset);
286 }
287 return v;
288 }
289
290 public final void setLongsAt(int byteOffset, final long[] v) {
291 for (int i = 0; i < v.length; i++, byteOffset+=8) {
292 bb.putLong(byteOffset, v[i]);
293 }
294 }
295
296 public final long[] getLongsAt(int byteOffset, final long[] v) {
297 for (int i = 0; i < v.length; i++, byteOffset+=8) {
298 v[i] = bb.getLong(byteOffset);
299 }
300 return v;
301 }
302}
final byte[] getBytesAt(int byteOffset, final byte[] v)
StructAccessor(final ByteBuffer bb)
final char[] getCharsAt(int byteOffset, final char[] v)
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 int getIntAt(final int byteOffset, final int nativeSizeInBytes)
Retrieves the int at the specified byteOffset.
final void setLongAt(final int byteOffset, final long v)
Puts a long at the specified byteOffset.
final char getCharAt(final int byteOffset)
Retrieves the char at the specified byteOffset.
final boolean[] getBooleansAt(int byteOffset, final boolean[] v)
final long getLongAt(final int byteOffset, final int nativeSizeInBytes)
Retrieves the long at the specified byteOffset.
final void setBooleansAt(int byteOffset, final boolean[] v)
final void setIntAt(final int byteOffset, final int v)
Puts a int at the specified byteOffset.
final void setFloatsAt(int byteOffset, final float[] v)
final void setShortsAt(int byteOffset, final short[] v)
final short[] getShortsAt(int byteOffset, final short[] v)
final void setIntsAt(int byteOffset, final int[] v)
final void setBooleanAt(final int byteOffset, final boolean v)
Puts a boolean at the specified byteOffset.
final void setCharsAt(int byteOffset, final char[] v)
final void setIntAt(final int byteOffset, final int v, final int nativeSizeInBytes)
Puts a int at the specified byteOffset.
final void setBytesAt(int byteOffset, final byte[] v)
final ByteBuffer getBuffer()
final float[] getFloatsAt(int byteOffset, final float[] v)
final short getShortAt(final int byteOffset)
Retrieves the short at the specified byteOffset.
final float getFloatAt(final int byteOffset)
Retrieves the float at the specified byteOffset.
final void setLongAt(final int byteOffset, final long v, final int nativeSizeInBytes)
Puts a long at the specified byteOffset.
final byte getByteAt(final int byteOffset)
Retrieves the byte at the specified byteOffset.
final void setShortAt(final int byteOffset, final short v)
Puts a short at the specified byteOffset.
final long[] getLongsAt(int byteOffset, final long[] v)
final int[] getIntsAt(int byteOffset, final int[] v)
final double getDoubleAt(final int byteOffset)
Retrieves the double at the specified byteOffset.
final double[] getDoublesAt(int byteOffset, final double[] v)
final void setFloatAt(final int byteOffset, final float v)
Puts a float at the specified byteOffset.
final void setByteAt(final int byteOffset, final byte v)
Puts a byte at the specified byteOffset.
final int getIntAt(final int byteOffset)
Retrieves the int at the specified byteOffset.
final boolean getBooleanAt(final int byteOffset)
Retrieves the boolean at the specified byteOffset.
final void setLongsAt(int byteOffset, final long[] v)
final void setCharAt(final int byteOffset, final char v)
Puts a char at the specified byteOffset.
final long getLongAt(final int byteOffset)
Retrieves the long at the specified byteOffset.
final void setDoublesAt(int byteOffset, final double[] v)
final void setDoubleAt(final int byteOffset, final double v)
Puts a double at the specified byteOffset.