DoubleBuffer duplicate() method in Java with Examples
Last Updated :
18 Oct, 2019
The
duplicate() method of
java.nio.DoubleBuffer Class is used to Create a new float buffer that shares the given buffer’s content.
The content of the new buffer will be that of this buffer. Changes to this buffer’s content will be visible in the new buffer, and vice versa; the two buffers’ position, limit, and mark values will be independent.
The new buffer’s capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
Syntax :
public abstract DoubleBuffer duplicate()
Return Value: This method returns
the new double buffer which is carrying the previous double buffer content
Below are the examples to illustrate the duplicate() method:
Program 1:
Java
// Java program to demonstrate
// duplicate() method
// Using direct Doublebuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 10;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity
DoubleBuffer db1 = DoubleBuffer.allocate(capacity);
// putting the value in Doublebuffer
db1.put(8.56F);
db1.put(2, 9.61F);
db1.rewind();
// print the Original DoubleBuffer
System.out.println("Original DoubleBuffer: "
+ Arrays.toString(db1.array()));
// Creating a duplicate copy of DoubleBuffer
// using duplicate() method
DoubleBuffer db2 = db1.duplicate();
// print the duplicate copy of DoubleBuffer
System.out.print("\nDuplicate DoubleBuffer: "
+ Arrays.toString(db2.array()));
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
Original DoubleBuffer: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Duplicate DoubleBuffer: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Example 2:
Java
// Java program to demonstrate
// duplicate() method
// using read-onlyDoublebuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
int capacity = 10;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size capacity
DoubleBuffer db1 = DoubleBuffer.allocate(capacity);
// putting the value in Doublebuffer
db1.put(8.56F);
db1.put(2, 9.61F);
db1.rewind();
// print the Original DoubleBuffer
System.out.println("Original DoubleBuffer: "
+ Arrays.toString(db1.array()));
// Creating a read-only copy of DoubleBuffer
// using asReadOnlyBuffer() method
DoubleBuffer readonly = db1.asReadOnlyBuffer();
// print the read-only copy of DoubleBuffer
System.out.print("\nread-only DoubleBuffer: ");
while (readonly.hasRemaining())
System.out.print(readonly.get() + ", ");
System.out.println("");
// Rewinding the readonly DoubleBuffer
readonly.rewind();
// Creating a duplicate copy of DoubleBuffer
// using duplicate() method
DoubleBuffer db2 = readonly.duplicate();
// print the duplicate copy of DoubleBuffer
System.out.print("\nduplicate copy of read-only DoubleBuffer: ");
while (db2.hasRemaining())
System.out.print(db2.get() + ", ");
System.out.println("");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
Original DoubleBuffer: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
read-only DoubleBuffer: 8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
duplicate copy of read-only DoubleBuffer: 8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
Similar Reads
Buffer duplicate() method in Java with Examples The duplicate() method of java.nio.Buffer class is used to create a new buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark
3 min read
ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit,
3 min read
DoubleBuffer equals() method in Java with Examples The equals() method of java.nio.DoubleBuffer Class is used to check whether or not the given buffer is equal to another object. Two double buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements,
3 min read
DoubleBuffer get() methods in Java with Examples The get() method of java.nio.DoubleBuffer Class is used to reads the double at the given bufferâs current position, and then increments the position. Syntax: public abstract double get() Return Value: This method returns the double value at the bufferâs current position. Exception: This method throw
3 min read
DoubleBuffer wrap() method in Java with Examples wrap(double[] array) The wrap() method of java.nio.DoubleBuffer Class is used to wraps a double array into a buffer. The new buffer will be backed by the given double array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new bufferâs capacity and limit w
4 min read