Buffer duplicate() method in Java with Examples Last Updated : 06 Sep, 2019 Comments Improve Suggest changes Like Article Like Report 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 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 Buffer duplicate() Return Value: This method returns the new buffer which is carrying the previous buffer content. Below are the examples to illustrate the duplicate() method: Examples 1: Using direct ByteBuffer Java // 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 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 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, Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#duplicate-- Comment More infoAdvertise with us Next Article Buffer duplicate() method in Java with Examples rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-Buffer Practice Tags : Java Similar Reads ByteBuffer duplicate() method in Java with Examples 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, 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 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 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 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 CharBuffer clear() methods in Java with Examples The clear() method of java.nio.CharBuffer 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: // Prepare buffer for 2 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 CharBuffer chars() methods in Java with Examples The chars() method of java.nio.CharBuffer Class is used to return a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted. The stream binds to this sequence when the terminal stream operation commences (specific 2 min read BufferedWriter close() method in Java with Examples The close() method of BufferedWriter class in Java is used to flush the characters from the buffer stream and then close it. Once the stream is closed further calling the methods like write() and append() will throw the exception. Syntax: public void close() Parameters: This method does not accept a 2 min read DoubleBuffer equals() method in Java with Examples The equals() method of java.nio.DoubleBuffer Class is used to check whether or not the given buffer is equal to another object. Two double 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, 3 min read Like