Java ByteBuffer mark

Introduction

Calling the mark() method will store the current value of its position as its mark value for the buffer.

Calling reset() method will set the position of a buffer to its previously marked value.

The mark is not set for a new buffer.

Calling the reset() method throws a runtime exception if the mark is not set.

If the mark is set, the position is set to the mark value.

import java.nio.ByteBuffer;
import java.nio.InvalidMarkException;

public class Main {
  public static void main(String[] args) {
    // Create a byte buffer of capacity 8
    ByteBuffer bb = ByteBuffer.allocate(8);

    try {/* w  w w. jav  a  2s . co m*/
      bb.reset();
      System.out.println("Mark: " + bb.position());
    } catch (InvalidMarkException e) {
      System.out.println("Mark is not set");
    }
  }
}



PreviousNext

Related