Open In App

DoubleBuffer duplicate() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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,


Last Updated : 18 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads