HomeLanguagesJavaFloatBuffer mark() methods in Java with Examples

FloatBuffer mark() methods in Java with Examples

The mark() method of java.nio.FloatBuffer Class is used to mark the current position of this FloatBuffer as the mark of this buffer.

Syntax:

public final FloatBuffer mark()

Parameters: The method does not take any parameters.

Return Value: This method returns this FloatBuffer after setting the buffer’s mark at the current position.

Below examples illustrate the mark() method:

Examples 1:




// Java program to demonstrate
// mark() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            float[] farr = { 10.5f, 20.5f, 30.5f, 40.5f };
  
            // creating object of FloatBuffer
            // and allocating size capacity
            FloatBuffer fb = FloatBuffer.wrap(farr);
  
            // try to set the position at index 2
            fb.position(2);
  
            // Set this buffer mark position
            // using mark() method
            fb.mark();
  
            // try to set the position at index 4
            fb.position(4);
  
            // display position
            System.out.println("position before reset: "
                               + fb.position());
  
            // try to call reset() to restore
            // to the position we marked
            fb.reset();
  
            // display position
            System.out.println("position after reset: "
                               + fb.position());
        }
  
        catch (InvalidMarkException e) {
            System.out.println("New position "
                               + "is less than "
                               + "the position "
                               + "marked before ");
            System.out.println("Exception throws: "
                               + e);
        }
    }
}


Output:

position before reset: 4
position after reset: 2

Examples 2: To demonstrate InvalidMarkException




// Java program to demonstrate
// mark() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            float[] farr = { 10.5f, 20.5f, 30.5f, 40.5f };
  
            // creating object of FloatBuffer
            // and allocating size capacity
            FloatBuffer fb = FloatBuffer.wrap(farr);
  
            // try to set the position at index 2
            fb.position(2);
  
            // Set this buffer mark position
            // using mark() method
            fb.mark();
  
            // try to set the position at index 1
            fb.position(1);
  
            // display position
            System.out.println("position before reset: "
                               + fb.position());
  
            // try to call reset() to restore
            // to the position we marked
            fb.reset();
  
            // display position
            System.out.println("position after reset: "
                               + fb.position());
        }
  
        catch (InvalidMarkException e) {
            System.out.println("\nNew position "
                               + "is less than "
                               + "the position "
                               + "marked before ");
            System.out.println("Exception throws: "
                               + e);
        }
    }
}


Output:

position before reset: 1

New position is less than the position marked before 
Exception throws: java.nio.InvalidMarkException

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#mark–

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32702 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7076 POSTS0 COMMENTS
Nicole Veronica
12209 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12303 POSTS0 COMMENTS
Shaida Kate Naidoo
7197 POSTS0 COMMENTS
Ted Musemwa
7456 POSTS0 COMMENTS
Thapelo Manthata
7174 POSTS0 COMMENTS
Umr Jansen
7161 POSTS0 COMMENTS