Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the shortBuffer reset() method in Java?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

We can use the reset() method of the class java.nio.ShortBuffer to set the position of ShortBuffer to a previously marked position.

The position of a buffer is the index of the next element that we read or write. Marking a position means recording a position. We can reset this marked position using the ShortBuffer.reset() method.

Note:

  • The mark remains unchanged.
  • The mark is not discarded.
  • If the new position is less than the previously marked position using the mark() method, InvalidMarkException is thrown.

Declaration

The ShortBuffer.reset() method can be declared as:

buff1.reset();
  • buff1: The buffer whose position will be set to the previously marked position.

Return value

The ShortBuffer.reset() method returns the ShortBuffer buff with the position set to the previously marked position.

Code

Example 1

Consider the code snippet below, which demonstrates the use of the ShortBuffer.reset() method.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 6;
ShortBuffer buff1 = ShortBuffer.allocate(n1);
buff1.put((short)1);
buff1.put((short)4);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
buff1.mark();
System.out.println("position marked at: " + buff1.position());
buff1.position(4);
System.out.println("position (before reset): " + buff1.position());
System.out.println("reset()");
buff1.reset();
System.out.println("position (after reset): " + buff1.position());
}
}

Explanation

  • A ShortBuffer buff1 is declared in line 7 with the capacity n1 = 6.

  • Two elements are added to buff1 using the put() method in lines 8-9. After adding the first element, the position of buff1 is incremented from 0 to 1. After adding the second element, the position of buff1 is incremented from 1 to 2.

  • The position of buff1 is marked to 2 using the mark() method in line 12.

  • The position of buff1 is set to 4 using the position() method in line 15.

  • The position of buff1 before reset is 4. After calling the reset() method in line 19, the position is set to the previously marked position 2.

Example 2

Consider the code snippet below, which demonstrates an example of the ShortBuffer.reset() method in which InvalidMarkException is thrown.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
try{
int n1 = 6;
ShortBuffer buff1 = ShortBuffer.allocate(n1);
buff1.put((short)1);
buff1.put((short)4);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
buff1.mark();
System.out.println("position marked at: " + buff1.position());
buff1.position(1);
System.out.println("position (before reset): " + buff1.position());
System.out.println("reset()");
buff1.reset();
System.out.println("position (after reset): " + buff1.position());
}catch (InvalidMarkException e) {
System.out.println("Exception: " + e);
}
}
}

Explanation

  • A ShortBuffer buff1 is declared in line 9 with the capacity n1 = 6.

  • Two elements are added to buff1 using the put() method in lines 10-11. After adding the first element, the position of buff1 is incremented from 0 to 1. After adding the second element, the position of buff1 is incremented from 1 to 2.

  • The position of buff1 is marked to 2 using the mark() method in line 14.

  • The position of buff1 is set to 1 using the position() method in line 17. As 1 is less than the previously marked position 2, InvalidMarkException is thrown.

RELATED TAGS

java

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring