FloatBuffer mark() methods in Java with Examples

The java.nio.FloatBuffer Class's mark() function is used to clear this buffer. The current position of this FloatBuffer is marked as the buffer's mark using the FloatBuffer Class.

Syntax:

Parameters: There are no parameters required by the method.

Return Value: This method sets the buffer's mark to the current position and then returns this FloatBuffer.

Example 1:

The example below illustrates how to utilize Java's mark and reset functions with a FloatBuffer. The position is set to index 3, and a preexisting float array is used to initialize a FloatBuffer. The position is then moved to index 6 and marked at this location. It prints the position prior to resetting. Resetting the buffer to the designated point (index 3) is accomplished by invoking the reset procedure. Lastly, it prints the location after resetting to show how mark and reset can be used to go back to a point that has already been noted in the buffer.

Implementation:

FileName: markFloatExample1.java

Output:

 
The position before reset is given by: 6
The position after reset is given by: 3   

Example 2:

The example below explains how to use Java's mark and reset functions with a FloatBuffer. It sets the position of the buffer to index 2 and initializes a FloatBuffer using a preconfigured float array. After that, it sets the position to index 1 and marks this location using the mark function. It prints the position, which is 1, prior to using reset. The buffer's position returns to the previously indicated position (index 2) when reset is called. To demonstrate how the mark and reset methods enable returning to a specified place within the buffer, the code prints the position after resetting.

Implementation:

FileName: markFloatExample2.java

Output:

 
The position before reset is given by: 1
The New position is less than the position that has marked before 
The Exception thrown is: java.nio.InvalidMarkException