ByteBuffer rewind() in Java

rewind(): This method is available in java.nio.ByteBuffer class of Java.

Syntax:

ByteBuffer java.nio.ByteBuffer.rewind()

This method rewinds this buffer. The position is set to zero and the mark is discarded. Invoke this method before a sequence of channel-write or get operations, assuming that the limit has already been set appropriately.

Parameters: NA

Returns: This buffer.

Exceptions: NA

Approach

Java

import java.nio.ByteBuffer;

public class ByteBufferrewind {
    public static void main(String[] args) {

        byte array[] = { 1234 };
        ByteBuffer bb = ByteBuffer.wrap(array);

        // set position
        bb.position(2);

        System.out.println(("Position before rewind is " +
 bb.position()));

        bb.rewind();

        System.out.println(("Position after rewind is " + 
bb.position()));

    }
}


Output:

Position before rewind is 2 Position after rewind is 0


No comments:

Post a Comment