DoubleBuffer duplicate() method in Java with Examples
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
DoubleBuffer db1 = DoubleBuffer.allocate(capacity);
db1.put( 8 .56F);
db1.put( 2 , 9 .61F);
db1.rewind();
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(db1.array()));
DoubleBuffer db2 = db1.duplicate();
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
DoubleBuffer db1 = DoubleBuffer.allocate(capacity);
db1.put( 8 .56F);
db1.put( 2 , 9 .61F);
db1.rewind();
System.out.println( "Original DoubleBuffer: "
+ Arrays.toString(db1.array()));
DoubleBuffer readonly = db1.asReadOnlyBuffer();
System.out.print( "\nread-only DoubleBuffer: " );
while (readonly.hasRemaining())
System.out.print(readonly.get() + ", " );
System.out.println( "" );
readonly.rewind();
DoubleBuffer db2 = readonly.duplicate();
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,
Last Updated :
18 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...