32package com.jogamp.common.nio;
34import java.io.IOException;
35import java.nio.ByteBuffer;
36import java.nio.CharBuffer;
37import java.nio.DoubleBuffer;
38import java.nio.FloatBuffer;
39import java.nio.IntBuffer;
40import java.nio.LongBuffer;
41import java.nio.ShortBuffer;
45import com.jogamp.junit.util.SingletonJunitCase;
47import static org.junit.Assert.*;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60 final byte[] byteData = { 1, 2, 3, 4, 5, 6, 7, 8 };
62 assertEquals(0, byteBuffer.position());
63 assertEquals(8, byteBuffer.limit());
64 assertEquals(8, byteBuffer.capacity());
65 assertEquals(8, byteBuffer.remaining());
66 assertEquals(5, byteBuffer.get(4));
68 final double[] doubleData = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
70 assertEquals(0, doubleBuffer.position());
71 assertEquals(8, doubleBuffer.limit());
72 assertEquals(8, doubleBuffer.capacity());
73 assertEquals(8, doubleBuffer.remaining());
74 assertEquals(5.0, doubleBuffer.get(4), 0.1);
76 final float[] floatData = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
78 assertEquals(0, floatBuffer.position());
79 assertEquals(8, floatBuffer.limit());
80 assertEquals(8, floatBuffer.capacity());
81 assertEquals(8, floatBuffer.remaining());
82 assertEquals(5.0f, floatBuffer.get(4), 0.1f);
84 final int[] intData = { 1, 2, 3, 4, 5, 6, 7, 8 };
86 assertEquals(0, intBuffer.position());
87 assertEquals(8, intBuffer.limit());
88 assertEquals(8, intBuffer.capacity());
89 assertEquals(8, intBuffer.remaining());
90 assertEquals(5, intBuffer.get(4));
92 final long[] longData = { 1, 2, 3, 4, 5, 6, 7, 8 };
94 assertEquals(0, longBuffer.position());
95 assertEquals(8, longBuffer.limit());
96 assertEquals(8, longBuffer.capacity());
97 assertEquals(8, longBuffer.remaining());
98 assertEquals(5, longBuffer.get(4));
100 final short[] shortData = { 1, 2, 3, 4, 5, 6, 7, 8 };
102 assertEquals(0, shortBuffer.position());
103 assertEquals(8, shortBuffer.limit());
104 assertEquals(8, shortBuffer.capacity());
105 assertEquals(8, shortBuffer.remaining());
106 assertEquals(5, shortBuffer.get(4));
108 final char[] charData = { 1, 2, 3, 4, 5, 6, 7, 8 };
110 assertEquals(0, charBuffer.position());
111 assertEquals(8, charBuffer.limit());
112 assertEquals(8, charBuffer.capacity());
113 assertEquals(8, charBuffer.remaining());
114 assertEquals(5, charBuffer.get(4));
121 buffer.put(
new int[]{1,2,3,4,5,6}).rewind();
123 final IntBuffer threefour =
Buffers.
slice(buffer, 2, 2);
125 assertEquals(3, threefour.get(0));
126 assertEquals(4, threefour.get(1));
127 assertEquals(2, threefour.capacity());
129 assertEquals(0, buffer.position());
130 assertEquals(6, buffer.limit());
132 final IntBuffer fourfivesix =
Buffers.
slice(buffer, 3, 3);
134 assertEquals(4, fourfivesix.get(0));
135 assertEquals(5, fourfivesix.get(1));
136 assertEquals(6, fourfivesix.get(2));
137 assertEquals(3, fourfivesix.capacity());
139 assertEquals(0, buffer.position());
140 assertEquals(6, buffer.limit());
142 final IntBuffer onetwothree =
Buffers.
slice(buffer, 0, 3);
144 assertEquals(1, onetwothree.get(0));
145 assertEquals(2, onetwothree.get(1));
146 assertEquals(3, onetwothree.get(2));
147 assertEquals(3, onetwothree.capacity());
149 assertEquals(0, buffer.position());
150 assertEquals(6, buffer.limit());
155 assertEquals(42, buffer.get(2));
156 assertEquals(42, onetwothree.get(2));
165 public static void main(
final String args[])
throws IOException {
166 final String tstname =
TestBuffers.class.getName();
167 org.junit.runner.JUnitCore.
main(tstname);
Access to NIO sun.misc.Cleaner, allowing caller to deterministically clean a given sun....
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...
Utility methods allowing easy java.nio.Buffer manipulations.
static LongBuffer newDirectLongBuffer(final int numElements)
Allocates a new direct LongBuffer with the specified number of elements.
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
static FloatBuffer newDirectFloatBuffer(final int numElements)
Allocates a new direct FloatBuffer with the specified number of elements.
static DoubleBuffer newDirectDoubleBuffer(final int numElements)
Allocates a new direct DoubleBuffer with the specified number of elements.
static< B extends Buffer > B slice(final B buffer)
Calls slice on the specified buffer while maintaining the byteorder.
static CharBuffer newDirectCharBuffer(final int numElements)
Allocates a new direct CharBuffer with the specified number of elements.
static ShortBuffer newDirectShortBuffer(final int numElements)
Allocates a new direct ShortBuffer with the specified number of elements.
static IntBuffer newDirectIntBuffer(final int numElements)
Allocates a new direct IntBuffer with the specified number of elements.
void test01PositionLimitCapacityAfterArrayAllocation()
static void main(final String args[])