Open In App

IntBuffer duplicate() method in Java with Examples

Last Updated : 29 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The duplicate() method of java.nio.IntBuffer Class is used to create a new IntBuffer that shares the given buffer’s content, identically in all aspects.

Syntax:

public abstract IntBuffer duplicate()

Return Value: This method returns the new IntBuffer which is carrying the previous IntBuffer’s content

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

Examples 1: Using direct IntBuffer




// Java program to demonstrate duplicate() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the IntBuffer
        int capacity = 10;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity);
  
            // putting the value in Intbuffer
            ib1.put(8);
            ib1.put(2, 9);
            ib1.rewind();
  
            // print the Original IntBuffer
            System.out.println("Original IntBuffer:  "
                               + Arrays.toString(ib1.array()));
  
            // Creating a duplicate copy of IntBuffer
            // using duplicate() method
            IntBuffer ib2 = ib1.duplicate();
  
            // print the duplicate copy of IntBuffer
            System.out.print("Duplicate IntBuffer: "
                             + Arrays.toString(ib2.array()));
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}


Output:

Original IntBuffer:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Duplicate IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

Examples 2: Using read-onlyintbuffer




// Java program to demonstrate
// duplicate() method
// using read-onlyIntbuffer
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the IntBuffer
        int capacity = 10;
  
        // Creating the IntBuffer
        try {
  
            // creating object of Intbuffer
            // and allocating size capacity
            IntBuffer ib1 = IntBuffer.allocate(capacity);
  
            // putting the value in Intbuffer
            ib1.put(8);
            ib1.put(2, 9);
            ib1.rewind();
  
            // print the Original IntBuffer
            System.out.println("Original IntBuffer:  "
                               + Arrays.toString(ib1.array()));
  
            // Creating a read-only copy of IntBuffer
            // using asReadOnlyBuffer() method
            IntBuffer readonly = ib1.asReadOnlyBuffer();
  
            // print the read-only copy of IntBuffer
            System.out.print("read-only IntBuffer:  ");
  
            while (Readonly.hasRemaining())
                System.out.print(readonly.get() + ", ");
  
            System.out.println("");
  
            // Rewinding the readonly IntBuffer
            readonly.rewind();
  
            // Creating a duplicate copy of IntBuffer
            // using duplicate() method
            IntBuffer ib2 = readonly.duplicate();
  
            // print the duplicate copy of IntBuffer
            System.out.print("Duplicate copy of read-only IntBuffer:  ");
  
            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");
        }
    }
}


Output:

Original IntBuffer:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
Read-only IntBuffer:  8, 0, 9, 0, 0, 0, 0, 0, 0, 0, 
Duplicate copy of read-only IntBuffer:  8, 0, 9, 0, 0, 0, 0, 0, 0, 0,


Similar Reads

IntBuffer limit() method in Java with Examples
The limit() method of java.nio.IntBuffer Class is used to modify this IntBuffer's limit. This method takes the limit to be set as the parameter and sets that as the new limit of this Buffer. If the mark of this Buffer is already defined and is larger than the new specified limit, then this new limit is not set and discarded. Syntax: public final In
3 min read
IntBuffer rewind() method in Java with Examples
The rewind() method of java.nio.IntBuffer Class is used to rewind this buffer. The position is set to zero and the mark is discarded. Invoke this method before a sequence of channel-write or get operations, assuming that the limit has already been set appropriately. Invoking this method neither changes nor discards the mark's value. Syntax: public
2 min read
IntBuffer clear() method in Java with Examples
The clear() method of java.nio.IntBuffer Class is used to clear this buffer. While clearing this buffer following changes are done: the position is set to zero the limit is set to the capacity the mark is discarded. Syntax: public final IntBuffer clear() Parameter: The method do not take any parameter. Return Value: This method returns this IntBuff
2 min read
IntBuffer mark() method in Java with Examples
The mark() method of java.nio.IntBuffer Class is used to mark the current position of this IntBuffer as the mark of this buffer. Syntax: public final IntBuffer mark() Parameter: The method do not take any parameter. Return Value: This method returns this IntBuffer after setting the buffer's mark at the current position. Below are the examples to il
2 min read
IntBuffer flip() method in Java with Examples
The flip() method of java.nio.IntBuffer Class is used to flip this buffer. By flipping this buffer, it meant that the buffer will be trimmed to the current position and then the position will be changed to zero. During this process, if any mark is there on the buffer, then that mark will be automatically discarded. Syntax: public final IntBuffer fl
2 min read
IntBuffer reset() methods in Java with Examples
The reset() method of java.nio.IntBuffer Class is used to reset this buffer's position to the previously-marked position. Invoking this method neither changes nor discards the mark's value. Syntax: public final IntBuffer reset() Parameter: The method do not take any parameter. Return Value: This method returns this buffer. Below are the examples to
2 min read
java.nio.IntBuffer Class in Java
IntBuffer holds a sequence of integer values to be used in an I/O operation. The IntBuffer class provides the following four categories of operations upon long buffers: Absolute and relative get method that read single ints.Absolute and relative put methods that write single ints.Relative bulk put and get methods that transfer contiguous sequences
4 min read
IntBuffer hasArray() method in Java
The hasArray() method of java.nio.IntBuffer class is used to ensure whether or not the given buffer is backed by an accessible int array. It returns true if there is an accessible backing array to this buffer, else it returns false. If this method returns true, then the array() and arrayOffset() methods may safely be invoked, as they work on the ba
2 min read
IntBuffer array() method in Java
The array() method of java.nio.IntBuffer Class is used to Return the int array that backs this buffer. Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa. Invoke() the hasArray() method are used before invoking this method in order to ensure that this buffer has an accessible backing array
3 min read
IntBuffer wrap() method in Java
wrap(int[] array) The wrap() method of java.nio.IntBuffer Class is used to wrap a int array into a buffer. The new buffer will be backed by the given int array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero, and its mark wi
4 min read
Practice Tags :