IntBuffer duplicate() method in Java with Examples
Last Updated :
29 Jul, 2019
The
duplicate() method of
java.nio.IntBuffer Class is used to create a new IntBuffer that shares the given buffer's content, identically in all aspects.
Syntax:
public abstract IntBuffer duplicate()
Return Value: This method returns the
new IntBuffer which is carrying the previous IntBuffer's content
Below are the examples to illustrate the
duplicate() method:
Examples 1: Using direct IntBuffer
Java
// Java program to demonstrate duplicate() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 10;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib1.put(8);
ib1.put(2, 9);
ib1.rewind();
// print the Original IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib1.array()));
// Creating a duplicate copy of IntBuffer
// using duplicate() method
IntBuffer ib2 = ib1.duplicate();
// print the duplicate copy of IntBuffer
System.out.print("Duplicate IntBuffer: "
+ Arrays.toString(ib2.array()));
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
Original IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Duplicate IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Examples 2: Using read-onlyintbuffer
Java
// Java program to demonstrate
// duplicate() method
// using read-onlyIntbuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the IntBuffer
int capacity = 10;
// Creating the IntBuffer
try {
// creating object of Intbuffer
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity);
// putting the value in Intbuffer
ib1.put(8);
ib1.put(2, 9);
ib1.rewind();
// print the Original IntBuffer
System.out.println("Original IntBuffer: "
+ Arrays.toString(ib1.array()));
// Creating a read-only copy of IntBuffer
// using asReadOnlyBuffer() method
IntBuffer readonly = ib1.asReadOnlyBuffer();
// print the read-only copy of IntBuffer
System.out.print("read-only IntBuffer: ");
while (Readonly.hasRemaining())
System.out.print(readonly.get() + ", ");
System.out.println("");
// Rewinding the readonly IntBuffer
readonly.rewind();
// Creating a duplicate copy of IntBuffer
// using duplicate() method
IntBuffer ib2 = readonly.duplicate();
// print the duplicate copy of IntBuffer
System.out.print("Duplicate copy of read-only IntBuffer: ");
while (ib2.hasRemaining())
System.out.print(ib2.get() + ", ");
System.out.println("");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
Output:
Original IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Read-only IntBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,
Duplicate copy of read-only IntBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,
Similar Reads
FloatBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.FloatBuffer 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,
3 min read
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
ShortBuffer duplicate() method in Java With Examples The duplicate() method of java.nio.ShortBuffer Class creates a new short 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
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,
3 min read