Monday, 10 June 2019

Nio: Get the current position of the buffer

Buffer class provides ‘position’ method to get the current position of the buffer.

Test.java
package com.sample.nio;

import java.nio.ByteBuffer;

public class Test {

 public static void main(String args[]) throws Exception {
  ByteBuffer byteBuffer = ByteBuffer.allocate(10);

  System.out.println("Current position of the buffer " + byteBuffer.position());

  System.out.println("Adding two elements to the buffer");
  byteBuffer.put((byte) 'H');
  byteBuffer.put((byte) 'I');

  System.out.println("Current position of the buffer " + byteBuffer.position());

 }
}


Output

Current position of the buffer 0
Adding two elements to the buffer
Current position of the buffer 2


Previous                                                 Next                                                 Home

No comments:

Post a Comment