Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

DoubleBuffer duplicate() method in Java with Examples

  • Last Updated : 18 Oct, 2019

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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:




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




// 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 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 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");
        }
    }
}
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
Recommended Articles
Page :

Start Your Coding Journey Now!