The duplicate() method of java.nio.LongBuffer Class is used to Create a new Long buffer that shares the given buffer’s content.
Syntax:
public abstract LongBuffer duplicate()
Return Value: This method returns the new Long buffer which is carrying the previous Long buffer content
Below are the examples to illustrate the duplicate() method:
Examples 1: Using direct Longbuffer
// Java program to demonstrate // duplicate() method // Using direct Longbuffer import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the LongBuffer int capacity = 10; // Creating the LongBuffer try { // creating object of Longbuffer // and allocating size capacity LongBuffer ib1 = LongBuffer.allocate(capacity); // putting the value in Longbuffer ib1.put(8); ib1.put(2, 9); ib1.rewind(); // prLong the Original LongBuffer System.out.println("Original LongBuffer: " + Arrays.toString(ib1.array())); // Creating a duplicate copy of LongBuffer // using duplicate() method LongBuffer ib2 = ib1.duplicate(); // prLong the duplicate copy of LongBuffer System.out.println("Duplicate LongBuffer: " + Arrays.toString(ib2.array())); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
Original LongBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] Duplicate LongBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Examples 2: Using read-onlyLongbuffer
// Java program to demonstrate // duplicate() method // using read-onlyLongbuffer import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the LongBuffer int capacity = 10; // Creating the LongBuffer try { // creating object of Longbuffer // and allocating size capacity LongBuffer ib1 = LongBuffer.allocate(capacity); // putting the value in Longbuffer ib1.put(8); ib1.put(2, 9); ib1.rewind(); // prLong the Original LongBuffer System.out.println("Original LongBuffer: " + Arrays.toString(ib1.array())); // Creating a read-only copy of LongBuffer // using asReadOnlyBuffer() method LongBuffer readonly = ib1.asReadOnlyBuffer(); // prLong the read-only copy of LongBuffer System.out.print("read-only LongBuffer: "); while (readonly.hasRemaining()) System.out.print(readonly.get() + ", "); System.out.println(""); // Rewinding the readonly LongBuffer readonly.rewind(); // Creating a duplicate copy of LongBuffer // using duplicate() method LongBuffer ib2 = readonly.duplicate(); // prLong the duplicate copy of LongBuffer System.out.print("duplicate copy of read-only LongBuffer: "); while (ib2.hasRemaining()) System.out.print(ib2.get() + ", "); System.out.println(""); } catch (IllegalArgumentException e) { System.out.println("IllegalArgumentException catched"); } catch (ReadOnlyBufferException e) { System.out.println("ReadOnlyBufferException catched"); } } } |
Original LongBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0] read-only LongBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0, duplicate copy of read-only LongBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,
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.
Recommended Posts:
- LongBuffer asReadOnlyBuffer() method in Java
- LongBuffer allocate() method in Java
- LongBuffer slice() method in Java
- LongBuffer equals() method in Java
- LongBuffer compact() method in Java
- LongBuffer hasArray() method in Java
- LongBuffer arrayOffset() method in Java
- LongBuffer wrap() method in Java
- LongBuffer flip() method in Java with Examples
- LongBuffer clear() method in Java with Examples
- LongBuffer mark() method in Java with Examples
- LongBuffer reset() method in Java with Examples
- LongBuffer rewind() method in Java with Examples
- LongBuffer limit() method in Java with Examples
- LongBuffer get() methods in Java
- LongBuffer put() methods in Java
- CharBuffer duplicate() method in Java
- IntBuffer duplicate() method in Java with Examples
- FloatBuffer duplicate() method in Java with Examples
- Buffer duplicate() method in Java with Examples
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

