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, 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 ByteBuffer duplicate()
Return Value: This method returns the new byte buffer which is carrying the previous byte buffer content.
Below are the examples to illustrate the duplicate() method:
Examples 1: Using direct ByteBuffer
// Java program to demonstrate // duplicate() method // Using direct ByteBuffer import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb1 = ByteBuffer.allocate(capacity); // putting the int to byte typecast value value in ByteBuffer bb1.put((byte)20); bb1.put((byte)30); bb1.put((byte)40); bb1.put((byte)50); bb1.rewind(); // print the Original ByteBuffer System.out.println("Original ByteBuffer: " + Arrays.toString(bb1.array())); // Creating a duplicate copy of ByteBuffer // using duplicate() method ByteBuffer bb2 = bb1.duplicate(); // print the duplicate copy of ByteBuffer System.out.print("\nDuplicate ByteBuffer: " + Arrays.toString(bb2.array())); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception thrown : " + e); } } } |
Original ByteBuffer: [20, 30, 40, 50] Duplicate ByteBuffer: [20, 30, 40, 50]
Examples 2: Using read-only ByteBuffer
// Java program to demonstrate // duplicate() method // using read-only ByteBuffer import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb1 = ByteBuffer.allocate(capacity); // putting the int to byte typecast value in ByteBuffer bb1.put((byte)20); bb1.put((byte)30); bb1.put((byte)40); bb1.put((byte)50); bb1.rewind(); // print the Original ByteBuffer System.out.println("Original ByteBuffer: " + Arrays.toString(bb1.array())); // Creating a read-only copy of ByteBuffer // using asReadOnlyBuffer() method ByteBuffer readonly = bb1.asReadOnlyBuffer(); // print the read-only copy of ByteBuffer System.out.print("\nRead-only ByteBuffer: "); while (readonly.hasRemaining()) System.out.print(readonly.get() + ", "); System.out.println(""); // Rewinding the readonly ByteBuffer readonly.rewind(); // Creating a duplicate copy of ByteBuffer // using duplicate() method ByteBuffer bb2 = readonly.duplicate(); // print the duplicate copy of ByteBuffer System.out.print("\nDuplicate copy of read-only ByteBuffer: "); while (bb2.hasRemaining()) System.out.print(bb2.get() + ", "); System.out.println(""); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception thrown : " + e); } } } |
Original ByteBuffer: [20, 30, 40, 50] Read-only ByteBuffer: 20, 30, 40, 50, Duplicate copy of read-only ByteBuffer: 20, 30, 40, 50,
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- ByteBuffer get() method in Java with Examples
- ByteBuffer allocateDirect() method in Java with Examples
- ByteBuffer asIntBuffer() method in Java with Examples
- ByteBuffer slice() method in Java with Examples
- ByteBuffer asDoubleBuffer() method in Java with Examples
- ByteBuffer asFloatBuffer() method in Java with Examples
- ByteBuffer arrayOffset() method in Java with Examples
- ByteBuffer asLongBuffer() method in Java with Examples
- ByteBuffer asReadOnlyBuffer() method in Java with Examples
- ByteBuffer compact() method in Java with Examples
- ByteBuffer equals() method in Java with Examples
- ByteBuffer compareTo() method in Java With Examples
- ByteBuffer getShort() method in Java with Examples
- ByteBuffer getInt() method in Java with Examples
- ByteBuffer asShortBuffer() method in Java with Examples
- ByteBuffer order() method in Java with Examples
- ByteBuffer getFloat() method in Java with Examples
- ByteBuffer getDouble() method in Java with Examples
- ByteBuffer getChar() method in Java with Examples
- ByteBuffer getLong() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

