LongBuffer duplicate() method in Java
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
LongBuffer ib1 = LongBuffer.allocate(capacity);
ib1.put( 8 );
ib1.put( 2 , 9 );
ib1.rewind();
System.out.println( "Original LongBuffer: "
+ Arrays.toString(ib1.array()));
LongBuffer ib2 = ib1.duplicate();
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" );
}
}
}
|
Output:
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
LongBuffer ib1 = LongBuffer.allocate(capacity);
ib1.put( 8 );
ib1.put( 2 , 9 );
ib1.rewind();
System.out.println( "Original LongBuffer: "
+ Arrays.toString(ib1.array()));
LongBuffer readonly = ib1.asReadOnlyBuffer();
System.out.print( "read-only LongBuffer: " );
while (readonly.hasRemaining())
System.out.print(readonly.get() + ", " );
System.out.println( "" );
readonly.rewind();
LongBuffer ib2 = readonly.duplicate();
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" );
}
}
}
|
Output:
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,
Last Updated :
18 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...