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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
        }
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 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 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");
        }
    }
}

chevron_right


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,


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.