Monday 10 June 2019

nio: check whether the buffer is read only or not


Buffer class provides 'isReadOnly()' method, to check whether the buffer is read only or not.

Test.java
package com.sample.nio;

import java.nio.ByteBuffer;

public class Test {

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

  ByteBuffer byteBuffer2 = byteBuffer1.asReadOnlyBuffer();

  System.out.println("is byteBuffer1 readOnly ? : " + byteBuffer1.isReadOnly());
  System.out.println("is byteBuffer2 readOnly ? : " + byteBuffer2.isReadOnly());
 }
}

Output
is byteBuffer1 readOnly ? : false
is byteBuffer2 readOnly ? : true

Previous                                                 Next                                                 Home

No comments:

Post a Comment