ShortBuffer limit() method in Java with Examples
Last Updated :
28 Aug, 2019
The limit() method of java.nio.ShortBuffer Class is used to modify this ShortBuffer’s limit. This method takes the limit to be set as the parameter and sets that as the new limit of this Buffer. If the mark of this Buffer is already defined and is larger than the new specified limit, then this new limit is not set and discarded.
Syntax:
public final ShortBuffer limit(int newLimit)
Parameter: This method accepts a parameter newLimit which is the new integer value for the limit.
Return Value: This method returns this buffer after setting the specified new limit as the new limit of this Buffer.
Below are the examples to illustrate the limit() method:
Examples 1:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ShortBuffer shortBuffer
= ShortBuffer.allocate( 4 );
shortBuffer.put(( short ) 20 );
shortBuffer.put(( short ) 30 );
System.out.println(
"ShortBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
shortBuffer.array())
+ "\nPosition: "
+ shortBuffer.position()
+ "\nLimit: "
+ shortBuffer.limit());
shortBuffer.limit( 1 );
System.out.println(
"\nShortBuffer after "
+ "setting buffer's limit: "
+ Arrays.toString(
shortBuffer.array())
+ "\nPosition: "
+ shortBuffer.position()
+ "\nLimit: "
+ shortBuffer.limit());
}
}
|
Output:
ShortBuffer before setting buffer's limit: [20, 30, 0, 0]
Position: 2
Limit: 4
ShortBuffer after setting buffer's limit: [20, 30, 0, 0]
Position: 1
Limit: 1
Examples 2:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ShortBuffer shortBuffer
= ShortBuffer.allocate( 5 );
shortBuffer.put(( short ) 20 );
shortBuffer.put(( short ) 30 );
shortBuffer.put(( short ) 40 );
shortBuffer.mark();
System.out.println(
"ShortBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
shortBuffer.array())
+ "\nPosition: "
+ shortBuffer.position()
+ "\nLimit: "
+ shortBuffer.limit());
shortBuffer.limit( 4 );
System.out.println(
"\nShortBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
shortBuffer.array())
+ "\nPosition: "
+ shortBuffer.position()
+ "\nLimit: "
+ shortBuffer.limit());
}
}
|
Output:
ShortBuffer before setting buffer's limit: [20, 30, 40, 0, 0]
Position: 3
Limit: 5
ShortBuffer before setting buffer's limit: [20, 30, 40, 0, 0]
Position: 3
Limit: 4
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ShortBuffer.html#mark–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...