The position(int newPosition) method of java.nio.Buffer Class is used to set this buffer’s position. If the mark is defined and larger than the new position then it is discarded.
Syntax:
public Buffer position(int newPosition)
Parameters: This method takes the newPosition as parameter which is the new position value. It must be non-negative and no larger than the current limit.
Return Value: This method returns this buffer.
Below are the examples to illustrate the position() method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
byte [] barr = { 10 , 20 , 30 , 40 };
ByteBuffer bb = ByteBuffer.wrap(barr);
Buffer bb1 = (Buffer)bb;
bb1.position( 2 );
bb1.mark();
bb1.position( 4 );
System.out.println( "position before reset: "
+ bb.position());
bb1.reset();
System.out.println( "position after reset: "
+ bb1.position());
}
}
|
Output:
position before reset: 4
position after reset: 2
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ByteBuffer bb = ByteBuffer.allocate( 4 );
Buffer bb1 = (Buffer)bb;
bb1.position( 3 );
System.out.println( "position before clear: "
+ bb1.position());
bb1.clear();
System.out.println( "position after clear: "
+ bb1.position());
}
}
|
Output:
position before clear: 3
position after clear: 0
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#position-int-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Jul, 2019
Like Article
Save Article