FloatBuffer duplicate() method in Java with Examples
The duplicate() method of java.nio.FloatBuffer 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, 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 FloatBuffer duplicate()
Return Value: This method returns the new float buffer which is carrying the previous float buffer content
Below are the examples to illustrate the duplicate() method:
Examples 1: Using direct floatbuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
FloatBuffer fb1 = FloatBuffer.allocate(capacity);
fb1.put( 8 .56F);
fb1.put( 2 , 9 .61F);
fb1.rewind();
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb1.array()));
FloatBuffer fb2 = fb1.duplicate();
System.out.print( "\nDuplicate FloatBuffer: "
+ Arrays.toString(fb2.array()));
}
catch (IllegalArgumentException e) {
System.out.println( "IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "ReadOnlyBufferException catched" );
}
}
}
|
Output:
Original FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Duplicate FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Examples 2: Using read-onlyfloatbuffer
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
FloatBuffer fb1 = FloatBuffer.allocate(capacity);
fb1.put( 8 .56F);
fb1.put( 2 , 9 .61F);
fb1.rewind();
System.out.println( "Original FloatBuffer: "
+ Arrays.toString(fb1.array()));
FloatBuffer readonly = fb1.asReadOnlyBuffer();
System.out.print( "\nread-only FloatBuffer: " );
while (readonly.hasRemaining())
System.out.print(readonly.get() + ", " );
System.out.println( "" );
readonly.rewind();
FloatBuffer fb2 = readonly.duplicate();
System.out.print( "\nduplicate copy of read-only FloatBuffer: " );
while (fb2.hasRemaining())
System.out.print(fb2.get() + ", " );
System.out.println( "" );
}
catch (IllegalArgumentException e) {
System.out.println( "IllegalArgumentException catched" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "ReadOnlyBufferException catched" );
}
}
}
|
Output:
Original FloatBuffer: [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
read-only FloatBuffer: 8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
duplicate copy of read-only FloatBuffer: 8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
Last Updated :
17 Oct, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...