Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the floatBuffer rewind() 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.

java.nio.FloatBuffer is a class we can use to store a buffer of floats. 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 FloatBuffer.rewind() method is declared as follows:

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

Return value

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

Code

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

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 4;
int n2 = 4;
try {
FloatBuffer buff1 = FloatBuffer.allocate(n1);
buff1.put(1.5F);
buff1.put(4.6F);
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(1.9F);
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

  • A FloatBuffer 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.

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.

Keep Exploring