FloatBuffer duplicate() method in Java with Examples

The duplicate() method of java.nio.FloatBuffer 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 FloatBuffer duplicate()

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

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

Examples 1: Using direct floatbuffer

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Original FloatBuffer:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Duplicate FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Examples 2: Using read-onlyfloatbuffer

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// duplicate() method
// using read-onlyfloatbuffer
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer
        int capacity = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity);
  
            // putting the value in floatbuffer
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // print the Original FloatBuffer
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb1.array()));
  
            // Creating a read-only copy of FloatBuffer
            // using asReadOnlyBuffer() method
            FloatBuffer readonly = fb1.asReadOnlyBuffer();
  
            // print the read-only copy of FloatBuffer
            System.out.print("\nread-only FloatBuffer:  ");
            while (readonly.hasRemaining())
                System.out.print(readonly.get() + ", ");
            System.out.println("");
  
            // Rewinding the readonly FloatBuffer
            readonly.rewind();
  
            // Creating a duplicate copy of FloatBuffer
            // using duplicate() method
            FloatBuffer fb2 = readonly.duplicate();
  
            // print the duplicate copy of FloatBuffer
            System.out.print("\nduplicate copy of read-only FloatBuffer:  ");
  
            while (fb2.hasRemaining())
                System.out.print(fb2.get() + ", ");
            System.out.println("");
        }
  
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

chevron_right


Output:

Original FloatBuffer:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

read-only FloatBuffer:  8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 

duplicate copy of read-only FloatBuffer:  8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,


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