Java : DoubleBuffer with Examples
DoubleBuffer (Java SE 20 & JDK 20) API Examples.
You will find code examples on most DoubleBuffer methods.
Summary
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
buffer.put(0.1);
System.out.println(buffer.position()); // 1
buffer.put(0.2);
System.out.println(buffer.position()); // 2
buffer.put(0.3);
System.out.println(buffer.position()); // 3
if (buffer.hasArray()) {
final var ret = buffer.array();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 0.0, 0.0]
}
final double[] array = {0.1, 0.2, 0.3};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 3
System.out.println(buffer.limit()); // 3
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 10
System.out.println(buffer.position()); // 0.1
System.out.println(buffer.get()); // 20
System.out.println(buffer.position()); // 0.2
System.out.println(buffer.get()); // 30
System.out.println(buffer.position()); // 0.3
Methods
static DoubleBuffer allocate (int capacity)
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.0, 0.0, 0.0, 0.0, 0.0]
}
final double[] array ()
final var buffer = DoubleBuffer.allocate(5)
.put(0.1)
.put(0.2)
.put(0.3);
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0]
}
final int arrayOffset ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
final var sliced = buffer.slice(2, 3);
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
}
System.out.println(sliced); // java.nio.HeapDoubleBuffer[pos=0 lim=3 cap=3]
if (sliced.hasArray()) {
System.out.println(sliced.arrayOffset()); // 2
System.out.println(sliced.get()); // 0.3
System.out.println(sliced.get()); // 0.4
System.out.println(sliced.get()); // 0.5
}
abstract DoubleBuffer asReadOnlyBuffer ()
final var buffer = DoubleBuffer.allocate(3);
buffer.put(0.1);
final var readOnlyBuffer = buffer.asReadOnlyBuffer();
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=1 lim=3 cap=3]
System.out.println(readOnlyBuffer); // java.nio.HeapDoubleBufferR[pos=1 lim=3 cap=3]
readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 0.1
System.out.println(readOnlyBuffer.get()); // 0.0
System.out.println(readOnlyBuffer.get()); // 0.0
buffer.put(0.2);
readOnlyBuffer.clear();
System.out.println(readOnlyBuffer.get()); // 0.1
System.out.println(readOnlyBuffer.get()); // 0.2
System.out.println(readOnlyBuffer.get()); // 0.0
try {
final var ret = readOnlyBuffer.put(0.3);
} catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException!");
}
// Result
// ↓
//ReadOnlyBufferException!
final DoubleBuffer clear ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(2);
buffer.limit(4);
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=2 lim=4 cap=5]
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.hasRemaining()); // false
System.out.println(buffer.clear()); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.hasRemaining()); // false
abstract DoubleBuffer compact ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(3);
System.out.println(buffer.compact()); // java.nio.HeapDoubleBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.4, 0.5, 0.6, 0.4, 0.5, 0.6]
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(2);
System.out.println(buffer.compact()); // java.nio.HeapDoubleBuffer[pos=4 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.3, 0.4, 0.5, 0.6, 0.5, 0.6]
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(4);
System.out.println(buffer.compact()); // java.nio.HeapDoubleBuffer[pos=2 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.5, 0.6, 0.3, 0.4, 0.5, 0.6]
int compareTo (DoubleBuffer that)
final var buffer1 = DoubleBuffer.allocate(3)
.put(0.1)
.put(0.2)
.put(0.3);
final var buffer2 = DoubleBuffer.allocate(3)
.put(0.1)
.put(0.2)
.put(0.3);
buffer1.clear();
buffer2.clear();
System.out.println(buffer1.compareTo(buffer2)); // 0
final var buffer1 = DoubleBuffer.allocate(3)
.put(10.0)
.put(20.0)
.put(30.0);
final var buffer2 = DoubleBuffer.allocate(3)
.put(5.0)
.put(50.0)
.put(500.0);
final var buffer3 = DoubleBuffer.allocate(3)
.put(30.0)
.put(20.0)
.put(10.0);
buffer1.clear();
buffer2.clear();
buffer3.clear();
System.out.println(buffer1.compareTo(buffer2)); // 1
System.out.println(buffer1.compareTo(buffer3)); // -1
abstract DoubleBuffer duplicate ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(1);
buffer.limit(4);
final var duplicated = buffer.duplicate();
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=1 lim=4 cap=5]
System.out.println(duplicated); // java.nio.HeapDoubleBuffer[pos=1 lim=4 cap=5]
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(duplicated.get()); // 0.2
System.out.println(duplicated.get()); // 0.3
System.out.println(duplicated.get()); // 0.4
boolean equals (Object ob)
final var buffer1 = DoubleBuffer.allocate(3)
.put(0.1)
.put(0.2)
.put(0.3);
final var buffer2 = DoubleBuffer.allocate(3)
.put(0.1)
.put(0.2)
.put(0.3);
buffer1.clear();
buffer2.clear();
System.out.println(buffer1.equals(buffer2)); // true
final var buffer1 = DoubleBuffer.allocate(3)
.put(10.0)
.put(20.0)
.put(30.0);
final var buffer2 = DoubleBuffer.allocate(3)
.put(5.0)
.put(50.0)
.put(500.0);
final var buffer3 = DoubleBuffer.allocate(3)
.put(30.0)
.put(20.0)
.put(10.0);
buffer1.clear();
buffer2.clear();
buffer3.clear();
System.out.println(buffer1.equals(buffer2)); // false
System.out.println(buffer1.equals(buffer3)); // false
final DoubleBuffer flip ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.flip()); // java.nio.HeapDoubleBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
abstract double get ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.position()); // 5
DoubleBuffer get (double[] dst)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 7
System.out.println(buffer.position()); // 0
{
final var dst = new double[3];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]
final var ret = buffer.get(dst);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=3 lim=7 cap=7]
System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3]
System.out.println(buffer.position()); // 3
}
{
final var dst = new double[3];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]
final var ret = buffer.get(dst);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=6 lim=7 cap=7]
System.out.println(Arrays.toString(dst)); // [0.4, 0.5, 0.6]
System.out.println(buffer.position()); // 6
}
try {
final var dst = new double[3];
final var ret = buffer.get(dst);
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// Result
// ↓
//BufferUnderflowException!
DoubleBuffer get (double[] dst, int offset, int length)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 8
System.out.println(buffer.position()); // 0
{
final var dst = new double[5];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]
final var ret = buffer.get(dst, 0, 3);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=3 lim=8 cap=8]
System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3, 0.0, 0.0]
System.out.println(buffer.position()); // 3
}
{
final var dst = new double[5];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]
final var ret = buffer.get(dst, 1, 3);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=6 lim=8 cap=8]
System.out.println(Arrays.toString(dst)); // [0.0, 0.4, 0.5, 0.6, 0.0]
System.out.println(buffer.position()); // 6
}
{
final var dst = new double[5];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]
final var ret = buffer.get(dst, 3, 2);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=8 lim=8 cap=8]
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.7, 0.8]
System.out.println(buffer.position()); // 8
}
abstract double get (int index)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.get(0)); // 0.1
System.out.println(buffer.get(1)); // 0.2
System.out.println(buffer.get(2)); // 0.3
System.out.println(buffer.get(3)); // 0.4
System.out.println(buffer.get(4)); // 0.5
System.out.println(buffer.position()); // 0
DoubleBuffer get (int index, double[] dst)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
{
final var dst = new double[3];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]
final var ret = buffer.get(0, dst);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3]
System.out.println(buffer.position()); // 0
}
{
final var dst = new double[3];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]
final var ret = buffer.get(1, dst);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(dst)); // [0.2, 0.3, 0.4]
System.out.println(buffer.position()); // 0
}
{
final var dst = new double[3];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0]
final var ret = buffer.get(2, dst);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(Arrays.toString(dst)); // [0.3, 0.4, 0.5]
System.out.println(buffer.position()); // 0
}
DoubleBuffer get (int index, double[] dst, int offset, int length)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
{
final var dst = new double[5];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]
final var ret = buffer.get(0, dst, 0, 3);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(dst)); // [0.1, 0.2, 0.3, 0.0, 0.0]
System.out.println(buffer.position()); // 0
}
{
final var dst = new double[5];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]
final var ret = buffer.get(2, dst, 1, 3);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(dst)); // [0.0, 0.3, 0.4, 0.5, 0.0]
System.out.println(buffer.position()); // 0
}
{
final var dst = new double[5];
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.0, 0.0]
final var ret = buffer.get(4, dst, 3, 2);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(dst)); // [0.0, 0.0, 0.0, 0.5, 0.6]
System.out.println(buffer.position()); // 0
}
final boolean hasArray ()
final var buffer = DoubleBuffer.allocate(5)
.put(0.1)
.put(0.2)
.put(0.3);
final var ret = buffer.hasArray();
System.out.println(ret); // true
if (ret) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0]
}
final var buffer = DoubleBuffer.allocate(5)
.put(0.1)
.put(0.2)
.put(0.3)
.asReadOnlyBuffer();
System.out.println(buffer.hasArray()); // false
try {
final var array = buffer.array();
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException!");
}
// Result
// ↓
//UnsupportedOperationException!
int hashCode ()
final var buffer = DoubleBuffer.allocate(0);
System.out.println(buffer.hashCode()); // 1
final var buffer = DoubleBuffer.allocate(3)
.put(0.1)
.put(0.2)
.put(0.3)
.clear();
System.out.println(buffer.hashCode()); // 29791
final var buffer = DoubleBuffer.allocate(3)
.put(-123.0)
.put(-456.0)
.put(-789.0)
.clear();
System.out.println(buffer.hashCode()); // -742697
abstract boolean isDirect ()
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.isDirect()); // false
final double[] array = {0.1, 0.2, 0.3};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.isDirect()); // false
final var buffer = ByteBuffer.allocateDirect(16).asDoubleBuffer();
System.out.println(buffer.isDirect()); // true
final DoubleBuffer limit (int newLimit)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
System.out.println(buffer.limit(3)); // java.nio.HeapDoubleBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.limit()); // 3
System.out.println(buffer.clear()); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.limit()); // 5
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.limit(3)); // java.nio.HeapDoubleBuffer[pos=0 lim=3 cap=5]
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
try {
final var ret = buffer.get();
} catch (BufferUnderflowException e) {
System.out.println("BufferUnderflowException!");
}
// Result
// ↓
//BufferUnderflowException!
final DoubleBuffer mark ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.position()); // 2
System.out.println(buffer.mark()); // java.nio.HeapDoubleBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.position()); // 5
System.out.println(buffer.reset()); // java.nio.HeapDoubleBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
int mismatch (DoubleBuffer that)
final double[] array = {0.1, 0.2, 0.3, 0.4};
final var buffer = DoubleBuffer.wrap(array);
{
final double[] a = {0.1};
final var that = DoubleBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 1
}
{
final double[] a = {0.1, 0.2};
final var that = DoubleBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 2
}
{
final double[] a = {0.1, 0.2, 0.3};
final var that = DoubleBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 3
}
{
final double[] a = {0.1, 0.2, 0.3, 0.4};
final var that = DoubleBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // -1
}
{
final double[] a = {0.1, 0.2, 0.3, 0.4, 0.5};
final var that = DoubleBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 4
}
{
final double[] a = {0.1, 0.2, 0.999};
final var that = DoubleBuffer.wrap(a); // 2
System.out.println(buffer.mismatch(that));
}
{
final double[] a = {-0.9, -0.8};
final var that = DoubleBuffer.wrap(a);
System.out.println(buffer.mismatch(that)); // 0
}
abstract ByteOrder order ()
final var os = System.getProperty("os.name");
System.out.println(os); // Windows 10
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.order()); // LITTLE_ENDIAN
final DoubleBuffer position (int newPosition)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.position()); // 0
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.position()); // 1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.position()); // 2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.position()); // 3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.position()); // 5
System.out.println(buffer.position(3)); // java.nio.HeapDoubleBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.position()); // 4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.position()); // 5
abstract DoubleBuffer put (double d)
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.put(0.1)); // java.nio.HeapDoubleBuffer[pos=1 lim=5 cap=5]
System.out.println(buffer.put(0.2)); // java.nio.HeapDoubleBuffer[pos=2 lim=5 cap=5]
System.out.println(buffer.put(0.3)); // java.nio.HeapDoubleBuffer[pos=3 lim=5 cap=5]
System.out.println(buffer.put(0.4)); // java.nio.HeapDoubleBuffer[pos=4 lim=5 cap=5]
System.out.println(buffer.put(0.5)); // java.nio.HeapDoubleBuffer[pos=5 lim=5 cap=5]
System.out.println(buffer.position()); // 5
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5]
}
final DoubleBuffer put (double[] src)
final var buffer = DoubleBuffer.allocate(6);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
{
final double[] src = {0.1};
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=1 lim=6 cap=6]
}
{
final double[] src = {0.2, 0.3};
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=3 lim=6 cap=6]
}
{
final double[] src = {0.4, 0.5, 0.6};
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=6 lim=6 cap=6]
}
System.out.println(buffer.position()); // 6
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
}
DoubleBuffer put (double[] src, int offset, int length)
final var array = new double[6];
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
final double[] src = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var ret1 = buffer.put(src, 0, 1);
System.out.println(ret1); // java.nio.HeapDoubleBuffer[pos=1 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.0, 0.0, 0.0, 0.0, 0.0]
final var ret2 = buffer.put(src, 1, 2);
System.out.println(ret2); // java.nio.HeapDoubleBuffer[pos=3 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0, 0.0]
final var ret3 = buffer.put(src, 3, 3);
System.out.println(ret3); // java.nio.HeapDoubleBuffer[pos=6 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
System.out.println(buffer.position()); // 6
abstract DoubleBuffer put (int index, double d)
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
System.out.println(buffer.put(0, 0.1)); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(1, 0.2)); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(2, 0.3)); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(3, 0.4)); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.put(4, 0.5)); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5]
}
DoubleBuffer put (int index, double[] src)
final var buffer = DoubleBuffer.allocate(6);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
{
final double[] src = {0.1};
final var ret = buffer.put(0, src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
}
{
final double[] src = {0.2, 0.3};
final var ret = buffer.put(1, src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
}
{
final double[] src = {0.4, 0.5, 0.6};
final var ret = buffer.put(3, src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
}
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
}
DoubleBuffer put (int index, double[] src, int offset, int length)
final var array = new double[6];
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
final double[] src = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.0, 0.0, 0.0, 0.0, 0.0]
final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0, 0.0]
final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
System.out.println(buffer.position()); // 0
DoubleBuffer put (int index, DoubleBuffer src, int offset, int length)
final var array = new double[6];
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 6
System.out.println(buffer.position()); // 0
final double[] srcArray = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};
final var src = DoubleBuffer.wrap(srcArray);
final var ret1 = buffer.put(0, src, 0, 1);
System.out.println(ret1); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.0, 0.0, 0.0, 0.0, 0.0]
final var ret2 = buffer.put(1, src, 1, 2);
System.out.println(ret2); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.0, 0.0, 0.0]
final var ret3 = buffer.put(3, src, 3, 3);
System.out.println(ret3); // java.nio.HeapDoubleBuffer[pos=0 lim=6 cap=6]
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
System.out.println(buffer.position()); // 0
DoubleBuffer put (DoubleBuffer src)
final var buffer = DoubleBuffer.allocate(5);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.position()); // 0
{
final double[] array = {0.1, 0.2};
final var src = DoubleBuffer.wrap(array);
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=2 lim=5 cap=5]
}
{
final double[] array = {0.3, 0.4, 0.5};
final var src = DoubleBuffer.wrap(array);
final var ret = buffer.put(src);
System.out.println(ret); // java.nio.HeapDoubleBuffer[pos=5 lim=5 cap=5]
}
System.out.println(buffer.position()); // 5
if (buffer.hasArray()) {
final var array = buffer.array();
System.out.println(Arrays.toString(array)); // [0.1, 0.2, 0.3, 0.4, 0.5]
}
final DoubleBuffer reset ()
Please see mark().
final DoubleBuffer rewind ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(2);
buffer.limit(4);
System.out.println(buffer.position()); // 2
System.out.println(buffer.limit()); // 4
System.out.println(buffer.rewind()); // java.nio.HeapDoubleBuffer[pos=0 lim=4 cap=5]
System.out.println(buffer.position()); // 0
System.out.println(buffer.limit()); // 4
abstract DoubleBuffer slice ()
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
buffer.position(1);
buffer.limit(4);
final var sliced = buffer.slice();
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=1 lim=4 cap=5]
System.out.println(sliced); // java.nio.HeapDoubleBuffer[pos=0 lim=3 cap=3]
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.hasRemaining()); // false
System.out.println(sliced.get()); // 0.2
System.out.println(sliced.get()); // 0.3
System.out.println(sliced.get()); // 0.4
System.out.println(sliced.hasRemaining()); // false
abstract DoubleBuffer slice (int index, int length)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
final var sliced = buffer.slice(2, 3);
System.out.println(buffer); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
System.out.println(sliced); // java.nio.HeapDoubleBuffer[pos=0 lim=3 cap=3]
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.hasRemaining()); // false
System.out.println(sliced.get()); // 0.3
System.out.println(sliced.get()); // 0.4
System.out.println(sliced.get()); // 0.5
System.out.println(sliced.hasRemaining()); // false
String toString ()
final var buffer = DoubleBuffer.allocate(5)
.limit(4)
.position(2);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 4
System.out.println(buffer.position()); // 2
final var str1 = buffer.toString();
System.out.println(str1); // java.nio.HeapDoubleBuffer[pos=2 lim=4 cap=5]
buffer.clear();
final var str2 = buffer.toString();
System.out.println(str2); // java.nio.HeapDoubleBuffer[pos=0 lim=5 cap=5]
static DoubleBuffer wrap (double[] array)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5};
final var buffer = DoubleBuffer.wrap(array);
System.out.println(buffer.capacity()); // 5
System.out.println(buffer.limit()); // 5
System.out.println(buffer.position()); // 0
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 0.1
System.out.println(buffer.get()); // 0.2
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
}
static DoubleBuffer wrap (double[] array, int offset, int length)
final double[] array = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7};
final var buffer = DoubleBuffer.wrap(array, 2, 4);
System.out.println(buffer.capacity()); // 7
System.out.println(buffer.limit()); // 6
System.out.println(buffer.position()); // 2
if (buffer.hasArray()) {
System.out.println(buffer.arrayOffset()); // 0
System.out.println(buffer.get()); // 0.3
System.out.println(buffer.get()); // 0.4
System.out.println(buffer.get()); // 0.5
System.out.println(buffer.get()); // 0.6
}
Methods declared in Buffer
capacity, hasRemaining, isReadOnly, limit, position, remaining
Please see the link below.