ByteBuffer rewind() methods in Java with Examples Last Updated : 16 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The rewind() method of java.nio.ByteBuffer Class is used to rewind 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. Invoking this method neither changes nor discards the mark's value. Syntax: public ByteBuffer rewind() Return Value: This method returns this buffer. Below are the examples to illustrate the rewind() method: Examples 1: Java // Java program to demonstrate // rewind() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // defining and allocating ByteBuffer // using allocate() method ByteBuffer byteBuffer = ByteBuffer.allocate(4); // put byte value in byteBuffer // using put() method byteBuffer.put((byte)20); byteBuffer.put((byte)'a'); // print the byte buffer System.out.println("Buffer before operation: " + Arrays.toString(byteBuffer.array()) + "\nPosition: " + byteBuffer.position() + "\nLimit: " + byteBuffer.limit()); // rewind the Buffer // using rewind() method byteBuffer.rewind(); // print the bytebuffer System.out.println("\nBuffer after operation: " + Arrays.toString(byteBuffer.array()) + "\nPosition: " + byteBuffer.position() + "\nLimit: " + byteBuffer.limit()); } } Output: Buffer before operation: [20, 97, 0, 0] Position: 2 Limit: 4 Buffer after operation: [20, 97, 0, 0] Position: 0 Limit: 4 Examples 2: Java // Java program to demonstrate // rewind() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // defining and allocating ByteBuffer // using allocate() method ByteBuffer byteBuffer = ByteBuffer.allocate(5); // put byte value in byteBuffer // using put() method byteBuffer.put((byte)20); byteBuffer.put((byte)30); byteBuffer.put((byte)40); // mark will be going to discarded by rewind() byteBuffer.mark(); // print the buffer System.out.println("Buffer before operation: " + Arrays.toString(byteBuffer.array()) + "\nPosition: " + byteBuffer.position() + "\nLimit: " + byteBuffer.limit()); // Rewind the Buffer // using rewind() method byteBuffer.rewind(); // print the buffer System.out.println("\nBuffer after operation: " + Arrays.toString(byteBuffer.array()) + "\nPosition: " + byteBuffer.position() + "\nLimit: " + byteBuffer.limit()); } } Output: Buffer before operation: [20, 30, 40, 0, 0] Position: 3 Limit: 5 Buffer after operation: [20, 30, 40, 0, 0] Position: 0 Limit: 5 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#rewind-- Comment More infoAdvertise with us Next Article Buffer rewind() methods in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-ByteBuffer Practice Tags : Java Similar Reads Buffer rewind() methods in Java with Examples The rewind() method of java.nio.ByteBuffer Class is used to rewind 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. Invoking this method neither cha 2 min read ByteBuffer reset() methods in Java with Examples The reset() method of java.nio.ByteBuffer Class is used to reset this buffer's position to the previously-marked position. Invoking this method neither changes nor discards the mark's value. Syntax: public ByteBuffer reset() Return Value: This method returns this buffer. Below are the examples to il 2 min read CharBuffer rewind() methods in Java with Examples The rewind() method of java.nio.CharBuffer Class is used to rewind 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. Invoking this method neither cha 2 min read ByteBuffer putInt() methods in Java with Examples putInt(int value) The putInt(int value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax : public abstract ByteBuffer putInt(int value) 6 min read ByteBuffer position() methods in Java with Examples The position(int newPosition) method of java.nio.ByteBuffer Class is used to Sets this buffer's position. If the mark is defined and larger than the new position then it is discarded. Syntax: public ByteBuffer position(int newPosition) Parameters: This method takes the newPosition as parameter which 2 min read Buffer reset() methods in Java with Examples The reset() method of java.nio.Buffer Class is used to reset this buffer's position to the previously-marked position. Invoking this method neither changes nor discards the mark's value.Syntax: public Buffer reset() Return Value: This method returns this buffer.Below are the examples to illustrate t 2 min read Like