Open In App

ByteBuffer limit() methods in Java with Examples

Last Updated : 19 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The limit() method of java.nio.ByteBuffer Class is used to set this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded. 

Syntax:

public ByteBuffer limit(int newLimit)

Return Value: This method returns this buffer. 

Below are the examples to illustrate the limit() method: 

Examples 1: 

Output:
ByteBuffer before compact: [20, 30, 0, 0]
Position: 2
Limit: 4

ByteBuffer after compact: [20, 30, 0, 0]
Position: 1
Limit: 1

Examples 2: 

Output:
ByteBuffer before compact: [20, 30, 40, 0, 0]
Position: 3
Limit: 5

ByteBuffer after compact: [20, 30, 40, 0, 0]
Position: 3
Limit: 4

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#limit-int-


Next Article
Practice Tags :

Similar Reads