What is the ShortBuffer rewind() method in Java?

What is the ShortBuffer rewind() method in Java? Answer

java.nio.ShortBuffer is a class we can use to store a buffer of shorts. We can use this class’s rewind() method to rewind a buffer. Rewinding a buffer sets the buffer position to zero.

  • Limit of the buffer remains unaffected.
  • Any position previously marked is discarded.

Declaration

The ShortBuffer.rewind() method is declared as follows:

buff.rewind()
  • buff: The ShortBuffer to rewind.

Return value

The ShortBuffer.rewind() method returns the ShortBuffer buff after rewinding.

Code

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

import java.nio.*;
import java.util.*;
public class main {
   public static void main(String[] args) {
      int n1 = 4;
      int n2 = 4;
      try {
         ShortBuffer buff1 = ShortBuffer.allocate(n1);
         buff1.put((short)1);
         buff1.put((short)4);

         System.out.println("buff1: " + Arrays.toString(buff1.array()));
         System.out.println("position at(before rewind): " + buff1.position());

         System.out.println("rewind()");
         buff1.rewind();

         System.out.println("position at(after rewind): " + buff1.position());

         buff1.put((short)3);
         System.out.println("buff1: " + Arrays.toString(buff1.array()));
         
      } catch (IllegalArgumentException e) {
         System.out.println("Error!!! IllegalArgumentException");
      } catch (ReadOnlyBufferException e) {
         System.out.println("Error!!! ReadOnlyBufferException");
      }
   }
}

Explanation

  • ShortBuffer buff1 is declared in line 8.
  • Two elements are added to buff1 using the put() method in lines 9-10. 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 before rewind is 2. After calling the rewind() method in line 16, the position of buff1 is set to 0. This is why calling the put() method on buff1 adds the element at the 0th index of buff1.
What is the ShortBuffer rewind() method in Java? Review:

In our experience, we suggest you solve this What is the ShortBuffer rewind() method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the ShortBuffer rewind() method in Java?

Find on Educative

Conclusion:

I hope this What is the ShortBuffer rewind() method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.