GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
Buffers.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.lang.reflect.Method;
43import java.nio.Buffer;
44import java.nio.ByteBuffer;
45import java.nio.ByteOrder;
46import java.nio.CharBuffer;
47import java.nio.DoubleBuffer;
48import java.nio.FloatBuffer;
49import java.nio.IntBuffer;
50import java.nio.LongBuffer;
51import java.nio.ShortBuffer;
52import java.security.PrivilegedAction;
53
54import com.jogamp.common.ExceptionUtils;
55import com.jogamp.common.util.ReflectionUtil;
56import com.jogamp.common.util.SecurityUtil;
57import com.jogamp.common.util.UnsafeUtil;
58import com.jogamp.common.util.ValueConv;
59
60import jogamp.common.Debug;
61import jogamp.common.os.PlatformPropsImpl;
62
63/**
64 * Utility methods allowing easy {@link java.nio.Buffer} manipulations.
65 *
66 * @author Kenneth Russel
67 * @author Sven Gothel
68 * @author Michael Bien
69 */
70public class Buffers {
71
72 static final boolean DEBUG;
73 static {
74 DEBUG = Debug.debug("Buffers");
75 }
76
77 public static final int SIZEOF_BYTE = 1;
78 public static final int SIZEOF_SHORT = 2;
79 public static final int SIZEOF_CHAR = 2;
80 public static final int SIZEOF_INT = 4;
81 public static final int SIZEOF_FLOAT = 4;
82 public static final int SIZEOF_LONG = 8;
83 public static final int SIZEOF_DOUBLE = 8;
84
85 protected Buffers() {}
86
87 /**
88 * Allocates a new direct ByteBuffer with the specified number of
89 * elements. The returned buffer will have its byte order set to
90 * the host platform's native byte order.
91 */
92 public static ByteBuffer newDirectByteBuffer(final int numElements) {
93 return nativeOrder(ByteBuffer.allocateDirect(numElements));
94 }
95
96 public static ByteBuffer newDirectByteBuffer(final byte[] values, final int offset, final int length) {
97 return (ByteBuffer)newDirectByteBuffer(length).put(values, offset, length).rewind();
98 }
99
100 public static ByteBuffer newDirectByteBuffer(final byte[] values, final int offset) {
101 return newDirectByteBuffer(values, offset, values.length-offset);
102 }
103
104 public static ByteBuffer newDirectByteBuffer(final byte[] values) {
105 return newDirectByteBuffer(values, 0);
106 }
107
108 /**
109 * Allocates a new direct DoubleBuffer with the specified number of
110 * elements. The returned buffer will have its byte order set to
111 * the host platform's native byte order.
112 */
113 public static DoubleBuffer newDirectDoubleBuffer(final int numElements) {
114 return newDirectByteBuffer(numElements * SIZEOF_DOUBLE).asDoubleBuffer();
115 }
116
117 public static DoubleBuffer newDirectDoubleBuffer(final double[] values, final int offset, final int length) {
118 return (DoubleBuffer)newDirectDoubleBuffer(length).put(values, offset, length).rewind();
119 }
120
121 public static DoubleBuffer newDirectDoubleBuffer(final double[] values, final int offset) {
122 return newDirectDoubleBuffer(values, offset, values.length - offset);
123 }
124
125 public static DoubleBuffer newDirectDoubleBuffer(final double[] values) {
126 return newDirectDoubleBuffer(values, 0);
127 }
128
129 /**
130 * Allocates a new direct FloatBuffer with the specified number of
131 * elements. The returned buffer will have its byte order set to
132 * the host platform's native byte order.
133 */
134 public static FloatBuffer newDirectFloatBuffer(final int numElements) {
135 return newDirectByteBuffer(numElements * SIZEOF_FLOAT).asFloatBuffer();
136 }
137
138 public static FloatBuffer newDirectFloatBuffer(final float[] values, final int offset, final int length) {
139 return (FloatBuffer)newDirectFloatBuffer(length).put(values, offset, length).rewind();
140 }
141
142 public static FloatBuffer newDirectFloatBuffer(final float[] values, final int offset) {
143 return newDirectFloatBuffer(values, offset, values.length - offset);
144 }
145
146 public static FloatBuffer newDirectFloatBuffer(final float[] values) {
147 return newDirectFloatBuffer(values, 0);
148 }
149
150 /**
151 * Allocates a new direct IntBuffer with the specified number of
152 * elements. The returned buffer will have its byte order set to
153 * the host platform's native byte order.
154 */
155 public static IntBuffer newDirectIntBuffer(final int numElements) {
156 return newDirectByteBuffer(numElements * SIZEOF_INT).asIntBuffer();
157 }
158
159 public static IntBuffer newDirectIntBuffer(final int[] values, final int offset, final int length) {
160 return (IntBuffer)newDirectIntBuffer(length).put(values, offset, length).rewind();
161 }
162
163 public static IntBuffer newDirectIntBuffer(final int[] values, final int offset) {
164 return newDirectIntBuffer(values, offset, values.length - offset);
165 }
166
167 public static IntBuffer newDirectIntBuffer(final int[] values) {
168 return newDirectIntBuffer(values, 0);
169 }
170
171 /**
172 * Allocates a new direct LongBuffer with the specified number of
173 * elements. The returned buffer will have its byte order set to
174 * the host platform's native byte order.
175 */
176 public static LongBuffer newDirectLongBuffer(final int numElements) {
177 return newDirectByteBuffer(numElements * SIZEOF_LONG).asLongBuffer();
178 }
179
180 public static LongBuffer newDirectLongBuffer(final long[] values, final int offset, final int length) {
181 return (LongBuffer)newDirectLongBuffer(length).put(values, offset, length).rewind();
182 }
183
184 public static LongBuffer newDirectLongBuffer(final long[] values, final int offset) {
185 return newDirectLongBuffer(values, offset, values.length - offset);
186 }
187
188 public static LongBuffer newDirectLongBuffer(final long[] values) {
189 return newDirectLongBuffer(values, 0);
190 }
191
192 /**
193 * Allocates a new direct ShortBuffer with the specified number of
194 * elements. The returned buffer will have its byte order set to
195 * the host platform's native byte order.
196 */
197 public static ShortBuffer newDirectShortBuffer(final int numElements) {
198 return newDirectByteBuffer(numElements * SIZEOF_SHORT).asShortBuffer();
199 }
200
201 public static ShortBuffer newDirectShortBuffer(final short[] values, final int offset, final int length) {
202 return (ShortBuffer)newDirectShortBuffer(length).put(values, offset, length).rewind();
203 }
204
205 public static ShortBuffer newDirectShortBuffer(final short[] values, final int offset) {
206 return newDirectShortBuffer(values, offset, values.length - offset);
207 }
208
209 public static ShortBuffer newDirectShortBuffer(final short[] values) {
210 return newDirectShortBuffer(values, 0);
211 }
212
213 /**
214 * Allocates a new direct CharBuffer with the specified number of
215 * elements. The returned buffer will have its byte order set to
216 * the host platform's native byte order.
217 */
218 public static CharBuffer newDirectCharBuffer(final int numElements) {
219 return newDirectByteBuffer(numElements * SIZEOF_SHORT).asCharBuffer();
220 }
221
222 public static CharBuffer newDirectCharBuffer(final char[] values, final int offset, final int length) {
223 return (CharBuffer)newDirectCharBuffer(length).put(values, offset, length).rewind();
224 }
225
226 public static CharBuffer newDirectCharBuffer(final char[] values, final int offset) {
227 return newDirectCharBuffer(values, offset, values.length - offset);
228 }
229
230 public static CharBuffer newDirectCharBuffer(final char[] values) {
231 return newDirectCharBuffer(values, 0);
232 }
233
234 /**
235 * Helper routine to set a ByteBuffer to the native byte order, if
236 * that operation is supported by the underlying NIO
237 * implementation.
238 */
239 public static ByteBuffer nativeOrder(final ByteBuffer buf) {
240 return buf.order(ByteOrder.nativeOrder());
241 }
242
243 /**
244 * Returns {@link Buffer} class matching the given lower case `typeName`
245 * @param typeName lower-case type name
246 * @return matching {@link Buffer} class or `null`
247 * @see #sizeOfBufferElem(Class)
248 */
249 public static Class<? extends Buffer> typeNameToBufferClass(final String typeName) {
250 if (typeName == null) {
251 return null;
252 }
253 if( "byte".equals(typeName) ) {
254 return ByteBuffer.class;
255 } else if( "short".equals(typeName) ) {
256 return ShortBuffer.class;
257 } else if( "char".equals(typeName) ) {
258 return CharBuffer.class;
259 } else if( "int".equals(typeName) ) {
260 return IntBuffer.class;
261 } else if( "float".equals(typeName) ) {
262 return FloatBuffer.class;
263 } else if( "long".equals(typeName) ) {
264 return LongBuffer.class;
265 } else if( "double".equals(typeName) ) {
266 return DoubleBuffer.class;
267 }
268 return null;
269 }
270
271 /**
272 * Returns the size of a single element of the given buffer class in bytes
273 * or <code>0</code> if the given buffer is <code>null</code>.
274 * @see #typeNameToBufferClass(String)
275 * @see #sizeOfBufferElem(Object)
276 */
277 public static int sizeOfBufferElem(final Class<? extends Buffer> bufferClz) {
278 if (bufferClz == null) {
279 return 0;
280 }
281 if (ByteBuffer.class.isAssignableFrom(bufferClz)) {
282 return SIZEOF_BYTE;
283 } else if (ShortBuffer.class.isAssignableFrom(bufferClz)) {
284 return SIZEOF_SHORT;
285 } else if (CharBuffer.class.isAssignableFrom(bufferClz)) {
286 return SIZEOF_CHAR;
287 } else if (IntBuffer.class.isAssignableFrom(bufferClz)) {
288 return SIZEOF_INT;
289 } else if (FloatBuffer.class.isAssignableFrom(bufferClz)) {
290 return SIZEOF_FLOAT;
291 } else if (LongBuffer.class.isAssignableFrom(bufferClz)) {
292 return SIZEOF_LONG;
293 } else if (DoubleBuffer.class.isAssignableFrom(bufferClz)) {
294 return SIZEOF_DOUBLE;
295 }
296 throw new RuntimeException("Unexpected buffer type " + bufferClz.getName());
297 }
298
299 /**
300 * Returns the size of a single element of the given buffer in bytes
301 * or <code>0</code> if the given buffer is <code>null</code>.
302 * @see #sizeOfBufferElem(Class)
303 */
304 public static int sizeOfBufferElem(final Object buffer) {
305 if (buffer == null) {
306 return 0;
307 }
308 if (buffer instanceof ByteBuffer) {
309 return SIZEOF_BYTE;
310 } else if (buffer instanceof IntBuffer) {
311 return SIZEOF_INT;
312 } else if (buffer instanceof ShortBuffer) {
313 return SIZEOF_SHORT;
314 } else if (buffer instanceof FloatBuffer) {
315 return SIZEOF_FLOAT;
316 } else if (buffer instanceof DoubleBuffer) {
317 return SIZEOF_DOUBLE;
318 } else if (buffer instanceof LongBuffer) {
319 return SIZEOF_LONG;
320 } else if (buffer instanceof CharBuffer) {
321 return SIZEOF_CHAR;
322 } else if (buffer instanceof NativeBuffer) {
323 return ((NativeBuffer<?>) buffer).elementSize();
324 }
325 throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName());
326 }
327
328 /**
329 * Returns the number of remaining elements of the given anonymous <code>buffer</code>.
330 *
331 * @param buffer Anonymous <i>Buffer</i> of type {@link NativeBuffer} or a derivation of {@link Buffer}.
332 * @return If <code>buffer</code> is null, returns <code>0<code>, otherwise the remaining size in elements.
333 * @throws IllegalArgumentException if <code>buffer</code> is of invalid type.
334 */
335 public static int remainingElem(final Object buffer) throws IllegalArgumentException {
336 if (buffer == null) {
337 return 0;
338 }
339 if (buffer instanceof Buffer) {
340 return ((Buffer) buffer).remaining();
341 } else if (buffer instanceof NativeBuffer) {
342 return ((NativeBuffer<?>) buffer).remaining();
343 } else {
344 throw new IllegalArgumentException("Unsupported anonymous buffer type: "+buffer.getClass().getCanonicalName());
345 }
346 }
347
348 /**
349 * Returns the number of remaining bytes of the given anonymous <code>buffer</code>.
350 *
351 * @param buffer Anonymous <i>Buffer</i> of type {@link NativeBuffer} or a derivation of {@link Buffer}.
352 * @return If <code>buffer</code> is null, returns <code>0<code>, otherwise the remaining size in bytes.
353 * @throws IllegalArgumentException if <code>buffer</code> is of invalid type.
354 */
355 public static int remainingBytes(final Object buffer) throws IllegalArgumentException {
356 if (buffer == null) {
357 return 0;
358 }
359 final int bytesRemaining;
360 if (buffer instanceof Buffer) {
361 final int elementsRemaining = ((Buffer) buffer).remaining();
362 if (buffer instanceof ByteBuffer) {
363 bytesRemaining = elementsRemaining;
364 } else if (buffer instanceof FloatBuffer) {
365 bytesRemaining = elementsRemaining * SIZEOF_FLOAT;
366 } else if (buffer instanceof IntBuffer) {
367 bytesRemaining = elementsRemaining * SIZEOF_INT;
368 } else if (buffer instanceof ShortBuffer) {
369 bytesRemaining = elementsRemaining * SIZEOF_SHORT;
370 } else if (buffer instanceof DoubleBuffer) {
371 bytesRemaining = elementsRemaining * SIZEOF_DOUBLE;
372 } else if (buffer instanceof LongBuffer) {
373 bytesRemaining = elementsRemaining * SIZEOF_LONG;
374 } else if (buffer instanceof CharBuffer) {
375 bytesRemaining = elementsRemaining * SIZEOF_CHAR;
376 } else {
377 throw new InternalError("Unsupported Buffer type: "+buffer.getClass().getCanonicalName());
378 }
379 } else if (buffer instanceof NativeBuffer) {
380 final NativeBuffer<?> nb = (NativeBuffer<?>) buffer;
381 bytesRemaining = nb.remaining() * nb.elementSize();
382 } else {
383 throw new IllegalArgumentException("Unsupported anonymous buffer type: "+buffer.getClass().getCanonicalName());
384 }
385 return bytesRemaining;
386 }
387
388 /**
389 * Helper routine to tell whether a buffer is direct or not. Null
390 * pointers <b>are</b> considered direct.
391 */
392 public static boolean isDirect(final Object buf) {
393 if (buf == null) {
394 return true;
395 }
396 if (buf instanceof Buffer) {
397 return ((Buffer) buf).isDirect();
398 } else if (buf instanceof PointerBuffer) {
399 return ((PointerBuffer) buf).isDirect();
400 }
401 throw new IllegalArgumentException("Unexpected buffer type " + buf.getClass().getName());
402 }
403
404 /**
405 * Helper routine to get the Buffer byte offset by taking into
406 * account the Buffer position and the underlying type. This is
407 * the total offset for Direct Buffers.
408 */
409 public static int getDirectBufferByteOffset(final Object buf) {
410 if (buf == null) {
411 return 0;
412 }
413 if (buf instanceof Buffer) {
414 final int pos = ((Buffer) buf).position();
415 if (buf instanceof ByteBuffer) {
416 return pos;
417 } else if (buf instanceof FloatBuffer) {
418 return pos * SIZEOF_FLOAT;
419 } else if (buf instanceof IntBuffer) {
420 return pos * SIZEOF_INT;
421 } else if (buf instanceof ShortBuffer) {
422 return pos * SIZEOF_SHORT;
423 } else if (buf instanceof DoubleBuffer) {
424 return pos * SIZEOF_DOUBLE;
425 } else if (buf instanceof LongBuffer) {
426 return pos * SIZEOF_LONG;
427 } else if (buf instanceof CharBuffer) {
428 return pos * SIZEOF_CHAR;
429 }
430 } else if (buf instanceof NativeBuffer) {
431 final NativeBuffer<?> nb = (NativeBuffer<?>) buf;
432 return nb.position() * nb.elementSize() ;
433 }
434
435 throw new IllegalArgumentException("Disallowed array backing store type in buffer " + buf.getClass().getName());
436 }
437
438 /**
439 * Helper routine to return the array backing store reference from
440 * a Buffer object.
441 * @throws UnsupportedOperationException if the passed Object does not have an array backing store
442 * @throws IllegalArgumentException if the passed Object is neither of type {@link java.nio.Buffer} or {@link NativeBuffer}.
443 */
444 public static Object getArray(final Object buf) throws UnsupportedOperationException, IllegalArgumentException {
445 if (buf == null) {
446 return null;
447 }
448 if (buf instanceof Buffer) {
449 return ((Buffer) buf).array();
450 } else if (buf instanceof NativeBuffer) {
451 return ((NativeBuffer<?>) buf).array();
452 }
453
454 throw new IllegalArgumentException("Disallowed array backing store type in buffer " + buf.getClass().getName());
455 }
456
457 /**
458 * Helper routine to get the full byte offset from the beginning of
459 * the array that is the storage for the indirect Buffer
460 * object. The array offset also includes the position offset
461 * within the buffer, in addition to any array offset.
462 */
463 public static int getIndirectBufferByteOffset(final Object buf) {
464 if (buf == null) {
465 return 0;
466 }
467 if (buf instanceof Buffer) {
468 final int pos = ((Buffer) buf).position();
469 if (buf instanceof ByteBuffer) {
470 return (((ByteBuffer) buf).arrayOffset() + pos);
471 } else if (buf instanceof FloatBuffer) {
472 return (SIZEOF_FLOAT * (((FloatBuffer) buf).arrayOffset() + pos));
473 } else if (buf instanceof IntBuffer) {
474 return (SIZEOF_INT * (((IntBuffer) buf).arrayOffset() + pos));
475 } else if (buf instanceof ShortBuffer) {
476 return (SIZEOF_SHORT * (((ShortBuffer) buf).arrayOffset() + pos));
477 } else if (buf instanceof DoubleBuffer) {
478 return (SIZEOF_DOUBLE * (((DoubleBuffer) buf).arrayOffset() + pos));
479 } else if (buf instanceof LongBuffer) {
480 return (SIZEOF_LONG * (((LongBuffer) buf).arrayOffset() + pos));
481 } else if (buf instanceof CharBuffer) {
482 return (SIZEOF_CHAR * (((CharBuffer) buf).arrayOffset() + pos));
483 }
484 } else if (buf instanceof NativeBuffer) {
485 final NativeBuffer<?> nb = (NativeBuffer<?>) buf;
486 return nb.elementSize() * ( nb.arrayOffset() + nb.position() );
487 }
488
489 throw new IllegalArgumentException("Unknown buffer type " + buf.getClass().getName());
490 }
491
492
493 //----------------------------------------------------------------------
494 // Slice routines (mapping buffer to typed-buffer w/o copy)
495 //
496 /**
497 * Calls slice on the specified buffer while maintaining the byteorder.
498 * @see #slice(java.nio.Buffer, int, int)
499 */
500 @SuppressWarnings("unchecked")
501 public static <B extends Buffer> B slice(final B buffer) {
502 if (buffer instanceof ByteBuffer) {
503 final ByteBuffer bb = (ByteBuffer) buffer;
504 return (B) bb.slice().order(bb.order()); // slice and duplicate may change byte order
505 } else if (buffer instanceof IntBuffer) {
506 return (B) ((IntBuffer) buffer).slice();
507 } else if (buffer instanceof ShortBuffer) {
508 return (B) ((ShortBuffer) buffer).slice();
509 } else if (buffer instanceof FloatBuffer) {
510 return (B) ((FloatBuffer) buffer).slice();
511 } else if (buffer instanceof DoubleBuffer) {
512 return (B) ((DoubleBuffer) buffer).slice();
513 } else if (buffer instanceof LongBuffer) {
514 return (B) ((LongBuffer) buffer).slice();
515 } else if (buffer instanceof CharBuffer) {
516 return (B) ((CharBuffer) buffer).slice();
517 }
518 throw new IllegalArgumentException("unexpected buffer type: " + buffer.getClass());
519 }
520
521 /**
522 * Slices the specified buffer with offset as position and offset+size as limit
523 * while maintaining the byteorder.
524 * Concurrency warning: this method changes the buffers position and limit but
525 * will restore it before return.
526 */
527 public static <B extends Buffer> B slice(final B buffer, final int offset, final int size) {
528 final int pos = buffer.position();
529 final int limit = buffer.limit();
530
531 B slice = null;
532 try {
533 buffer.position(offset).limit(offset+size);
534 slice = slice(buffer);
535 } finally {
536 buffer.position(pos).limit(limit);
537 }
538
539 return slice;
540 }
541
542 /**
543 * Slices a ByteBuffer <i>or</i> a FloatBuffer to a FloatBuffer
544 * at the given `elementStartPos` with the given `elementCount` in float-space.
545 * <p>
546 * The returned sliced buffer's start position is zero.
547 * </p>
548 * <p>
549 * The returned sliced buffer is {@link FloatBuffer#mark() marked} at it's {@link FloatBuffer#position() start position}. Hence
550 * {@link FloatBuffer#reset()} will rewind it to start after applying relative operations like {@link FloatBuffer#get()}.
551 * </p>
552 * <p>
553 * Using a ByteBuffer as the source guarantees
554 * keeping the source native order programmatically.
555 * This works around <a href="http://code.google.com/p/android/issues/detail?id=16434">Honeycomb / Android 3.0 Issue 16434</a>.
556 * This bug is resolved at least in Android 3.2.
557 * </p>
558 *
559 * @param buf source Buffer, maybe ByteBuffer (recommended) or FloatBuffer.
560 * Buffer's position is ignored and `elementStartPos` is being used.
561 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_FLOAT}
562 * @param elementCount element count for element of size {@link Buffers#SIZEOF_FLOAT}
563 * @return FloatBuffer w/ native byte order as given ByteBuffer
564 */
565 public static final FloatBuffer slice2Float(final Buffer buf, final int elementStartPos, final int elementCount) {
566 if( null == buf ) {
567 throw new IllegalArgumentException("Buffer is null");
568 }
569 final int pos = buf.position();
570 final int limit = buf.limit();
571 final FloatBuffer res;
572 try {
573 if(buf instanceof ByteBuffer) {
574 final ByteBuffer bb = (ByteBuffer) buf;
575 bb.position( elementStartPos * Buffers.SIZEOF_FLOAT );
576 bb.limit( (elementStartPos + elementCount) * Buffers.SIZEOF_FLOAT );
577 res = bb.slice().order(bb.order()).asFloatBuffer(); // slice and duplicate may change byte order
578 } else if(buf instanceof FloatBuffer) {
579 final FloatBuffer fb = (FloatBuffer) buf;
580 fb.position( elementStartPos );
581 fb.limit( elementStartPos + elementCount );
582 res = fb.slice(); // slice and duplicate may change byte order
583 } else {
584 throw new IllegalArgumentException("Buffer not ByteBuffer, nor FloarBuffer, nor backing array given");
585 }
586 } finally {
587 buf.position(pos).limit(limit);
588 }
589 res.mark();
590 return res;
591 }
592 /**
593 * Slices a primitive float backing array to a FloatBuffer at the given `elementStartPos` with the given `elementCount`
594 * in float-space by {@link FloatBuffer#wrap(float[], int, int) wrapping} the backing array.
595 * <p>
596 * Due to {@link FloatBuffer#wrap(float[], int, int) wrapping} the backing array,
597 * the returned sliced buffer's {@link FloatBuffer#position() start position} equals
598 * the given <code>floatStartPos</code> within the given backing array
599 * while it's {@link FloatBuffer#arrayOffset() array-offset} is zero.
600 * This has the advantage of being able to dismiss the {@link FloatBuffer#arrayOffset() array-offset}
601 * in user code, while only being required to consider it's {@link FloatBuffer#position() position}.
602 * </p>
603 * <p>
604 * The returned sliced buffer is {@link FloatBuffer#mark() marked} at it's {@link FloatBuffer#position() start position}. Hence
605 * {@link FloatBuffer#reset()} will rewind it to start after applying relative operations like {@link FloatBuffer#get()}.
606 * </p>
607 *
608 * @param backing source float array
609 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_FLOAT}
610 * @param elementCount element count for element of size {@link Buffers#SIZEOF_FLOAT}
611 * @return FloatBuffer w/ native byte order as given ByteBuffer
612 */
613 public static final FloatBuffer slice2Float(final float[] backing, final int elementStartPos, final int elementCount) {
614 return (FloatBuffer) FloatBuffer.wrap(backing, elementStartPos, elementCount).mark();
615 }
616
617 /**
618 * Slices a ByteBuffer <i>or</i> a ShortBuffer to a ShortBuffer
619 * at the given `elementStartPos` with the given `elementCount` in short-space.
620 * <p>
621 * The returned sliced buffer's start position is zero.
622 * </p>
623 * <p>
624 * See {@link #slice2Float(Buffer, int, int)} for details.
625 * </p>
626 *
627 * @param buf source Buffer, maybe ByteBuffer (recommended) or ShortBuffer.
628 * Buffer's position is ignored and `elementStartPos` is being used.
629 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_SHORT}
630 * @param elementCount element count for element of size {@link Buffers#SIZEOF_SHORT}
631 * @return ShortBuffer w/ native byte order as given ByteBuffer
632 * @see #slice2Float(Buffer, int, int)
633 */
634 public static final ShortBuffer slice2Short(final Buffer buf, final int elementStartPos, final int elementCount) {
635 if( null == buf ) {
636 throw new IllegalArgumentException("Buffer is null");
637 }
638 final int pos = buf.position();
639 final int limit = buf.limit();
640 final ShortBuffer res;
641 try {
642 if(buf instanceof ByteBuffer) {
643 final ByteBuffer bb = (ByteBuffer) buf;
644 bb.position( elementStartPos * Buffers.SIZEOF_SHORT );
645 bb.limit( (elementStartPos + elementCount) * Buffers.SIZEOF_SHORT );
646 res = bb.slice().order(bb.order()).asShortBuffer(); // slice and duplicate may change byte order
647 } else if(buf instanceof ShortBuffer) {
648 final ShortBuffer fb = (ShortBuffer) buf;
649 fb.position( elementStartPos );
650 fb.limit( elementStartPos + elementCount );
651 res = fb.slice(); // slice and duplicate may change byte order
652 } else {
653 throw new IllegalArgumentException("Buffer not ByteBuffer, nor ShortBuffer");
654 }
655 } finally {
656 buf.position(pos).limit(limit);
657 }
658 res.mark();
659 return res;
660 }
661 /**
662 * Slices a primitive float backing array to a ShortBuffer at the given `elementStartPos` with the given `elementCount`
663 * in short-space by {@link ShortBuffer#wrap(float[], int, int) wrapping} the backing array.
664 * <p>
665 * See {@link #slice2Float(float[], int, int)} for details.
666 * </p>
667 * @param backing source float array
668 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_SHORT}
669 * @param elementCount element count for element of size {@link Buffers#SIZEOF_SHORT}
670 * @return ShortBuffer w/ native byte order as given ByteBuffer
671 * @see #slice2Float(float[], int, int)
672 */
673 public static final ShortBuffer slice2Short(final short[] backing, final int elementStartPos, final int elementCount) {
674 return (ShortBuffer) ShortBuffer.wrap(backing, elementStartPos, elementCount).mark();
675 }
676
677 /**
678 * Slices a ByteBuffer <i>or</i> a CharBuffer to a CharBuffer
679 * at the given `elementStartPos` with the given `elementCount` in short-space.
680 * <p>
681 * The returned sliced buffer's start position is zero.
682 * </p>
683 * <p>
684 * See {@link #slice2Float(Buffer, int, int)} for details.
685 * </p>
686 *
687 * @param buf source Buffer, maybe ByteBuffer (recommended) or CharBuffer.
688 * Buffer's position is ignored and `elementStartPos` is being used.
689 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_CHAR}
690 * @param elementCount element count for element of size {@link Buffers#SIZEOF_CHAR}
691 * @return CharBuffer w/ native byte order as given ByteBuffer
692 * @see #slice2Float(Buffer, int, int)
693 */
694 public static final CharBuffer slice2Char(final Buffer buf, final int elementStartPos, final int elementCount) {
695 if( null == buf ) {
696 throw new IllegalArgumentException("Buffer is null");
697 }
698 final int pos = buf.position();
699 final int limit = buf.limit();
700 final CharBuffer res;
701 try {
702 if(buf instanceof ByteBuffer) {
703 final ByteBuffer bb = (ByteBuffer) buf;
704 bb.position( elementStartPos * Buffers.SIZEOF_CHAR );
705 bb.limit( (elementStartPos + elementCount) * Buffers.SIZEOF_CHAR );
706 res = bb.slice().order(bb.order()).asCharBuffer(); // slice and duplicate may change byte order
707 } else if(buf instanceof CharBuffer) {
708 final CharBuffer fb = (CharBuffer) buf;
709 fb.position( elementStartPos );
710 fb.limit( elementStartPos + elementCount );
711 res = fb.slice(); // slice and duplicate may change byte order
712 } else {
713 throw new IllegalArgumentException("Buffer not ByteBuffer, nor CharBuffer");
714 }
715 } finally {
716 buf.position(pos).limit(limit);
717 }
718 res.mark();
719 return res;
720 }
721 /**
722 * Slices a primitive float backing array to a CharBuffer at the given `elementStartPos` with the given `elementCount`
723 * in short-space by {@link CharBuffer#wrap(float[], int, int) wrapping} the backing array.
724 * <p>
725 * See {@link #slice2Float(float[], int, int)} for details.
726 * </p>
727 * @param backing source float array
728 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_CHAR}
729 * @param elementCount element count for element of size {@link Buffers#SIZEOF_CHAR}
730 * @return CharBuffer w/ native byte order as given ByteBuffer
731 * @see #slice2Float(float[], int, int)
732 */
733 public static final CharBuffer slice2Char(final char[] backing, final int elementStartPos, final int elementCount) {
734 return (CharBuffer) CharBuffer.wrap(backing, elementStartPos, elementCount).mark();
735 }
736
737 /**
738 * Slices a ByteBuffer <i>or</i> a IntBuffer to a IntBuffer
739 * at the given `elementStartPos` with the given `elementCount` in int-space.
740 * <p>
741 * The returned sliced buffer's start position is zero.
742 * </p>
743 * <p>
744 * See {@link #slice2Float(Buffer, int, int)} for details.
745 * </p>
746 *
747 * @param buf source Buffer, maybe ByteBuffer (recommended) or IntBuffer.
748 * Buffer's position is ignored and `elementStartPos` is being used.
749 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_INT}
750 * @param elementCount element count for element of size {@link Buffers#SIZEOF_INT}
751 * @return IntBuffer w/ native byte order as given ByteBuffer
752 * @see #slice2Float(Buffer, int, int)
753 */
754 public static final IntBuffer slice2Int(final Buffer buf, final int elementStartPos, final int elementCount) {
755 if( null == buf ) {
756 throw new IllegalArgumentException("Buffer is null");
757 }
758 final int pos = buf.position();
759 final int limit = buf.limit();
760 final IntBuffer res;
761 try {
762 if(buf instanceof ByteBuffer) {
763 final ByteBuffer bb = (ByteBuffer) buf;
764 bb.position( elementStartPos * Buffers.SIZEOF_INT );
765 bb.limit( (elementStartPos + elementCount) * Buffers.SIZEOF_INT );
766 res = bb.slice().order(bb.order()).asIntBuffer(); // slice and duplicate may change byte order
767 } else if(buf instanceof IntBuffer) {
768 final IntBuffer fb = (IntBuffer) buf;
769 fb.position( elementStartPos );
770 fb.limit( elementStartPos + elementCount );
771 res = fb.slice(); // slice and duplicate may change byte order
772 } else {
773 throw new IllegalArgumentException("Buffer not ByteBuffer, nor IntBuffer");
774 }
775 } finally {
776 buf.position(pos).limit(limit);
777 }
778 res.mark();
779 return res;
780 }
781 /**
782 * Slices a primitive float backing array to a IntBuffer at the given `elementStartPos` with the given `elementCount`
783 * in int-space by {@link IntBuffer#wrap(float[], int, int) wrapping} the backing array.
784 * <p>
785 * See {@link #slice2Float(float[], int, int)} for details.
786 * </p>
787 * @param backing source float array
788 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_INT}
789 * @param elementCount element count for element of size {@link Buffers#SIZEOF_INT}
790 * @return IntBuffer w/ native byte order as given ByteBuffer
791 * @see #slice2Float(float[], int, int)
792 */
793 public static final IntBuffer slice2Int(final int[] backing, final int elementStartPos, final int elementCount) {
794 return (IntBuffer) IntBuffer.wrap(backing, elementStartPos, elementCount).mark();
795 }
796
797 /**
798 * Slices a ByteBuffer <i>or</i> a LongBuffer to a LongBuffer
799 * at the given `elementStartPos` with the given `elementCount` in long-space.
800 * <p>
801 * The returned sliced buffer's start position is zero.
802 * </p>
803 * <p>
804 * See {@link #slice2Float(Buffer, int, int)} for details.
805 * </p>
806 *
807 * @param buf source Buffer, maybe ByteBuffer (recommended) or LongBuffer.
808 * Buffer's position is ignored and `elementStartPos` is being used.
809 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_LONG}
810 * @param elementCount element count for element of size {@link Buffers#SIZEOF_LONG}
811 * @return LongBuffer w/ native byte order as given ByteBuffer
812 * @see #slice2Float(Buffer, int, int)
813 */
814 public static final LongBuffer slice2Long(final Buffer buf, final int elementStartPos, final int elementCount) {
815 if( null == buf ) {
816 throw new IllegalArgumentException("Buffer is null");
817 }
818 final int pos = buf.position();
819 final int limit = buf.limit();
820 final LongBuffer res;
821 try {
822 if(buf instanceof ByteBuffer) {
823 final ByteBuffer bb = (ByteBuffer) buf;
824 bb.position( elementStartPos * Buffers.SIZEOF_LONG );
825 bb.limit( (elementStartPos + elementCount) * Buffers.SIZEOF_LONG );
826 res = bb.slice().order(bb.order()).asLongBuffer(); // slice and duplicate may change byte order
827 } else if(buf instanceof LongBuffer) {
828 final LongBuffer fb = (LongBuffer) buf;
829 fb.position( elementStartPos );
830 fb.limit( elementStartPos + elementCount );
831 res = fb.slice(); // slice and duplicate may change byte order
832 } else {
833 throw new IllegalArgumentException("Buffer not ByteBuffer, nor LongBuffer");
834 }
835 } finally {
836 buf.position(pos).limit(limit);
837 }
838 res.mark();
839 return res;
840 }
841 /**
842 * Slices a primitive float backing array to a LongBuffer at the given `elementStartPos` with the given `elementCount`
843 * in long-space by {@link LongBuffer#wrap(float[], int, int) wrapping} the backing array.
844 * <p>
845 * See {@link #slice2Float(float[], int, int)} for details.
846 * </p>
847 * @param backing source float array
848 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_LONG}
849 * @param elementCount element count for element of size {@link Buffers#SIZEOF_LONG}
850 * @return LongBuffer w/ native byte order as given ByteBuffer
851 * @see #slice2Float(float[], int, int)
852 */
853 public static final LongBuffer slice2Long(final long[] backing, final int elementStartPos, final int elementCount) {
854 return (LongBuffer) LongBuffer.wrap(backing, elementStartPos, elementCount).mark();
855 }
856
857 /**
858 * Slices a ByteBuffer <i>or</i> a DoubleBuffer to a DoubleBuffer
859 * at the given `elementStartPos` with the given `elementCount` in double-space.
860 * <p>
861 * The returned sliced buffer's start position is zero.
862 * </p>
863 * <p>
864 * See {@link #slice2Float(Buffer, int, int)} for details.
865 * </p>
866 *
867 * @param buf source Buffer, maybe ByteBuffer (recommended) or DoubleBuffer.
868 * Buffer's position is ignored and `elementStartPos` is being used.
869 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_DOUBLE}
870 * @param elementCount element count for element of size {@link Buffers#SIZEOF_DOUBLE}
871 * @return DoubleBuffer w/ native byte order as given ByteBuffer
872 * @see #slice2Float(Buffer, int, int)
873 */
874 public static final DoubleBuffer slice2Double(final Buffer buf, final int elementStartPos, final int elementCount) {
875 if( null == buf ) {
876 throw new IllegalArgumentException("Buffer is null");
877 }
878 final int pos = buf.position();
879 final int limit = buf.limit();
880 final DoubleBuffer res;
881 try {
882 if(buf instanceof ByteBuffer) {
883 final ByteBuffer bb = (ByteBuffer) buf;
884 bb.position( elementStartPos * Buffers.SIZEOF_DOUBLE );
885 bb.limit( (elementStartPos + elementCount) * Buffers.SIZEOF_DOUBLE );
886 res = bb.slice().order(bb.order()).asDoubleBuffer(); // slice and duplicate may change byte order
887 } else if(buf instanceof DoubleBuffer) {
888 final DoubleBuffer fb = (DoubleBuffer) buf;
889 fb.position( elementStartPos );
890 fb.limit( elementStartPos + elementCount );
891 res = fb.slice(); // slice and duplicate may change byte order
892 } else {
893 throw new IllegalArgumentException("Buffer not ByteBuffer, nor DoubleBuffer");
894 }
895 } finally {
896 buf.position(pos).limit(limit);
897 }
898 res.mark();
899 return res;
900 }
901 /**
902 * Slices a primitive float backing array to a DoubleBuffer at the given `elementStartPos` with the given `elementCount`
903 * in double-space by {@link DoubleBuffer#wrap(float[], int, int) wrapping} the backing array.
904 * <p>
905 * See {@link #slice2Float(float[], int, int)} for details.
906 * </p>
907 * @param backing source float array
908 * @param elementStartPos element start position w/ element of size {@link Buffers#SIZEOF_DOUBLE}
909 * @param elementCount element count for element of size {@link Buffers#SIZEOF_DOUBLE}
910 * @return DoubleBuffer w/ native byte order as given ByteBuffer
911 * @see #slice2Float(float[], int, int)
912 */
913 public static final DoubleBuffer slice2Double(final double[] backing, final int elementStartPos, final int elementCount) {
914 return (DoubleBuffer) DoubleBuffer.wrap(backing, elementStartPos, elementCount).mark();
915 }
916 //----------------------------------------------------------------------
917 // Copy routines (type-to-type)
918 //
919 /**
920 * Copies the <i>remaining</i> elements (as defined by
921 * <code>limit() - position()</code>) in the passed ByteBuffer into
922 * a newly-allocated direct ByteBuffer. The returned buffer will
923 * have its byte order set to the host platform's native byte
924 * order. The position of the newly-allocated buffer will be zero,
925 * and the position of the passed buffer is unchanged.
926 */
927 public static ByteBuffer copyByteBuffer(final ByteBuffer orig) {
928 final int op0 = orig.position();
929 final ByteBuffer dest = newDirectByteBuffer(orig.remaining());
930 dest.put(orig);
931 dest.rewind();
932 orig.position(op0);
933 return dest;
934 }
935
936 /**
937 * Copies the <i>remaining</i> elements (as defined by
938 * <code>limit() - position()</code>) in the passed FloatBuffer
939 * into a newly-allocated direct FloatBuffer. The returned buffer
940 * will have its byte order set to the host platform's native byte
941 * order. The position of the newly-allocated buffer will be zero,
942 * and the position of the passed buffer is unchanged.
943 */
944 public static FloatBuffer copyFloatBuffer(final FloatBuffer orig) {
945 return copyFloatBufferAsByteBuffer(orig).asFloatBuffer();
946 }
947
948 /**
949 * Copies the <i>remaining</i> elements (as defined by
950 * <code>limit() - position()</code>) in the passed IntBuffer
951 * into a newly-allocated direct IntBuffer. The returned buffer
952 * will have its byte order set to the host platform's native byte
953 * order. The position of the newly-allocated buffer will be zero,
954 * and the position of the passed buffer is unchanged.
955 */
956 public static IntBuffer copyIntBuffer(final IntBuffer orig) {
957 return copyIntBufferAsByteBuffer(orig).asIntBuffer();
958 }
959
960 /**
961 * Copies the <i>remaining</i> elements (as defined by
962 * <code>limit() - position()</code>) in the passed ShortBuffer
963 * into a newly-allocated direct ShortBuffer. The returned buffer
964 * will have its byte order set to the host platform's native byte
965 * order. The position of the newly-allocated buffer will be zero,
966 * and the position of the passed buffer is unchanged.
967 */
968 public static ShortBuffer copyShortBuffer(final ShortBuffer orig) {
969 return copyShortBufferAsByteBuffer(orig).asShortBuffer();
970 }
971
972 //----------------------------------------------------------------------
973 // Copy routines (type-to-ByteBuffer)
974 //
975 /**
976 * Copies the <i>remaining</i> elements (as defined by
977 * <code>limit() - position()</code>) in the passed FloatBuffer
978 * into a newly-allocated direct ByteBuffer. The returned buffer
979 * will have its byte order set to the host platform's native byte
980 * order. The position of the newly-allocated buffer will be zero,
981 * and the position of the passed buffer is unchanged.
982 */
983 public static ByteBuffer copyFloatBufferAsByteBuffer(final FloatBuffer orig) {
984 final int op0 = orig.position();
985 final ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_FLOAT);
986 dest.asFloatBuffer().put(orig);
987 dest.rewind();
988 orig.position(op0);
989 return dest;
990 }
991
992 /**
993 * Copies the <i>remaining</i> elements (as defined by
994 * <code>limit() - position()</code>) in the passed IntBuffer into
995 * a newly-allocated direct ByteBuffer. The returned buffer will
996 * have its byte order set to the host platform's native byte
997 * order. The position of the newly-allocated buffer will be zero,
998 * and the position of the passed buffer is unchanged.
999 */
1000 public static ByteBuffer copyIntBufferAsByteBuffer(final IntBuffer orig) {
1001 final int op0 = orig.position();
1002 final ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_INT);
1003 dest.asIntBuffer().put(orig);
1004 dest.rewind();
1005 orig.position(op0);
1006 return dest;
1007 }
1008
1009 /**
1010 * Copies the <i>remaining</i> elements (as defined by
1011 * <code>limit() - position()</code>) in the passed ShortBuffer
1012 * into a newly-allocated direct ByteBuffer. The returned buffer
1013 * will have its byte order set to the host platform's native byte
1014 * order. The position of the newly-allocated buffer will be zero,
1015 * and the position of the passed buffer is unchanged.
1016 */
1017 public static ByteBuffer copyShortBufferAsByteBuffer(final ShortBuffer orig) {
1018 final int op0 = orig.position();
1019 final ByteBuffer dest = newDirectByteBuffer(orig.remaining() * SIZEOF_SHORT);
1020 dest.asShortBuffer().put(orig);
1021 dest.rewind();
1022 orig.position(op0);
1023 return dest;
1024 }
1025
1026 //----------------------------------------------------------------------
1027 // Conversion routines
1028 //
1029
1030 /**
1031 * @param source the source array
1032 * @param soffset the offset
1033 * @param dest the target array, if null, a new array is being created with size len.
1034 * @param doffset the offset in the dest array
1035 * @param len the payload of elements to be copied, if <code>len < 0</code> then <code>len = source.length - soffset</code>
1036 * @return the passed or newly created target array
1037 */
1038 public static float[] getFloatArray(final double[] source, final int soffset, float[] dest, int doffset, int len) {
1039 if(0>len) {
1040 len = source.length - soffset;
1041 }
1042 if(len > source.length - soffset) {
1043 throw new IllegalArgumentException("payload ("+len+") greater than remaining source bytes [len "+source.length+", offset "+soffset+"]");
1044 }
1045 if(null==dest) {
1046 dest = new float[len];
1047 doffset = 0;
1048 }
1049 if(len > dest.length - doffset) {
1050 throw new IllegalArgumentException("payload ("+len+") greater than remaining dest bytes [len "+dest.length+", offset "+doffset+"]");
1051 }
1052 for(int i=0; i<len; i++) {
1053 dest[doffset+i] = (float) source[soffset+i];
1054 }
1055 return dest;
1056 }
1057
1058 /**
1059 * No rewind or repositioning is performed.
1060 * @param source the source buffer, which elements from it's current position and it's limit are being copied
1061 * @param dest the target buffer, if null, a new buffer is being created with size </code>source.remaining()</code>
1062 * @return the passed or newly created target buffer
1063 */
1064 public static FloatBuffer getFloatBuffer(final DoubleBuffer source, FloatBuffer dest) {
1065 if(null == dest) {
1066 dest = newDirectFloatBuffer(source.remaining());
1067 }
1068 if( dest.remaining() < source.remaining() ) {
1069 throw new IllegalArgumentException("payload ("+source.remaining()+") is greater than remaining dest bytes: "+dest.remaining());
1070 }
1071 while (source.hasRemaining()) {
1072 dest.put((float) source.get());
1073 }
1074 return dest;
1075 }
1076
1077 /**
1078 * @param source the source array
1079 * @param soffset the offset
1080 * @param dest the target array, if null, a new array is being created with size len.
1081 * @param doffset the offset in the dest array
1082 * @param len the payload of elements to be copied, if <code>len < 0</code> then <code>len = source.length - soffset</code>
1083 * @return the passed or newly created target array
1084 */
1085 public static double[] getDoubleArray(final float[] source, final int soffset, double[] dest, int doffset, int len) {
1086 if(0>len) {
1087 len = source.length - soffset;
1088 }
1089 if(len > source.length - soffset) {
1090 throw new IllegalArgumentException("payload ("+len+") greater than remaining source bytes [len "+source.length+", offset "+soffset+"]");
1091 }
1092 if(null==dest) {
1093 dest = new double[len];
1094 doffset = 0;
1095 }
1096 if(len > dest.length - doffset) {
1097 throw new IllegalArgumentException("payload ("+len+") greater than remaining dest bytes [len "+dest.length+", offset "+doffset+"]");
1098 }
1099 for(int i=0; i<len; i++) {
1100 dest[doffset+i] = source[soffset+i];
1101 }
1102 return dest;
1103 }
1104
1105 /**
1106 * No rewind or repositioning is performed.
1107 * @param source the source buffer, which elements from it's current position and it's limit are being copied
1108 * @param dest the target buffer, if null, a new buffer is being created with size </code>source.remaining()</code>
1109 * @return the passed or newly created target buffer
1110 */
1111 public static DoubleBuffer getDoubleBuffer(final FloatBuffer source, DoubleBuffer dest) {
1112 if(null == dest) {
1113 dest = newDirectDoubleBuffer(source.remaining());
1114 }
1115 if( dest.remaining() < source.remaining() ) {
1116 throw new IllegalArgumentException("payload ("+source.remaining()+") is greater than remaining dest bytes: "+dest.remaining());
1117 }
1118 while (source.hasRemaining()) {
1119 dest.put(source.get());
1120 }
1121 return dest;
1122 }
1123
1124
1125 //----------------------------------------------------------------------
1126 // Convenient put methods with generic target Buffer w/o value range conversion, i.e. normalization
1127 //
1128
1129 @SuppressWarnings("unchecked")
1130 public static <B extends Buffer> B put(final B dest, final Buffer src) {
1131 if ((dest instanceof ByteBuffer) && (src instanceof ByteBuffer)) {
1132 return (B) ((ByteBuffer) dest).put((ByteBuffer) src);
1133 } else if ((dest instanceof ShortBuffer) && (src instanceof ShortBuffer)) {
1134 return (B) ((ShortBuffer) dest).put((ShortBuffer) src);
1135 } else if ((dest instanceof IntBuffer) && (src instanceof IntBuffer)) {
1136 return (B) ((IntBuffer) dest).put((IntBuffer) src);
1137 } else if ((dest instanceof FloatBuffer) && (src instanceof FloatBuffer)) {
1138 return (B) ((FloatBuffer) dest).put((FloatBuffer) src);
1139 } else if ((dest instanceof LongBuffer) && (src instanceof LongBuffer)) {
1140 return (B) ((LongBuffer) dest).put((LongBuffer) src);
1141 } else if ((dest instanceof DoubleBuffer) && (src instanceof DoubleBuffer)) {
1142 return (B) ((DoubleBuffer) dest).put((DoubleBuffer) src);
1143 } else if ((dest instanceof CharBuffer) && (src instanceof CharBuffer)) {
1144 return (B) ((CharBuffer) dest).put((CharBuffer) src);
1145 }
1146 throw new IllegalArgumentException("Incompatible Buffer classes: dest = " + dest.getClass().getName() + ", src = " + src.getClass().getName());
1147 }
1148
1149 @SuppressWarnings("unchecked")
1150 public static <B extends Buffer> B putb(final B dest, final byte v) {
1151 if (dest instanceof ByteBuffer) {
1152 return (B) ((ByteBuffer) dest).put(v);
1153 } else if (dest instanceof ShortBuffer) {
1154 return (B) ((ShortBuffer) dest).put(v);
1155 } else if (dest instanceof IntBuffer) {
1156 return (B) ((IntBuffer) dest).put(v);
1157 } else if (dest instanceof FloatBuffer) {
1158 return (B) ((FloatBuffer) dest).put(v);
1159 } else if (dest instanceof LongBuffer) {
1160 return (B) ((LongBuffer) dest).put(v);
1161 } else if (dest instanceof DoubleBuffer) {
1162 return (B) ((DoubleBuffer) dest).put(v);
1163 } else if (dest instanceof CharBuffer) {
1164 return (B) ((CharBuffer) dest).put((char) v);
1165 } else {
1166 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1167 }
1168 }
1169
1170 @SuppressWarnings("unchecked")
1171 public static <B extends Buffer> B put3b(final B dest, final byte v1, final byte v2, final byte v3) {
1172 if (dest instanceof ByteBuffer) {
1173 final ByteBuffer b = (ByteBuffer) dest;
1174 b.put(v1);
1175 b.put(v2);
1176 b.put(v3);
1177 return (B)b;
1178 } else if (dest instanceof ShortBuffer) {
1179 final ShortBuffer b = (ShortBuffer) dest;
1180 b.put(v1);
1181 b.put(v2);
1182 b.put(v3);
1183 return (B)b;
1184 } else if (dest instanceof IntBuffer) {
1185 final IntBuffer b = (IntBuffer) dest;
1186 b.put(v1);
1187 b.put(v2);
1188 b.put(v3);
1189 return (B)b;
1190 } else if (dest instanceof FloatBuffer) {
1191 final FloatBuffer b = (FloatBuffer) dest;
1192 b.put(v1);
1193 b.put(v2);
1194 b.put(v3);
1195 return (B)b;
1196 } else if (dest instanceof LongBuffer) {
1197 final LongBuffer b = (LongBuffer) dest;
1198 b.put(v1);
1199 b.put(v2);
1200 b.put(v3);
1201 return (B)b;
1202 } else if (dest instanceof DoubleBuffer) {
1203 final DoubleBuffer b = (DoubleBuffer) dest;
1204 b.put(v1);
1205 b.put(v2);
1206 b.put(v3);
1207 return (B)b;
1208 } else if (dest instanceof CharBuffer) {
1209 final CharBuffer b = (CharBuffer) dest;
1210 b.put((char)v1);
1211 b.put((char)v2);
1212 b.put((char)v3);
1213 return (B)b;
1214 } else {
1215 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1216 }
1217 }
1218
1219 @SuppressWarnings("unchecked")
1220 public static <B extends Buffer> B put4b(final B dest, final byte v1, final byte v2, final byte v3, final byte v4) {
1221 if (dest instanceof ByteBuffer) {
1222 final ByteBuffer b = (ByteBuffer) dest;
1223 b.put(v1);
1224 b.put(v2);
1225 b.put(v3);
1226 b.put(v4);
1227 return (B)b;
1228 } else if (dest instanceof ShortBuffer) {
1229 final ShortBuffer b = (ShortBuffer) dest;
1230 b.put(v1);
1231 b.put(v2);
1232 b.put(v3);
1233 b.put(v4);
1234 return (B)b;
1235 } else if (dest instanceof IntBuffer) {
1236 final IntBuffer b = (IntBuffer) dest;
1237 b.put(v1);
1238 b.put(v2);
1239 b.put(v3);
1240 b.put(v4);
1241 return (B)b;
1242 } else if (dest instanceof FloatBuffer) {
1243 final FloatBuffer b = (FloatBuffer) dest;
1244 b.put(v1);
1245 b.put(v2);
1246 b.put(v3);
1247 b.put(v4);
1248 return (B)b;
1249 } else if (dest instanceof LongBuffer) {
1250 final LongBuffer b = (LongBuffer) dest;
1251 b.put(v1);
1252 b.put(v2);
1253 b.put(v3);
1254 b.put(v4);
1255 return (B)b;
1256 } else if (dest instanceof DoubleBuffer) {
1257 final DoubleBuffer b = (DoubleBuffer) dest;
1258 b.put(v1);
1259 b.put(v2);
1260 b.put(v3);
1261 b.put(v4);
1262 return (B)b;
1263 } else if (dest instanceof CharBuffer) {
1264 final CharBuffer b = (CharBuffer) dest;
1265 b.put((char)v1);
1266 b.put((char)v2);
1267 b.put((char)v3);
1268 b.put((char)v4);
1269 return (B)b;
1270 } else {
1271 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1272 }
1273 }
1274
1275 @SuppressWarnings("unchecked")
1276 public static <B extends Buffer> B putb(final B dest, final byte[] src, final int offset, final int length) {
1277 if (dest instanceof ByteBuffer) {
1278 return (B) ((ByteBuffer) dest).put(src, offset, length);
1279 } else if (dest instanceof ShortBuffer) {
1280 final ShortBuffer b = (ShortBuffer) dest;
1281 for(int i=0; i<length; ++i) {
1282 b.put(src[offset+i]);
1283 }
1284 return (B)b;
1285 } else if (dest instanceof IntBuffer) {
1286 final IntBuffer b = (IntBuffer) dest;
1287 for(int i=0; i<length; ++i) {
1288 b.put(src[offset+i]);
1289 }
1290 return (B)b;
1291 } else if (dest instanceof FloatBuffer) {
1292 final FloatBuffer b = (FloatBuffer) dest;
1293 for(int i=0; i<length; ++i) {
1294 b.put(src[offset+i]);
1295 }
1296 return (B)b;
1297 } else if (dest instanceof LongBuffer) {
1298 final LongBuffer b = (LongBuffer) dest;
1299 for(int i=0; i<length; ++i) {
1300 b.put(src[offset+i]);
1301 }
1302 return (B)b;
1303 } else if (dest instanceof DoubleBuffer) {
1304 final DoubleBuffer b = (DoubleBuffer) dest;
1305 for(int i=0; i<length; ++i) {
1306 b.put(src[offset+i]);
1307 }
1308 return (B)b;
1309 } else if (dest instanceof CharBuffer) {
1310 final CharBuffer b = (CharBuffer) dest;
1311 for(int i=0; i<length; ++i) {
1312 b.put((char) src[offset+i]);
1313 }
1314 return (B)b;
1315 } else {
1316 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1317 }
1318 }
1319
1320 @SuppressWarnings("unchecked")
1321 public static <B extends Buffer> B puts(final B dest, final short v) {
1322 if (dest instanceof ShortBuffer) {
1323 return (B) ((ShortBuffer) dest).put(v);
1324 } else if (dest instanceof IntBuffer) {
1325 return (B) ((IntBuffer) dest).put(v);
1326 } else if (dest instanceof FloatBuffer) {
1327 return (B) ((FloatBuffer) dest).put(v);
1328 } else if (dest instanceof LongBuffer) {
1329 return (B) ((LongBuffer) dest).put(v);
1330 } else if (dest instanceof DoubleBuffer) {
1331 return (B) ((DoubleBuffer) dest).put(v);
1332 } else {
1333 throw new IllegalArgumentException("Short doesn't match Buffer Class: " + dest);
1334 }
1335 }
1336
1337 @SuppressWarnings("unchecked")
1338 public static <B extends Buffer> B put3s(final B dest, final short v1, final short v2, final short v3) {
1339 if (dest instanceof ShortBuffer) {
1340 final ShortBuffer b = (ShortBuffer) dest;
1341 b.put(v1);
1342 b.put(v2);
1343 b.put(v3);
1344 return (B)b;
1345 } else if (dest instanceof IntBuffer) {
1346 final IntBuffer b = (IntBuffer) dest;
1347 b.put(v1);
1348 b.put(v2);
1349 b.put(v3);
1350 return (B)b;
1351 } else if (dest instanceof FloatBuffer) {
1352 final FloatBuffer b = (FloatBuffer) dest;
1353 b.put(v1);
1354 b.put(v2);
1355 b.put(v3);
1356 return (B)b;
1357 } else if (dest instanceof LongBuffer) {
1358 final LongBuffer b = (LongBuffer) dest;
1359 b.put(v1);
1360 b.put(v2);
1361 b.put(v3);
1362 return (B)b;
1363 } else if (dest instanceof DoubleBuffer) {
1364 final DoubleBuffer b = (DoubleBuffer) dest;
1365 b.put(v1);
1366 b.put(v2);
1367 b.put(v3);
1368 return (B)b;
1369 } else {
1370 throw new IllegalArgumentException("Short doesn't match Buffer Class: " + dest);
1371 }
1372 }
1373
1374 @SuppressWarnings("unchecked")
1375 public static <B extends Buffer> B put4s(final B dest, final short v1, final short v2, final short v3, final short v4) {
1376 if (dest instanceof ShortBuffer) {
1377 final ShortBuffer b = (ShortBuffer) dest;
1378 b.put(v1);
1379 b.put(v2);
1380 b.put(v3);
1381 b.put(v4);
1382 return (B)b;
1383 } else if (dest instanceof IntBuffer) {
1384 final IntBuffer b = (IntBuffer) dest;
1385 b.put(v1);
1386 b.put(v2);
1387 b.put(v3);
1388 b.put(v4);
1389 return (B)b;
1390 } else if (dest instanceof FloatBuffer) {
1391 final FloatBuffer b = (FloatBuffer) dest;
1392 b.put(v1);
1393 b.put(v2);
1394 b.put(v3);
1395 b.put(v4);
1396 return (B)b;
1397 } else if (dest instanceof LongBuffer) {
1398 final LongBuffer b = (LongBuffer) dest;
1399 b.put(v1);
1400 b.put(v2);
1401 b.put(v3);
1402 b.put(v4);
1403 return (B)b;
1404 } else if (dest instanceof DoubleBuffer) {
1405 final DoubleBuffer b = (DoubleBuffer) dest;
1406 b.put(v1);
1407 b.put(v2);
1408 b.put(v3);
1409 b.put(v4);
1410 return (B)b;
1411 } else {
1412 throw new IllegalArgumentException("Short doesn't match Buffer Class: " + dest);
1413 }
1414 }
1415
1416 @SuppressWarnings("unchecked")
1417 public static <B extends Buffer> B puts(final B dest, final short[] src, final int offset, final int length) {
1418 if (dest instanceof ShortBuffer) {
1419 return (B) ((ShortBuffer) dest).put(src, offset, length);
1420 } else if (dest instanceof IntBuffer) {
1421 final IntBuffer b = (IntBuffer) dest;
1422 for(int i=0; i<length; ++i) {
1423 b.put(src[offset+i]);
1424 }
1425 return (B)b;
1426 } else if (dest instanceof FloatBuffer) {
1427 final FloatBuffer b = (FloatBuffer) dest;
1428 for(int i=0; i<length; ++i) {
1429 b.put(src[offset+i]);
1430 }
1431 return (B)b;
1432 } else if (dest instanceof LongBuffer) {
1433 final LongBuffer b = (LongBuffer) dest;
1434 for(int i=0; i<length; ++i) {
1435 b.put(src[offset+i]);
1436 }
1437 return (B)b;
1438 } else if (dest instanceof DoubleBuffer) {
1439 final DoubleBuffer b = (DoubleBuffer) dest;
1440 for(int i=0; i<length; ++i) {
1441 b.put(src[offset+i]);
1442 }
1443 return (B)b;
1444 } else {
1445 throw new IllegalArgumentException("Short doesn't match Buffer Class: " + dest);
1446 }
1447 }
1448
1449 @SuppressWarnings("unchecked")
1450 public static <B extends Buffer> B puti(final B dest, final int v) {
1451 if (dest instanceof IntBuffer) {
1452 return (B) ((IntBuffer) dest).put(v);
1453 } else if (dest instanceof FloatBuffer) {
1454 return (B) ((FloatBuffer) dest).put(v);
1455 } else if (dest instanceof LongBuffer) {
1456 return (B) ((LongBuffer) dest).put(v);
1457 } else if (dest instanceof DoubleBuffer) {
1458 return (B) ((DoubleBuffer) dest).put(v);
1459 } else {
1460 throw new IllegalArgumentException("Integer doesn't match Buffer Class: " + dest);
1461 }
1462 }
1463
1464 @SuppressWarnings("unchecked")
1465 public static <B extends Buffer> B put3i(final B dest, final int v1, final int v2, final int v3) {
1466 if (dest instanceof IntBuffer) {
1467 final IntBuffer b = (IntBuffer) dest;
1468 b.put(v1);
1469 b.put(v2);
1470 b.put(v3);
1471 return (B)b;
1472 } else if (dest instanceof FloatBuffer) {
1473 final FloatBuffer b = (FloatBuffer) dest;
1474 b.put(v1);
1475 b.put(v2);
1476 b.put(v3);
1477 return (B)b;
1478 } else if (dest instanceof LongBuffer) {
1479 final LongBuffer b = (LongBuffer) dest;
1480 b.put(v1);
1481 b.put(v2);
1482 b.put(v3);
1483 return (B)b;
1484 } else if (dest instanceof DoubleBuffer) {
1485 final DoubleBuffer b = (DoubleBuffer) dest;
1486 b.put(v1);
1487 b.put(v2);
1488 b.put(v3);
1489 return (B)b;
1490 } else {
1491 throw new IllegalArgumentException("Integer doesn't match Buffer Class: " + dest);
1492 }
1493 }
1494
1495 @SuppressWarnings("unchecked")
1496 public static <B extends Buffer> B put4i(final B dest, final int v1, final int v2, final int v3, final int v4) {
1497 if (dest instanceof IntBuffer) {
1498 final IntBuffer b = (IntBuffer) dest;
1499 b.put(v1);
1500 b.put(v2);
1501 b.put(v3);
1502 b.put(v4);
1503 return (B)b;
1504 } else if (dest instanceof FloatBuffer) {
1505 final FloatBuffer b = (FloatBuffer) dest;
1506 b.put(v1);
1507 b.put(v2);
1508 b.put(v3);
1509 b.put(v4);
1510 return (B)b;
1511 } else if (dest instanceof LongBuffer) {
1512 final LongBuffer b = (LongBuffer) dest;
1513 b.put(v1);
1514 b.put(v2);
1515 b.put(v3);
1516 b.put(v4);
1517 return (B)b;
1518 } else if (dest instanceof DoubleBuffer) {
1519 final DoubleBuffer b = (DoubleBuffer) dest;
1520 b.put(v1);
1521 b.put(v2);
1522 b.put(v3);
1523 b.put(v4);
1524 return (B)b;
1525 } else {
1526 throw new IllegalArgumentException("Integer doesn't match Buffer Class: " + dest);
1527 }
1528 }
1529
1530 @SuppressWarnings("unchecked")
1531 public static <B extends Buffer> B puti(final B dest, final int[] src, final int offset, final int length) {
1532 if (dest instanceof IntBuffer) {
1533 return (B) ((IntBuffer) dest).put(src, offset, length);
1534 } else if (dest instanceof FloatBuffer) {
1535 final FloatBuffer b = (FloatBuffer) dest;
1536 for(int i=0; i<length; ++i) {
1537 b.put(src[offset+i]);
1538 }
1539 return (B)b;
1540 } else if (dest instanceof LongBuffer) {
1541 final LongBuffer b = (LongBuffer) dest;
1542 for(int i=0; i<length; ++i) {
1543 b.put(src[offset+i]);
1544 }
1545 return (B)b;
1546 } else if (dest instanceof DoubleBuffer) {
1547 final DoubleBuffer b = (DoubleBuffer) dest;
1548 for(int i=0; i<length; ++i) {
1549 b.put(src[offset+i]);
1550 }
1551 return (B)b;
1552 } else {
1553 throw new IllegalArgumentException("Integer doesn't match Buffer Class: " + dest);
1554 }
1555 }
1556
1557 @SuppressWarnings("unchecked")
1558 public static <B extends Buffer> B putf(final B dest, final float v) {
1559 if (dest instanceof FloatBuffer) {
1560 return (B) ((FloatBuffer) dest).put(v);
1561 } else if (dest instanceof DoubleBuffer) {
1562 return (B) ((DoubleBuffer) dest).put(v);
1563/* TODO FixedPoint required
1564 } else if (dest instanceof IntBuffer) {
1565 return (B) ((IntBuffer) dest).put(FixedPoint.toFixed(v));
1566*/
1567 } else {
1568 throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest);
1569 }
1570 }
1571
1572 @SuppressWarnings("unchecked")
1573 public static <B extends Buffer> B put3f(final B dest, final float v1, final float v2, final float v3) {
1574 if (dest instanceof FloatBuffer) {
1575 final FloatBuffer b = (FloatBuffer) dest;
1576 b.put(v1);
1577 b.put(v2);
1578 b.put(v3);
1579 return (B)b;
1580 } else if (dest instanceof DoubleBuffer) {
1581 final DoubleBuffer b = (DoubleBuffer) dest;
1582 b.put(v1);
1583 b.put(v2);
1584 b.put(v3);
1585 return (B)b;
1586 } else {
1587 throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest);
1588 }
1589 }
1590
1591 @SuppressWarnings("unchecked")
1592 public static <B extends Buffer> B put4f(final B dest, final float v1, final float v2, final float v3, final float v4) {
1593 if (dest instanceof FloatBuffer) {
1594 final FloatBuffer b = (FloatBuffer) dest;
1595 b.put(v1);
1596 b.put(v2);
1597 b.put(v3);
1598 b.put(v4);
1599 return (B)b;
1600 } else if (dest instanceof DoubleBuffer) {
1601 final DoubleBuffer b = (DoubleBuffer) dest;
1602 b.put(v1);
1603 b.put(v2);
1604 b.put(v3);
1605 b.put(v4);
1606 return (B)b;
1607 } else {
1608 throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest);
1609 }
1610 }
1611
1612 @SuppressWarnings("unchecked")
1613 public static <B extends Buffer> B putf(final B dest, final float[] src, final int offset, final int length) {
1614 if (dest instanceof FloatBuffer) {
1615 return (B) ((FloatBuffer) dest).put(src, offset, length);
1616 } else if (dest instanceof DoubleBuffer) {
1617 final DoubleBuffer b = (DoubleBuffer) dest;
1618 for(int i=0; i<length; ++i) {
1619 b.put(src[offset+i]);
1620 }
1621 return (B)b;
1622 } else {
1623 throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest);
1624 }
1625 }
1626
1627 @SuppressWarnings("unchecked")
1628 public static <B extends Buffer> B putd(final B dest, final double v) {
1629 if (dest instanceof DoubleBuffer) {
1630 return (B) ((DoubleBuffer) dest).put(v);
1631 } else if (dest instanceof FloatBuffer) {
1632 return (B) ((FloatBuffer) dest).put((float) v);
1633 } else {
1634 throw new IllegalArgumentException("Double doesn't match Buffer Class: " + dest);
1635 }
1636 }
1637
1638 @SuppressWarnings("unchecked")
1639 public static <B extends Buffer> B put3d(final B dest, final double v1, final double v2, final double v3) {
1640 if (dest instanceof DoubleBuffer) {
1641 final DoubleBuffer b = (DoubleBuffer) dest;
1642 b.put(v1);
1643 b.put(v2);
1644 b.put(v3);
1645 return (B)b;
1646 } else if (dest instanceof FloatBuffer) {
1647 final FloatBuffer b = (FloatBuffer) dest;
1648 b.put((float) v1);
1649 b.put((float) v2);
1650 b.put((float) v3);
1651 return (B)b;
1652 } else {
1653 throw new IllegalArgumentException("Double doesn't match Buffer Class: " + dest);
1654 }
1655 }
1656
1657 @SuppressWarnings("unchecked")
1658 public static <B extends Buffer> B put4d(final B dest, final double v1, final double v2, final double v3, final double v4) {
1659 if (dest instanceof DoubleBuffer) {
1660 final DoubleBuffer b = (DoubleBuffer) dest;
1661 b.put(v1);
1662 b.put(v2);
1663 b.put(v3);
1664 b.put(v4);
1665 return (B)b;
1666 } else if (dest instanceof FloatBuffer) {
1667 final FloatBuffer b = (FloatBuffer) dest;
1668 b.put((float) v1);
1669 b.put((float) v2);
1670 b.put((float) v3);
1671 b.put((float) v4);
1672 return (B)b;
1673 } else {
1674 throw new IllegalArgumentException("Double doesn't match Buffer Class: " + dest);
1675 }
1676 }
1677
1678 @SuppressWarnings("unchecked")
1679 public static <B extends Buffer> B putd(final B dest, final double[] src, final int offset, final int length) {
1680 if (dest instanceof DoubleBuffer) {
1681 return (B) ((DoubleBuffer) dest).put(src, offset, length);
1682 } else if (dest instanceof FloatBuffer) {
1683 final FloatBuffer b = (FloatBuffer) dest;
1684 for(int i=0; i<length; ++i) {
1685 b.put((float) src[offset+i]);
1686 }
1687 return (B)b;
1688 } else {
1689 throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest);
1690 }
1691 }
1692
1693 //----------------------------------------------------------------------
1694 // Convenient put methods with generic target Buffer and value range conversion, i.e. normalization
1695 //
1696
1697 /**
1698 * Store byte source value in given buffer after normalizing it to the destination value range
1699 * considering signed and unsigned source and destination representation.
1700 *
1701 * @param dest One of {@link ByteBuffer}, {@link ShortBuffer}, {@link IntBuffer}, {@link FloatBuffer}
1702 * @param dSigned true if destination buffer holds signed values, false if destination buffer holds unsigned values
1703 * @param v source byte value to be put in dest buffer
1704 * @param sSigned true if source represents a signed value, false if source represents an unsigned value
1705 */
1706 @SuppressWarnings("unchecked")
1707 public static <B extends Buffer> B putNb(final B dest, final boolean dSigned, final byte v, final boolean sSigned) {
1708 if (dest instanceof ByteBuffer) {
1709 return (B) ((ByteBuffer) dest).put( v );
1710 } else if (dest instanceof ShortBuffer) {
1711 return (B) ((ShortBuffer) dest).put( ValueConv.byte_to_short(v, sSigned, dSigned) );
1712 } else if (dest instanceof IntBuffer) {
1713 return (B) ((IntBuffer) dest).put( ValueConv.byte_to_int(v, sSigned, dSigned) );
1714 } else if (dest instanceof FloatBuffer) {
1715 return (B) ((FloatBuffer) dest).put( ValueConv.byte_to_float(v, sSigned) );
1716 } else {
1717 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1718 }
1719 }
1720
1721 /**
1722 * Store short source value in given buffer after normalizing it to the destination value range
1723 * considering signed and unsigned source and destination representation.
1724 *
1725 * @param dest One of {@link ByteBuffer}, {@link ShortBuffer}, {@link IntBuffer}, {@link FloatBuffer}
1726 * @param dSigned true if destination buffer holds signed values, false if destination buffer holds unsigned values
1727 * @param v source short value to be put in dest buffer
1728 * @param sSigned true if source represents a signed value, false if source represents an unsigned value
1729 */
1730 @SuppressWarnings("unchecked")
1731 public static <B extends Buffer> B putNs(final B dest, final boolean dSigned, final short v, final boolean sSigned) {
1732 if (dest instanceof ByteBuffer) {
1733 return (B) ((ByteBuffer) dest).put( ValueConv.short_to_byte(v, sSigned, dSigned) );
1734 } else if (dest instanceof ShortBuffer) {
1735 return (B) ((ShortBuffer) dest).put( v );
1736 } else if (dest instanceof IntBuffer) {
1737 return (B) ((IntBuffer) dest).put( ValueConv.short_to_int(v, sSigned, dSigned) );
1738 } else if (dest instanceof FloatBuffer) {
1739 return (B) ((FloatBuffer) dest).put( ValueConv.short_to_float(v, sSigned) );
1740 } else {
1741 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1742 }
1743 }
1744
1745 /**
1746 * Store short source value in given buffer after normalizing it to the destination value range
1747 * considering signed and unsigned source and destination representation.
1748 *
1749 * @param dest One of {@link ByteBuffer}, {@link ShortBuffer}, {@link IntBuffer}, {@link FloatBuffer}
1750 * @param dSigned true if destination buffer holds signed values, false if destination buffer holds unsigned values
1751 * @param v source short value to be put in dest buffer
1752 * @param sSigned true if source represents a signed value, false if source represents an unsigned value
1753 */
1754 @SuppressWarnings("unchecked")
1755 public static <B extends Buffer> B putNi(final B dest, final boolean dSigned, final int v, final boolean sSigned) {
1756 if (dest instanceof ByteBuffer) {
1757 return (B) ((ByteBuffer) dest).put( ValueConv.int_to_byte(v, sSigned, dSigned) );
1758 } else if (dest instanceof ShortBuffer) {
1759 return (B) ((ShortBuffer) dest).put( ValueConv.int_to_short(v, sSigned, dSigned) );
1760 } else if (dest instanceof IntBuffer) {
1761 return (B) ((IntBuffer) dest).put( v );
1762 } else if (dest instanceof FloatBuffer) {
1763 return (B) ((FloatBuffer) dest).put( ValueConv.int_to_float(v, sSigned) );
1764 } else {
1765 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1766 }
1767 }
1768
1769 /**
1770 * Store float source value in given buffer after normalizing it to the destination value range
1771 * considering signed and unsigned destination representation.
1772 *
1773 * @param dest One of {@link ByteBuffer}, {@link ShortBuffer}, {@link IntBuffer}, {@link FloatBuffer}
1774 * @param dSigned true if destination buffer holds signed values, false if destination buffer holds unsigned values
1775 * @param v source float value to be put in dest buffer
1776 */
1777 @SuppressWarnings("unchecked")
1778 public static <B extends Buffer> B putNf(final B dest, final boolean dSigned, final float v) {
1779 if (dest instanceof ByteBuffer) {
1780 return (B) ((ByteBuffer) dest).put( ValueConv.float_to_byte(v, dSigned) );
1781 } else if (dest instanceof ShortBuffer) {
1782 return (B) ((ShortBuffer) dest).put( ValueConv.float_to_short(v, dSigned) );
1783 } else if (dest instanceof IntBuffer) {
1784 return (B) ((IntBuffer) dest).put( ValueConv.float_to_int(v, dSigned) );
1785 } else if (dest instanceof FloatBuffer) {
1786 return (B) ((FloatBuffer) dest).put( v );
1787 } else {
1788 throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest);
1789 }
1790 }
1791
1792 //----------------------------------------------------------------------
1793 // Range check methods
1794 //
1795
1796 public static void rangeCheck(final byte[] array, final int offset, final int minElementsRemaining) {
1797 if (array == null) {
1798 return;
1799 }
1800
1801 if (array.length < offset + minElementsRemaining) {
1802 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1803 }
1804 }
1805
1806 public static void rangeCheck(final char[] array, final int offset, final int minElementsRemaining) {
1807 if (array == null) {
1808 return;
1809 }
1810
1811 if (array.length < offset + minElementsRemaining) {
1812 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1813 }
1814 }
1815
1816 public static void rangeCheck(final short[] array, final int offset, final int minElementsRemaining) {
1817 if (array == null) {
1818 return;
1819 }
1820
1821 if (array.length < offset + minElementsRemaining) {
1822 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1823 }
1824 }
1825
1826 public static void rangeCheck(final int[] array, final int offset, final int minElementsRemaining) {
1827 if (array == null) {
1828 return;
1829 }
1830
1831 if (array.length < offset + minElementsRemaining) {
1832 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1833 }
1834 }
1835
1836 public static void rangeCheck(final long[] array, final int offset, final int minElementsRemaining) {
1837 if (array == null) {
1838 return;
1839 }
1840
1841 if (array.length < offset + minElementsRemaining) {
1842 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1843 }
1844 }
1845
1846 public static void rangeCheck(final float[] array, final int offset, final int minElementsRemaining) {
1847 if (array == null) {
1848 return;
1849 }
1850
1851 if (array.length < offset + minElementsRemaining) {
1852 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1853 }
1854 }
1855
1856 public static void rangeCheck(final double[] array, final int offset, final int minElementsRemaining) {
1857 if (array == null) {
1858 return;
1859 }
1860
1861 if (array.length < offset + minElementsRemaining) {
1862 throw new ArrayIndexOutOfBoundsException("Required " + minElementsRemaining + " elements in array, only had " + (array.length - offset));
1863 }
1864 }
1865
1866 public static void rangeCheck(final Buffer buffer, final int minElementsRemaining) {
1867 if (buffer == null) {
1868 return;
1869 }
1870
1871 if (buffer.remaining() < minElementsRemaining) {
1872 throw new IndexOutOfBoundsException("Required " + minElementsRemaining + " remaining elements in buffer, only had " + buffer.remaining());
1873 }
1874 }
1875
1876 /**
1877 * @param buffer buffer to test for minimum
1878 * @param minBytesRemaining minimum bytes remaining
1879 * @throws IllegalArgumentException if <code>buffer</code> is of invalid type.
1880 * @throws IndexOutOfBoundsException if {@link #remainingBytes(Object)} is &lt; <code>minBytesRemaining<code>.
1881 */
1882 public static void rangeCheckBytes(final Object buffer, final int minBytesRemaining) throws IllegalArgumentException, IndexOutOfBoundsException {
1883 if (buffer == null) {
1884 return;
1885 }
1886 final int bytesRemaining = remainingBytes(buffer);
1887 if (bytesRemaining < minBytesRemaining) {
1888 throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining);
1889 }
1890 }
1891
1892 /**
1893 * Appends Buffer details inclusive data to a StringBuilder instance.
1894 * @param sb optional pass through StringBuilder
1895 * @param f optional format string of one element, i.e. "%10.5f" for {@link FloatBuffer}, see {@link java.util.Formatter},
1896 * or <code>null</code> for unformatted output.
1897 * <b>Note:</b> Caller is responsible to match the format string w/ the data type as expected in the given buffer.
1898 * @param buffer Any valid Buffer instance
1899 * @return the modified StringBuilder containing the Buffer details
1900 */
1901 public static StringBuilder toString(StringBuilder sb, final String f, final Buffer buffer) {
1902 if(null == sb) {
1903 sb = new StringBuilder();
1904 }
1905 sb.append(buffer.getClass().getSimpleName());
1906 sb.append("[pos ").append(buffer.position()).append(", lim ").append(buffer.limit()).append(", cap ").append(buffer.capacity());
1907 sb.append(", remaining ").append(buffer.remaining());
1908 sb.append("; array ").append(buffer.hasArray()).append(", direct ").append(buffer.isDirect());
1909 sb.append(", r/w ").append(!buffer.isReadOnly()).append(": ");
1910 if (buffer instanceof ByteBuffer) {
1911 final ByteBuffer b = (ByteBuffer)buffer;
1912 for(int i=0; i<b.limit(); i++) {
1913 if(0<i) { sb.append(", "); }
1914 if(null == f) {
1915 sb.append(b.get(i));
1916 } else {
1917 sb.append(String.format(f, b.get(i)));
1918 }
1919 }
1920 } else if (buffer instanceof FloatBuffer) {
1921 final FloatBuffer b = (FloatBuffer)buffer;
1922 for(int i=0; i<b.limit(); i++) {
1923 if(0<i) { sb.append(", "); }
1924 if(null == f) {
1925 sb.append(b.get(i));
1926 } else {
1927 sb.append(String.format(f, b.get(i)));
1928 }
1929 }
1930 } else if (buffer instanceof IntBuffer) {
1931 final IntBuffer b = (IntBuffer)buffer;
1932 for(int i=0; i<b.limit(); i++) {
1933 if(0<i) { sb.append(", "); }
1934 if(null == f) {
1935 sb.append(b.get(i));
1936 } else {
1937 sb.append(String.format(f, b.get(i)));
1938 }
1939 }
1940 } else if (buffer instanceof ShortBuffer) {
1941 final ShortBuffer b = (ShortBuffer)buffer;
1942 for(int i=0; i<b.limit(); i++) {
1943 if(0<i) { sb.append(", "); }
1944 if(null == f) {
1945 sb.append(b.get(i));
1946 } else {
1947 sb.append(String.format(f, b.get(i)));
1948 }
1949 }
1950 } else if (buffer instanceof DoubleBuffer) {
1951 final DoubleBuffer b = (DoubleBuffer)buffer;
1952 for(int i=0; i<b.limit(); i++) {
1953 if(0<i) { sb.append(", "); }
1954 if(null == f) {
1955 sb.append(b.get(i));
1956 } else {
1957 sb.append(String.format(f, b.get(i)));
1958 }
1959 }
1960 } else if (buffer instanceof LongBuffer) {
1961 final LongBuffer b = (LongBuffer)buffer;
1962 for(int i=0; i<b.limit(); i++) {
1963 if(0<i) { sb.append(", "); }
1964 if(null == f) {
1965 sb.append(b.get(i));
1966 } else {
1967 sb.append(String.format(f, b.get(i)));
1968 }
1969 }
1970 } else if (buffer instanceof CharBuffer) {
1971 final CharBuffer b = (CharBuffer)buffer;
1972 for(int i=0; i<b.limit(); i++) {
1973 if(0<i) { sb.append(", "); }
1974 if(null == f) {
1975 sb.append(b.get(i));
1976 } else {
1977 sb.append(String.format(f, b.get(i)));
1978 }
1979 }
1980 }
1981 sb.append("]");
1982 return sb;
1983 }
1984
1985 /**
1986 * Access to NIO {@link sun.misc.Cleaner}, allowing caller to deterministically clean a given {@link sun.nio.ch.DirectBuffer}.
1987 */
1988 public static class Cleaner {
1989 private static final Method mbbCleaner;
1990 private static final Method cClean;
1991 /** OK to be lazy on thread synchronization, just for early out **/
1992 private static volatile boolean cleanerError;
1993 static {
1994 final Method[] _mbbCleaner = { null };
1995 final Method[] _cClean = { null };
1996 final boolean hasCleaner;
1997 if( SecurityUtil.doPrivileged(new PrivilegedAction<Boolean>() {
1998 @Override
1999 public Boolean run() {
2000 try {
2001 if( PlatformPropsImpl.JAVA_9 ) {
2003 } else {
2004 _mbbCleaner[0] = ReflectionUtil.getMethod("sun.nio.ch.DirectBuffer", "cleaner", null, Buffers.class.getClassLoader());
2005 _mbbCleaner[0].setAccessible(true);
2006 final Class<?> cleanerType = _mbbCleaner[0].getReturnType();
2007 // Java >= 9: jdk.internal.ref.Cleaner (NOT accessible!)
2008 // "Unable to make public void jdk.internal.ref.Cleaner.clean() accessible:
2009 // module java.base does not "exports jdk.internal.ref" to unnamed module"
2010 // Java <= 8: sun.misc.Cleaner OK
2011 _cClean[0] = cleanerType.getMethod("clean");
2012 _cClean[0].setAccessible(true);
2013 return Boolean.TRUE;
2014 }
2015 } catch(final Throwable t) {
2016 if( DEBUG ) {
2017 ExceptionUtils.dumpThrowable("Buffers", t);
2018 }
2019 return Boolean.FALSE;
2020 } } } ).booleanValue() ) {
2021 mbbCleaner = _mbbCleaner[0];
2022 cClean = _cClean[0];
2023 hasCleaner = PlatformPropsImpl.JAVA_9 || ( null != mbbCleaner && null != cClean );
2024 } else {
2025 mbbCleaner = null;
2026 cClean = null;
2027 hasCleaner = false;
2028 }
2029 cleanerError = !hasCleaner;
2030 if( DEBUG ) {
2031 System.err.print("Buffers.Cleaner.init: hasCleaner: "+hasCleaner+", cleanerError "+cleanerError);
2032 if( null != mbbCleaner ) {
2033 System.err.print(", using Cleaner class: "+mbbCleaner.getReturnType().getName());
2034 }
2035 System.err.println();
2036 }
2037 }
2038 /**
2039 * If {@code b} is an direct NIO buffer, i.e {@link sun.nio.ch.DirectBuffer},
2040 * calls it's {@link sun.misc.Cleaner} instance {@code clean()} method once.
2041 * @return {@code true} if successful, otherwise {@code false}.
2042 */
2043 public static boolean clean(final ByteBuffer bb) {
2044 if( cleanerError || !bb.isDirect() ) {
2045 return false;
2046 }
2047 try {
2048 if( PlatformPropsImpl.JAVA_9 ) {
2050 } else {
2051 cClean.invoke(mbbCleaner.invoke(bb));
2052 }
2053 return true;
2054 } catch(final Throwable t) {
2055 cleanerError = true;
2056 if( DEBUG ) {
2057 ExceptionUtils.dumpThrowable("Buffers", t);
2058 }
2059 return false;
2060 }
2061 }
2062 }
2063
2064 /**
2065 * Copy `len` native bytes @ `source_address` into a newly created direct {@link ByteBuffer}.
2066 * @param source_address memory address of bytes to copy from
2067 * @param len number of bytes to copy
2068 * @return newly created direct {@link ByteBuffer} holding the copied bytes
2069 */
2070 public static ByteBuffer copyNativeToDirectByteBuffer(final long source_address, final long len) {
2071 if( Integer.MAX_VALUE < len ) {
2072 throw new IllegalArgumentException("length "+len+" > MAX_INT");
2073 }
2074 final int lenI = (int)len;
2075 final ByteBuffer bb = Buffers.newDirectByteBuffer(lenI);
2076 if( null == bb ) {
2077 throw new RuntimeException("New direct ByteBuffer is NULL");
2078 }
2079 if( 0 < lenI ) {
2080 final long byteBufferPtr = getDirectBufferAddressImpl(bb);
2081 memcpyImpl(byteBufferPtr, source_address, lenI);
2082 }
2083 return bb;
2084 }
2085
2086 /* pp */ static ByteBuffer getDirectByteBuffer(final long aptr, final int byteCount) {
2087 final ByteBuffer r = getDirectByteBufferImpl(aptr, byteCount);
2088 return null != r ? nativeOrder( r ) : null;
2089 }
2090
2091 /* pp */ static void storeDirectAddress(final long addr, final ByteBuffer dest, final int destBytePos, final int nativeSizeInBytes) {
2092 switch(nativeSizeInBytes) {
2093 case 4:
2094 dest.putInt(destBytePos, (int) ( addr & 0x00000000FFFFFFFFL ) );
2095 break;
2096 case 8:
2097 dest.putLong(destBytePos, addr);
2098 break;
2099 default:
2100 throw new InternalError("invalid nativeSizeInBytes "+nativeSizeInBytes);
2101 }
2102 }
2103
2104 /**
2105 * Returns <code>strnlen(cstrptr, maxlen)</code> according to POSIX.1-2008.
2106 * <p>
2107 * The `strnlen()` function returns the number of bytes in the string pointed to by `cstrptr`, excluding the terminating null byte ('\0'), but at most `maxlen`.
2108 * In doing this, `strnlen()` looks only at the first `maxlen` characters in the string pointed to by `cstrptr` and never beyond `cstrptr[maxlen-1]`.
2109 * </p>
2110 */
2111 public static int strnlen(final long cstrptr, final int maxlen) {
2112 return strnlenImpl(cstrptr, maxlen);
2113 }
2114
2115 /**
2116 * Returns <code>memcpy(dest, src, len)</code> according to POSIX.1-2001, POSIX.1-2008.
2117 * <p>
2118 * The `memcpy()` function copies `len` bytes from memory area `src` to memory area `dest`. The memory areas must not overlap.<br/>
2119 * The `memcpy()` function returns a pointer to `dest`.
2120 * </p>
2121 public static long memcpy(final long dest, final long src, final long len) {
2122 return memcpyImpl(dest, src, len);
2123 }
2124 */
2125
2126 /* pp */ static native long getDirectBufferAddressImpl(Object directBuffer);
2127 private static native ByteBuffer getDirectByteBufferImpl(long aptr, int byteCount);
2128 private static native int strnlenImpl(long cstrptr, int maxlen);
2129 private static native long memcpyImpl(long dest, long src, long len);
2130}
static void dumpThrowable(final String additionalDescr, final Throwable t)
Dumps a Throwable to System.err in a decorating message including the current thread name,...
Access to NIO sun.misc.Cleaner, allowing caller to deterministically clean a given sun....
Definition: Buffers.java:1988
static boolean clean(final ByteBuffer bb)
If b is an direct NIO buffer, i.e sun.nio.ch.DirectBuffer, calls it's sun.misc.Cleaner instance clean...
Definition: Buffers.java:2043
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
static LongBuffer newDirectLongBuffer(final int numElements)
Allocates a new direct LongBuffer with the specified number of elements.
Definition: Buffers.java:176
static int remainingBytes(final Object buffer)
Returns the number of remaining bytes of the given anonymous buffer.
Definition: Buffers.java:355
static< B extends Buffer > B putNi(final B dest, final boolean dSigned, final int v, final boolean sSigned)
Store short source value in given buffer after normalizing it to the destination value range consider...
Definition: Buffers.java:1755
static final FloatBuffer slice2Float(final Buffer buf, final int elementStartPos, final int elementCount)
Slices a ByteBuffer or a FloatBuffer to a FloatBuffer at the given elementStartPos with the given ele...
Definition: Buffers.java:565
static CharBuffer newDirectCharBuffer(final char[] values, final int offset, final int length)
Definition: Buffers.java:222
static final FloatBuffer slice2Float(final float[] backing, final int elementStartPos, final int elementCount)
Slices a primitive float backing array to a FloatBuffer at the given elementStartPos with the given e...
Definition: Buffers.java:613
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< B extends Buffer > B put3s(final B dest, final short v1, final short v2, final short v3)
Definition: Buffers.java:1338
static IntBuffer copyIntBuffer(final IntBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed IntBuffer into a new...
Definition: Buffers.java:956
static ShortBuffer copyShortBuffer(final ShortBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed ShortBuffer into a n...
Definition: Buffers.java:968
static void rangeCheck(final long[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1836
static LongBuffer newDirectLongBuffer(final long[] values)
Definition: Buffers.java:188
static< B extends Buffer > B putb(final B dest, final byte v)
Definition: Buffers.java:1150
static ByteBuffer newDirectByteBuffer(final byte[] values)
Definition: Buffers.java:104
static IntBuffer newDirectIntBuffer(final int[] values)
Definition: Buffers.java:167
static final DoubleBuffer slice2Double(final Buffer buf, final int elementStartPos, final int elementCount)
Slices a ByteBuffer or a DoubleBuffer to a DoubleBuffer at the given elementStartPos with the given e...
Definition: Buffers.java:874
static boolean isDirect(final Object buf)
Helper routine to tell whether a buffer is direct or not.
Definition: Buffers.java:392
static< B extends Buffer > B put3d(final B dest, final double v1, final double v2, final double v3)
Definition: Buffers.java:1639
static< B extends Buffer > B putf(final B dest, final float v)
Definition: Buffers.java:1558
static final IntBuffer slice2Int(final int[] backing, final int elementStartPos, final int elementCount)
Slices a primitive float backing array to a IntBuffer at the given elementStartPos with the given ele...
Definition: Buffers.java:793
static void rangeCheck(final Buffer buffer, final int minElementsRemaining)
Definition: Buffers.java:1866
static Class<? extends Buffer > typeNameToBufferClass(final String typeName)
Returns Buffer class matching the given lower case typeName
Definition: Buffers.java:249
static FloatBuffer newDirectFloatBuffer(final float[] values)
Definition: Buffers.java:146
static final ShortBuffer slice2Short(final short[] backing, final int elementStartPos, final int elementCount)
Slices a primitive float backing array to a ShortBuffer at the given elementStartPos with the given e...
Definition: Buffers.java:673
static FloatBuffer newDirectFloatBuffer(final int numElements)
Allocates a new direct FloatBuffer with the specified number of elements.
Definition: Buffers.java:134
static< B extends Buffer > B put(final B dest, final Buffer src)
Definition: Buffers.java:1130
static DoubleBuffer newDirectDoubleBuffer(final double[] values)
Definition: Buffers.java:125
static LongBuffer newDirectLongBuffer(final long[] values, final int offset, final int length)
Definition: Buffers.java:180
static CharBuffer newDirectCharBuffer(final char[] values)
Definition: Buffers.java:230
static void rangeCheck(final float[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1846
static DoubleBuffer newDirectDoubleBuffer(final double[] values, final int offset)
Definition: Buffers.java:121
static ShortBuffer newDirectShortBuffer(final short[] values)
Definition: Buffers.java:209
static final CharBuffer slice2Char(final Buffer buf, final int elementStartPos, final int elementCount)
Slices a ByteBuffer or a CharBuffer to a CharBuffer at the given elementStartPos with the given eleme...
Definition: Buffers.java:694
static int strnlen(final long cstrptr, final int maxlen)
Returns strnlen(cstrptr, maxlen) according to POSIX.1-2008.
Definition: Buffers.java:2111
static FloatBuffer copyFloatBuffer(final FloatBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed FloatBuffer into a n...
Definition: Buffers.java:944
static int getIndirectBufferByteOffset(final Object buf)
Helper routine to get the full byte offset from the beginning of the array that is the storage for th...
Definition: Buffers.java:463
static int getDirectBufferByteOffset(final Object buf)
Helper routine to get the Buffer byte offset by taking into account the Buffer position and the under...
Definition: Buffers.java:409
static< B extends Buffer > B put4i(final B dest, final int v1, final int v2, final int v3, final int v4)
Definition: Buffers.java:1496
static final LongBuffer slice2Long(final Buffer buf, final int elementStartPos, final int elementCount)
Slices a ByteBuffer or a LongBuffer to a LongBuffer at the given elementStartPos with the given eleme...
Definition: Buffers.java:814
static FloatBuffer newDirectFloatBuffer(final float[] values, final int offset)
Definition: Buffers.java:142
static DoubleBuffer newDirectDoubleBuffer(final int numElements)
Allocates a new direct DoubleBuffer with the specified number of elements.
Definition: Buffers.java:113
static IntBuffer newDirectIntBuffer(final int[] values, final int offset, final int length)
Definition: Buffers.java:159
static< B extends Buffer > B put3i(final B dest, final int v1, final int v2, final int v3)
Definition: Buffers.java:1465
static int sizeOfBufferElem(final Object buffer)
Returns the size of a single element of the given buffer in bytes or 0 if the given buffer is null.
Definition: Buffers.java:304
static< B extends Buffer > B putNf(final B dest, final boolean dSigned, final float v)
Store float source value in given buffer after normalizing it to the destination value range consider...
Definition: Buffers.java:1778
static void rangeCheckBytes(final Object buffer, final int minBytesRemaining)
Definition: Buffers.java:1882
static FloatBuffer getFloatBuffer(final DoubleBuffer source, FloatBuffer dest)
No rewind or repositioning is performed.
Definition: Buffers.java:1064
static final int SIZEOF_SHORT
Definition: Buffers.java:78
static< B extends Buffer > B put3f(final B dest, final float v1, final float v2, final float v3)
Definition: Buffers.java:1573
static< B extends Buffer > B putNb(final B dest, final boolean dSigned, final byte v, final boolean sSigned)
Store byte source value in given buffer after normalizing it to the destination value range consideri...
Definition: Buffers.java:1707
static final int SIZEOF_DOUBLE
Definition: Buffers.java:83
static< B extends Buffer > B puti(final B dest, final int v)
Definition: Buffers.java:1450
static< B extends Buffer > B put4f(final B dest, final float v1, final float v2, final float v3, final float v4)
Definition: Buffers.java:1592
static ByteBuffer copyIntBufferAsByteBuffer(final IntBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed IntBuffer into a new...
Definition: Buffers.java:1000
static FloatBuffer newDirectFloatBuffer(final float[] values, final int offset, final int length)
Definition: Buffers.java:138
static ShortBuffer newDirectShortBuffer(final short[] values, final int offset, final int length)
Definition: Buffers.java:201
static final int SIZEOF_CHAR
Definition: Buffers.java:79
static< B extends Buffer > B puts(final B dest, final short v)
Definition: Buffers.java:1321
static ShortBuffer newDirectShortBuffer(final short[] values, final int offset)
Definition: Buffers.java:205
static StringBuilder toString(StringBuilder sb, final String f, final Buffer buffer)
Appends Buffer details inclusive data to a StringBuilder instance.
Definition: Buffers.java:1901
static final int SIZEOF_INT
Definition: Buffers.java:80
static final int SIZEOF_LONG
Definition: Buffers.java:82
static ByteBuffer newDirectByteBuffer(final byte[] values, final int offset)
Definition: Buffers.java:100
static< B extends Buffer > B putd(final B dest, final double v)
Definition: Buffers.java:1628
static void rangeCheck(final double[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1856
static void rangeCheck(final char[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1806
static final DoubleBuffer slice2Double(final double[] backing, final int elementStartPos, final int elementCount)
Slices a primitive float backing array to a DoubleBuffer at the given elementStartPos with the given ...
Definition: Buffers.java:913
static int sizeOfBufferElem(final Class<? extends Buffer > bufferClz)
Returns the size of a single element of the given buffer class in bytes or 0 if the given buffer is n...
Definition: Buffers.java:277
static< B extends Buffer > B putNs(final B dest, final boolean dSigned, final short v, final boolean sSigned)
Store short source value in given buffer after normalizing it to the destination value range consider...
Definition: Buffers.java:1731
static final int SIZEOF_FLOAT
Definition: Buffers.java:81
static< B extends Buffer > B slice(final B buffer, final int offset, final int size)
Slices the specified buffer with offset as position and offset+size as limit while maintaining the by...
Definition: Buffers.java:527
static final CharBuffer slice2Char(final char[] backing, final int elementStartPos, final int elementCount)
Slices a primitive float backing array to a CharBuffer at the given elementStartPos with the given el...
Definition: Buffers.java:733
static< B extends Buffer > B slice(final B buffer)
Calls slice on the specified buffer while maintaining the byteorder.
Definition: Buffers.java:501
static< B extends Buffer > B put4d(final B dest, final double v1, final double v2, final double v3, final double v4)
Definition: Buffers.java:1658
static LongBuffer newDirectLongBuffer(final long[] values, final int offset)
Definition: Buffers.java:184
static ByteBuffer newDirectByteBuffer(final byte[] values, final int offset, final int length)
Definition: Buffers.java:96
static< B extends Buffer > B put3b(final B dest, final byte v1, final byte v2, final byte v3)
Definition: Buffers.java:1171
static CharBuffer newDirectCharBuffer(final int numElements)
Allocates a new direct CharBuffer with the specified number of elements.
Definition: Buffers.java:218
static CharBuffer newDirectCharBuffer(final char[] values, final int offset)
Definition: Buffers.java:226
static final LongBuffer slice2Long(final long[] backing, final int elementStartPos, final int elementCount)
Slices a primitive float backing array to a LongBuffer at the given elementStartPos with the given el...
Definition: Buffers.java:853
static IntBuffer newDirectIntBuffer(final int[] values, final int offset)
Definition: Buffers.java:163
static ByteBuffer copyByteBuffer(final ByteBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed ByteBuffer into a ne...
Definition: Buffers.java:927
static< B extends Buffer > B put4b(final B dest, final byte v1, final byte v2, final byte v3, final byte v4)
Definition: Buffers.java:1220
static DoubleBuffer newDirectDoubleBuffer(final double[] values, final int offset, final int length)
Definition: Buffers.java:117
static ByteBuffer copyShortBufferAsByteBuffer(final ShortBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed ShortBuffer into a n...
Definition: Buffers.java:1017
static DoubleBuffer getDoubleBuffer(final FloatBuffer source, DoubleBuffer dest)
No rewind or repositioning is performed.
Definition: Buffers.java:1111
static double[] getDoubleArray(final float[] source, final int soffset, double[] dest, int doffset, int len)
Definition: Buffers.java:1085
static final IntBuffer slice2Int(final Buffer buf, final int elementStartPos, final int elementCount)
Slices a ByteBuffer or a IntBuffer to a IntBuffer at the given elementStartPos with the given element...
Definition: Buffers.java:754
static void rangeCheck(final short[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1816
static void rangeCheck(final byte[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1796
static ShortBuffer newDirectShortBuffer(final int numElements)
Allocates a new direct ShortBuffer with the specified number of elements.
Definition: Buffers.java:197
static ByteBuffer copyNativeToDirectByteBuffer(final long source_address, final long len)
Copy len native bytes @ source_address into a newly created direct ByteBuffer.
Definition: Buffers.java:2070
static final ShortBuffer slice2Short(final Buffer buf, final int elementStartPos, final int elementCount)
Slices a ByteBuffer or a ShortBuffer to a ShortBuffer at the given elementStartPos with the given ele...
Definition: Buffers.java:634
static void rangeCheck(final int[] array, final int offset, final int minElementsRemaining)
Definition: Buffers.java:1826
static Object getArray(final Object buf)
Helper routine to return the array backing store reference from a Buffer object.
Definition: Buffers.java:444
static float[] getFloatArray(final double[] source, final int soffset, float[] dest, int doffset, int len)
Definition: Buffers.java:1038
static IntBuffer newDirectIntBuffer(final int numElements)
Allocates a new direct IntBuffer with the specified number of elements.
Definition: Buffers.java:155
static ByteBuffer copyFloatBufferAsByteBuffer(final FloatBuffer orig)
Copies the remaining elements (as defined by limit() - position()) in the passed FloatBuffer into a n...
Definition: Buffers.java:983
static int remainingElem(final Object buffer)
Returns the number of remaining elements of the given anonymous buffer.
Definition: Buffers.java:335
static ByteBuffer nativeOrder(final ByteBuffer buf)
Helper routine to set a ByteBuffer to the native byte order, if that operation is supported by the un...
Definition: Buffers.java:239
static< B extends Buffer > B put4s(final B dest, final short v1, final short v2, final short v3, final short v4)
Definition: Buffers.java:1375
Hardware independent container holding an array of native pointer, while its getDirectBufferAddress()...
static final Method getMethod(final Class<?> clazz, final String methodName, final Class<?> ... argTypes)
static< T > T doPrivileged(final PrivilegedAction< T > o)
Call wrapper for java.security.AccessController#doPrivileged(PrivilegedAction).
Utility methods allowing easy access to certain sun.misc.Unsafe functionality.
Definition: UnsafeUtil.java:44
static boolean invokeCleaner(final ByteBuffer bb)
Access to sun.misc.Unsafe.invokeCleaner(java.nio.ByteBuffer).
static boolean hasInvokeCleaner()
Returns true if sun.misc.Unsafe.invokeCleaner(java.nio.ByteBuffer) is available and has not caused an...
Copyright 2012 JogAmp Community.
Definition: ValueConv.java:39
static final float short_to_float(final short v, final boolean sSigned)
Definition: ValueConv.java:106
static final int byte_to_int(final byte v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:144
static final int float_to_int(final float v, final boolean dSigned)
Definition: ValueConv.java:55
static final byte short_to_byte(final short v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:148
static final byte float_to_byte(final float v, final boolean dSigned)
Definition: ValueConv.java:40
static final short float_to_short(final float v, final boolean dSigned)
Definition: ValueConv.java:48
static final short byte_to_short(final byte v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:141
static final short int_to_short(final int v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:158
static final float byte_to_float(final byte v, final boolean sSigned)
Definition: ValueConv.java:92
static final byte int_to_byte(final int v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:155
static final float int_to_float(final int v, final boolean sSigned)
Definition: ValueConv.java:121
static final int short_to_int(final short v, final boolean sSigned, final boolean dSigned)
Definition: ValueConv.java:151
Hardware independent container for various kinds of buffers.
int elementSize()
Returns byte size of one element.
int position()
Returns this buffer's element position.
int remaining()
Returns this buffer's remaining element, i.e.