CharBuffer duplicate() method in Java

The duplicate() method of java.nio.CharBuffer Class is used to Create a new char 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 CharBuffer duplicate()

Return Value: This method returns the new char buffer which is carrying the previous char buffer content



Below are the examples to illustrate the duplicate() method:

Example 1: Using direct charbuffer

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// duplicate() method
// Using direct intbuffer
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the CharBuffer
        int capacity = 10;
  
        // Creating the CharBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            CharBuffer cb1 = CharBuffer.allocate(capacity);
  
            // putting the value in Intbuffer
            cb1.put('a');
            cb1.put(2, 'b');
            cb1.rewind();
  
            // print the Original CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb1.array()));
  
            // Creating a duplicate copy of CharBuffer
            // using duplicate() method
            CharBuffer cb2 = cb1.duplicate();
  
            // print the duplicate copy of CharBuffer
            System.out.print("Duplicate CharBuffer: "
                             + Arrays.toString(cb2.array()));
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

chevron_right


Output:

Original CharBuffer: [a, , b, , , , , , , ]
Duplicate CharBuffer: [a, , b, , , , , , , ]

Example 2: Using read-onlyintbuffer

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 CharBuffer
        int capacity = 10;
  
        // Creating the CharBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            CharBuffer cb1 = CharBuffer.allocate(capacity);
  
            // putting the value in Intbuffer
            cb1.put('a');
            cb1.put(2, 'b');
            cb1.rewind();
  
            // print the Original CharBuffer
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb1.array()));
  
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer readonly = cb1.asReadOnlyBuffer();
  
            // print the read-only copy of CharBuffer
            System.out.print("read-only CharBuffer: ");
            while (readonly.hasRemaining())
                System.out.print(readonly.get() + ", ");
            System.out.println("");
  
            // Rewinding the readonly CharBuffer
            readonly.rewind();
  
            // Creating a duplicate copy of CharBuffer
            // using duplicate() method
            CharBuffer cb2 = readonly.duplicate();
  
            // print the duplicate copy of CharBuffer
            System.out.print("duplicate copy of read-only CharBuffer: ");
  
            while (cb2.hasRemaining())
                System.out.print(cb2.get() + ", ");
            System.out.println("");
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

chevron_right


Output:

Original CharBuffer: [a, , b, , , , , , , ]
read-only CharBuffer: a, , b, , , , , , , , 
duplicate copy of read-only CharBuffer: a, , b, , , , , , , , 

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : nidhi_biet