GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
ElementBuffer.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
29package com.jogamp.common.nio;
30
31import java.nio.ByteBuffer;
32import java.nio.CharBuffer;
33import java.nio.DoubleBuffer;
34import java.nio.FloatBuffer;
35import java.nio.IntBuffer;
36import java.nio.LongBuffer;
37import java.nio.ShortBuffer;
38
39/**
40 * Hardware independent container holding an array of linearly aligned elements,
41 * while its {@link #getDirectBufferAddress()} is-a pointer-type value, i.e. the element-array address.
42 * <p>
43 * An instance maps an array of linearly aligned elements, represented as bytes.
44 * </p>
45 */
46public class ElementBuffer extends AbstractBuffer<ElementBuffer> {
47 /** supports backup array */
48 ElementBuffer(final int elementSize, final ByteBuffer b) {
49 super(b, elementSize, b.capacity()/elementSize);
50 }
51
52 /** Returns a non direct PointerBuffer in native order, having a backup array */
53 public static ElementBuffer allocate(final int elementSize, final int elemCount) {
54 return new ElementBuffer(elementSize, ByteBuffer.allocate(elemCount * elementSize));
55 }
56
57 /** Returns a direct PointerBuffer in native order, w/o backup array */
58 public static ElementBuffer allocateDirect(final int elementSize, final int elemCount) {
60 }
61
62 public static ElementBuffer wrap(final int elementSize, final ByteBuffer src) {
63 return new ElementBuffer(elementSize, src);
64 }
65 public static ElementBuffer wrap(final int elementSize, final ByteBuffer src, final int srcByteOffset, final int elemCount) {
66 final int oldPos = src.position();
67 final int oldLimit = src.limit();
68 src.position(srcByteOffset);
69 src.limit(srcByteOffset + ( elementSize * elemCount ));
70 final ElementBuffer r = new ElementBuffer(elementSize, src.slice().order(src.order())); // slice and duplicate may change byte order
71 src.position(oldPos);
72 src.limit(oldLimit);
73 return r;
74 }
75 public static ElementBuffer derefPointer(final int elementSize, final long aptr, final int elemCount) {
76 if( 0 == aptr ) {
77 throw new NullPointerException("aptr is null");
78 }
79 final ByteBuffer bb = Buffers.getDirectByteBuffer(aptr, elemCount * elementSize);
80 if( null == bb ) {
81 throw new InternalError("Couldn't dereference aptr 0x"+Long.toHexString(aptr)+", size "+elemCount+" * "+elementSize);
82 }
83 return new ElementBuffer(elementSize, bb);
84 }
85 public static ElementBuffer derefPointer(final int elementSize, final ByteBuffer ptrSrc, final int ptrSrcByteOffset, final int elemCount) {
86 return derefPointer(elementSize, PointerBuffer.wrap(ptrSrc, ptrSrcByteOffset, 1).get(0), elemCount);
87 }
88
89 @Override
90 public final ElementBuffer put(final ElementBuffer src) {
91 final int elemCount = src.remaining();
92 if (remaining() < elemCount) {
93 throw new IndexOutOfBoundsException("remaining[this "+remaining()+" < src "+elemCount+"], this "+this+", src "+src);
94 }
95 if( this.elementSize() != src.elementSize() ) {
96 throw new IllegalArgumentException("Element-Size mismatch source "+src+", dest "+this);
97 }
98 final int srcPos = src.position();
99 put(src.getByteBuffer(), srcPos, position, elemCount);
100 src.position(srcPos + elemCount);
101 position += elemCount;
102 return this;
103 }
104
105 /** Returns the ByteBuffer, i.e. {@link #getBuffer()} w/o casting. */
106 public final ByteBuffer getByteBuffer() {
107 return (ByteBuffer) buffer;
108 }
109
110 /**
111 * Returns a slice of this instance's ByteBuffer `[offset..offset+length)`, i.e. referencing the underlying bytes.
112 * @param offset starting element-index within this buffer
113 * @param length element count
114 * @return slice of this instance's ByteBuffer
115 */
116 public final ByteBuffer slice(final int offset, final int length) {
117 if (0 > offset || offset + length > limit()) {
118 throw new IndexOutOfBoundsException("idx "+offset+" + elemCount "+length+" not within [0.."+limit()+"), "+this);
119 }
120 final ByteBuffer src = getByteBuffer();
121 final int oldPos = src.position();
122 final int oldLimit = src.limit();
123 src.position( elementSize * offset ).limit(elementSize * (offset + length));
124 final ByteBuffer ref = src.slice().order(src.order()); // slice and duplicate may change byte order
125 src.position( oldPos ).limit( oldLimit );
126 return ref;
127 }
128
129 /** Absolute get method. Get element-bytes for `elemCount` elements from this buffer at `srcElemPos` into `destElemBytes` at the given element-index `destElemPos` */
130 public final ByteBuffer get(final int srcElemPos, final ByteBuffer destElemBytes, final int destElemPos, final int elemCount) {
131 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
132 0 > destElemPos || elementSize * ( destElemPos + elemCount ) > destElemBytes.limit() ) {
133 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
134 ", srcLimit "+limit()+", destLimit "+(destElemBytes.limit()/elementSize)+", "+this);
135 }
136 final ByteBuffer srcElemBytes = getByteBuffer();
137 final int oldSrcLim = srcElemBytes.limit();
138 srcElemBytes.position( srcElemPos * elementSize ).limit( ( srcElemPos + elemCount ) * elementSize ); // remaining = elemCount * elementSize
139 final int oldDestPos = destElemBytes.position();
140 destElemBytes.position( destElemPos * elementSize );
141 destElemBytes.put(srcElemBytes).position(oldDestPos);
142 srcElemBytes.limit(oldSrcLim).rewind();
143 return destElemBytes;
144 }
145 /** Absolute get method. Copy the element-bytes from this buffer at the given element-index `srcElemPos`, storing them into `destElemBytes`. */
146 public final ByteBuffer get(final int srcElemPos, final ByteBuffer destElemBytes) {
147 return get(srcElemPos, destElemBytes, 0, 1);
148 }
149 /** Relative get method. Copy the element-bytes at the current position and increment the position by one, storing the element-bytes into `destElemBytes`. */
150 public final ByteBuffer get(final ByteBuffer destElemBytes) {
151 final ByteBuffer r = get(position, destElemBytes, 0, 1);
152 position++;
153 return r;
154 }
155 /**
156 * Relative bulk get method. Copy the element-bytes <code> [ position .. position+elemCount [</code>
157 * to the destination array <code> [ destElements[destElemPos] .. destElements[destElemPos+elemCount] [ </code>
158 * and increment the position by <code>elemCount</code>. */
159 public final ElementBuffer get(final ByteBuffer[] destElements, int destElemPos, int elemCount) {
160 if (destElements.length<destElemPos+elemCount) {
161 throw new IndexOutOfBoundsException("dest.length "+destElements.length+" < (offset "+destElemPos+" + length "+elemCount+")");
162 }
163 if (remaining() < elemCount) {
164 throw new IndexOutOfBoundsException("remaining "+remaining()+" < length "+elemCount+", this "+this);
165 }
166 while(elemCount>0) {
167 get(position++, destElements[destElemPos++]);
168 elemCount--;
169 }
170 return this;
171 }
172
173 /** Absolute get method. Get byte-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
174 public final ElementBuffer get(final int srcElemPos, final byte[] dest, final int destElemPos, final int elemCount) {
175 if( Buffers.SIZEOF_BYTE != elementSize ) { throw new UnsupportedOperationException("'byte' type byte-size "+Buffers.SIZEOF_BYTE+" != elementSize "+elementSize+", "+this); }
176 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
177 0 > destElemPos || destElemPos + elemCount > dest.length ) {
178 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
179 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
180 }
181 final ByteBuffer src = getByteBuffer();
182 final int oldPos = src.position();
183 final int oldLim = src.limit();
184 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
185 src.get(dest, destElemPos, elemCount);
186 src.position( oldPos ).limit( oldLim );
187 return this;
188 }
189 /** Absolute get method. Get short-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
190 public final ElementBuffer get(final int srcElemPos, final short[] dest, final int destElemPos, final int elemCount) {
191 if( Buffers.SIZEOF_SHORT != elementSize ) { throw new UnsupportedOperationException("'short' type byte-size "+Buffers.SIZEOF_SHORT+" != elementSize "+elementSize+", "+this); }
192 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
193 0 > destElemPos || destElemPos + elemCount > dest.length ) {
194 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
195 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
196 }
197 final ShortBuffer src = getByteBuffer().asShortBuffer();
198 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
199 src.get(dest, destElemPos, elemCount);
200 return this;
201 }
202 /** Absolute get method. Get char-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
203 public final ElementBuffer get(final int srcElemPos, final char[] dest, final int destElemPos, final int elemCount) {
204 if( Buffers.SIZEOF_CHAR != elementSize ) { throw new UnsupportedOperationException("'char' type byte-size "+Buffers.SIZEOF_CHAR+" != elementSize "+elementSize+", "+this); }
205 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
206 0 > destElemPos || destElemPos + elemCount > dest.length ) {
207 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
208 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
209 }
210 final CharBuffer src = getByteBuffer().asCharBuffer();
211 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
212 src.get(dest, destElemPos, elemCount);
213 return this;
214 }
215 /** Absolute get method. Get int-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
216 public final ElementBuffer get(final int srcElemPos, final int[] dest, final int destElemPos, final int elemCount) {
217 if( Buffers.SIZEOF_INT != elementSize ) { throw new UnsupportedOperationException("'int' type byte-size "+Buffers.SIZEOF_INT+" != elementSize "+elementSize+", "+this); }
218 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
219 0 > destElemPos || destElemPos + elemCount > dest.length ) {
220 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
221 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
222 }
223 final IntBuffer src = getByteBuffer().asIntBuffer();
224 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
225 src.get(dest, destElemPos, elemCount);
226 return this;
227 }
228 /** Absolute get method. Get float-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
229 public final ElementBuffer get(final int srcElemPos, final float[] dest, final int destElemPos, final int elemCount) {
230 if( Buffers.SIZEOF_FLOAT != elementSize ) { throw new UnsupportedOperationException("'float' type byte-size "+Buffers.SIZEOF_FLOAT+" != elementSize "+elementSize+", "+this); }
231 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
232 0 > destElemPos || destElemPos + elemCount > dest.length ) {
233 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
234 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
235 }
236 final FloatBuffer src = getByteBuffer().asFloatBuffer();
237 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
238 src.get(dest, destElemPos, elemCount);
239 return this;
240 }
241 /** Absolute get method. Get long-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
242 public final ElementBuffer get(final int srcElemPos, final long[] dest, final int destElemPos, final int elemCount) {
243 if( Buffers.SIZEOF_LONG != elementSize ) { throw new UnsupportedOperationException("'long' type byte-size "+Buffers.SIZEOF_LONG+" != elementSize "+elementSize+", "+this); }
244 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
245 0 > destElemPos || destElemPos + elemCount > dest.length ) {
246 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
247 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
248 }
249 final LongBuffer src = getByteBuffer().asLongBuffer();
250 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
251 src.get(dest, destElemPos, elemCount);
252 return this;
253 }
254 /** Absolute get method. Get double-elements for `elemCount` elements from this buffer at `srcElemPos` into `dest` at the given element-index `destElemPos` */
255 public final ElementBuffer get(final int srcElemPos, final double[] dest, final int destElemPos, final int elemCount) {
256 if( Buffers.SIZEOF_DOUBLE != elementSize ) { throw new UnsupportedOperationException("'double' type byte-size "+Buffers.SIZEOF_DOUBLE+" != elementSize "+elementSize+", "+this); }
257 if (0 > srcElemPos || srcElemPos + elemCount > limit() || 0 > elemCount ||
258 0 > destElemPos || destElemPos + elemCount > dest.length ) {
259 throw new IndexOutOfBoundsException("destElemPos "+destElemPos+", srcElemPos "+srcElemPos+", elemCount "+elemCount+
260 ", srcLimit "+limit()+", destLimit "+dest.length+", "+this);
261 }
262 final DoubleBuffer src = getByteBuffer().asDoubleBuffer();
263 src.position( srcElemPos ).limit( srcElemPos + elemCount ); // remaining = elemCount
264 src.get(dest, destElemPos, elemCount);
265 return this;
266 }
267
268
269 /** Absolute put method. Put element-bytes for `elemCount` elements from `srcElemBytes` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
270 public final ElementBuffer put(final ByteBuffer srcElemBytes, final int srcElemPos, final int destElemPos, final int elemCount) {
271 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
272 0 > srcElemPos || elementSize * ( srcElemPos + elemCount ) > srcElemBytes.limit() ) {
273 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
274 ", destLimit "+limit()+", srcLimit "+(srcElemBytes.limit()/elementSize)+", "+this);
275 }
276 final ByteBuffer destElemBytes = getByteBuffer();
277 final int oldSrcPos = srcElemBytes.position();
278 final int oldSrcLim = srcElemBytes.limit();
279 srcElemBytes.position( srcElemPos * elementSize ).limit( ( srcElemPos + elemCount ) * elementSize ); // remaining = elemCount * elementSize
280 destElemBytes.position( elementSize * destElemPos );
281 destElemBytes.put(srcElemBytes).rewind();
282 srcElemBytes.limit(oldSrcLim).position(oldSrcPos);
283 return this;
284 }
285 /** Absolute put method. Put element-bytes from `srcElemBytes` into the given element-index `destElemPos` */
286 public final ElementBuffer put(final int destElemPos, final ByteBuffer srcElemBytes) {
287 return put(srcElemBytes, 0, destElemPos, 1);
288 }
289 /** Relative put method. Put the element-bytes at the current position and increment the position by one. */
290 public final ElementBuffer put(final ByteBuffer srcElemBytes) {
291 put(position, srcElemBytes);
292 position++;
293 return this;
294 }
295 /**
296 * Relative bulk put method. Put the element-bytes <code> [ srcElements[offset] .. srcElements[offset+length] [</code>
297 * at the current position and increment the position by <code>length</code>. */
298 public final ElementBuffer put(final ByteBuffer[] srcElements, int offset, int length) {
299 if (srcElements.length<offset+length) {
300 throw new IndexOutOfBoundsException("src.length "+srcElements.length+" < (offset "+offset+" + length "+length+")");
301 }
302 if (remaining() < length) {
303 throw new IndexOutOfBoundsException("remaining "+remaining()+" < length "+length+", this "+this);
304 }
305 while(length>0) {
306 put(position++, srcElements[offset++]);
307 length--;
308 }
309 return this;
310 }
311
312 /** Absolute put method. Put byte-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
313 public final ElementBuffer put(final byte[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
314 if( Buffers.SIZEOF_BYTE != elementSize ) { throw new UnsupportedOperationException("'byte' type byte-size "+Buffers.SIZEOF_BYTE+" != elementSize "+elementSize+", "+this); }
315 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
316 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
317 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
318 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
319 }
320 final ByteBuffer dest = getByteBuffer();
321 final int oldPos = dest.position();
322 final int oldLim = dest.limit();
323 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
324 dest.put(src, srcElemPos, elemCount);
325 dest.position( oldPos ).limit( oldLim );
326 return this;
327 }
328 /** Absolute put method. Put short-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
329 public final ElementBuffer put(final short[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
330 if( Buffers.SIZEOF_SHORT != elementSize ) { throw new UnsupportedOperationException("'short' type byte-size "+Buffers.SIZEOF_SHORT+" != elementSize "+elementSize+", "+this); }
331 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
332 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
333 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
334 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
335 }
336 final ShortBuffer dest = getByteBuffer().asShortBuffer();
337 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
338 dest.put(src, srcElemPos, elemCount);
339 return this;
340 }
341 /** Absolute put method. Put char-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
342 public final ElementBuffer put(final char[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
343 if( Buffers.SIZEOF_CHAR != elementSize ) { throw new UnsupportedOperationException("'char' type byte-size "+Buffers.SIZEOF_CHAR+" != elementSize "+elementSize+", "+this); }
344 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
345 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
346 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
347 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
348 }
349 final CharBuffer dest = getByteBuffer().asCharBuffer();
350 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
351 dest.put(src, srcElemPos, elemCount);
352 return this;
353 }
354 /** Absolute put method. Put int-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
355 public final ElementBuffer put(final int[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
356 if( Buffers.SIZEOF_INT != elementSize ) { throw new UnsupportedOperationException("'int' type byte-size "+Buffers.SIZEOF_INT+" != elementSize "+elementSize+", "+this); }
357 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
358 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
359 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
360 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
361 }
362 final IntBuffer dest = getByteBuffer().asIntBuffer();
363 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
364 dest.put(src, srcElemPos, elemCount);
365 return this;
366 }
367 /** Absolute put method. Put float-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
368 public final ElementBuffer put(final float[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
369 if( Buffers.SIZEOF_FLOAT != elementSize ) { throw new UnsupportedOperationException("'float' type byte-size "+Buffers.SIZEOF_FLOAT+" != elementSize "+elementSize+", "+this); }
370 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
371 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
372 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
373 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
374 }
375 final FloatBuffer dest = getByteBuffer().asFloatBuffer();
376 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
377 dest.put(src, srcElemPos, elemCount);
378 return this;
379 }
380 /** Absolute put method. Put long-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
381 public final ElementBuffer put(final long[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
382 if( Buffers.SIZEOF_LONG != elementSize ) { throw new UnsupportedOperationException("'long' type byte-size "+Buffers.SIZEOF_LONG+" != elementSize "+elementSize+", "+this); }
383 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
384 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
385 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
386 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
387 }
388 final LongBuffer dest = getByteBuffer().asLongBuffer();
389 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
390 dest.put(src, srcElemPos, elemCount);
391 return this;
392 }
393 /** Absolute put method. Put double-elements for `elemCount` elements from `src` at `srcElemPos` into this buffer at the given element-index `destElemPos` */
394 public final ElementBuffer put(final double[] src, final int srcElemPos, final int destElemPos, final int elemCount) {
395 if( Buffers.SIZEOF_DOUBLE != elementSize ) { throw new UnsupportedOperationException("'double' type byte-size "+Buffers.SIZEOF_DOUBLE+" != elementSize "+elementSize+", "+this); }
396 if (0 > destElemPos || destElemPos + elemCount > limit() || 0 > elemCount ||
397 0 > srcElemPos || srcElemPos + elemCount > src.length ) {
398 throw new IndexOutOfBoundsException("srcElemPos "+srcElemPos+", destElemPos "+destElemPos+", elemCount "+elemCount+
399 ", destLimit "+limit()+", srcLimit "+src.length+", "+this);
400 }
401 final DoubleBuffer dest = getByteBuffer().asDoubleBuffer();
402 dest.position( destElemPos ).limit( destElemPos + elemCount ); // remaining = elemCount
403 dest.put(src, srcElemPos, elemCount);
404 return this;
405 }
406
407 @Override
408 public String toString() {
409 return "ElementBuffer"+toSubString();
410 }
411}
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
Definition: Buffers.java:92
static final int SIZEOF_BYTE
Definition: Buffers.java:77
static final int SIZEOF_SHORT
Definition: Buffers.java:78
static final int SIZEOF_DOUBLE
Definition: Buffers.java:83
static final int SIZEOF_CHAR
Definition: Buffers.java:79
static final int SIZEOF_INT
Definition: Buffers.java:80
static final int SIZEOF_LONG
Definition: Buffers.java:82
static final int SIZEOF_FLOAT
Definition: Buffers.java:81
Hardware independent container holding an array of linearly aligned elements, while its getDirectBuff...
final ElementBuffer put(final int[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
final ElementBuffer put(final ByteBuffer[] srcElements, int offset, int length)
Relative bulk put method.
final ByteBuffer slice(final int offset, final int length)
Returns a slice of this instance's ByteBuffer [offset..offset+length), i.e.
final ElementBuffer put(final long[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
static ElementBuffer allocateDirect(final int elementSize, final int elemCount)
Returns a direct PointerBuffer in native order, w/o backup array.
final ByteBuffer getByteBuffer()
Returns the ByteBuffer, i.e.
final ElementBuffer put(final ByteBuffer srcElemBytes, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
final ElementBuffer put(final int destElemPos, final ByteBuffer srcElemBytes)
Absolute put method.
final ElementBuffer put(final double[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
static ElementBuffer derefPointer(final int elementSize, final ByteBuffer ptrSrc, final int ptrSrcByteOffset, final int elemCount)
static ElementBuffer wrap(final int elementSize, final ByteBuffer src)
final ElementBuffer put(final ByteBuffer srcElemBytes)
Relative put method.
final ElementBuffer put(final char[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
final ElementBuffer put(final ElementBuffer src)
static ElementBuffer derefPointer(final int elementSize, final long aptr, final int elemCount)
static ElementBuffer allocate(final int elementSize, final int elemCount)
Returns a non direct PointerBuffer in native order, having a backup array.
final ElementBuffer put(final byte[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
final ElementBuffer put(final float[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
final ElementBuffer put(final short[] src, final int srcElemPos, final int destElemPos, final int elemCount)
Absolute put method.
static ElementBuffer wrap(final int elementSize, final ByteBuffer src, final int srcByteOffset, final int elemCount)
Hardware independent container holding an array of native pointer, while its getDirectBufferAddress()...
static PointerBuffer wrap(final ByteBuffer src)
Wraps given ByteBuffer src up to it ByteBuffer#capacity()/POINTER_SIZE pointers.
final long get(final int idx)
Absolute get method.