ByteBuffer duplicate() method in Java with Examples
Last Updated :
06 Sep, 2022
The duplicate() method of java.nio.ByteBuffer class is used to create a new byte buffer that shares this 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 ByteBuffer duplicate()
Return Value: This method returns the new byte buffer which is carrying the previous byte buffer content. Below are the examples to illustrate the duplicate() method: Examples 1: Using direct ByteBuffer
Java
// Java program to demonstrate
// duplicate() method
// Using direct ByteBuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 4;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity);
// putting the int to byte typecast value in ByteBuffer
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
bb1.put((byte)50);
bb1.rewind();
// print the Original ByteBuffer
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb1.array()));
// Creating a duplicate copy of ByteBuffer
// using duplicate() method
ByteBuffer bb2 = bb1.duplicate();
// print the duplicate copy of ByteBuffer
System.out.print("\nDuplicate ByteBuffer: "
+ Arrays.toString(bb2.array()));
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:Original ByteBuffer: [20, 30, 40, 50]
Duplicate ByteBuffer: [20, 30, 40, 50]
Examples 2: Using read-only ByteBuffer
Java
// Java program to demonstrate
// duplicate() method
// using read-only ByteBuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 4;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb1 = ByteBuffer.allocate(capacity);
// putting the int to byte typecast value in ByteBuffer
bb1.put((byte)20);
bb1.put((byte)30);
bb1.put((byte)40);
bb1.put((byte)50);
bb1.rewind();
// print the Original ByteBuffer
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb1.array()));
// Creating a read-only copy of ByteBuffer
// using asReadOnlyBuffer() method
ByteBuffer readonly = bb1.asReadOnlyBuffer();
// print the read-only copy of ByteBuffer
System.out.print("\nRead-only ByteBuffer: ");
while (readonly.hasRemaining())
System.out.print(readonly.get() + ", ");
System.out.println("");
// Rewinding the readonly ByteBuffer
readonly.rewind();
// Creating a duplicate copy of ByteBuffer
// using duplicate() method
ByteBuffer bb2 = readonly.duplicate();
// print the duplicate copy of ByteBuffer
System.out.print("\nDuplicate copy of read-only ByteBuffer: ");
while (bb2.hasRemaining())
System.out.print(bb2.get() + ", ");
System.out.println("");
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:Original ByteBuffer: [20, 30, 40, 50]
Read-only ByteBuffer: 20, 30, 40, 50,
Duplicate copy of read-only ByteBuffer: 20, 30, 40, 50,
Similar Reads
Buffer duplicate() method in Java with Examples The duplicate() method of java.nio.Buffer class is used to create a new buffer that shares this 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
3 min read
DoubleBuffer duplicate() method in Java with Examples 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,
3 min read
ByteBuffer compact() method in Java with Examples The compact() method of java.nio.ByteBuffer class is used to compact the given buffer.The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied
3 min read
ByteBuffer equals() method in Java with Examples The equals() method of java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object. Two byte buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con
3 min read
ByteBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another. Two byte buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of byte el
4 min read
ByteBuffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example: buf.clear(); // Prepa
2 min read
ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
Buffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example: buf.clear(); // Prepa
3 min read
Buffer capacity() method in Java with Examples The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: Java // Java program to demonstrate // capacity() method impo
2 min read
ByteBuffer wrap() method in Java with Examples wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte 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 a
4 min read