Use of isDirect() method of intBuffer class in java.


 

Use of isDirect() method of intBuffer class in java.

In this tutorial you will see the use of isDirect() method of intBuffer class in java.

In this tutorial you will see the use of isDirect() method of intBuffer class in java.

Use of isDirect() method of intBuffer class in java.

In this tutorial, we will check int buffer is direct or not.

IntBuffer API.

The java.nio.IntBuffer class extends java.nio.Buffer class. It provides the following methods:

Return type Method Description
abstract boolean isDirect() The isDirect() method tells whether this associated buffer is direct or not.
static IntBuffer wrap(int[] array)  The wrap(...) method create a int buffer by wrapping  the associated int array. 

Code

import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class IntIsDirect {
  public static void main(String[] argv){
ByteBuffer b = ByteBuffer.allocateDirect(512);
    IntBuffer i = b.asIntBuffer();
    if (i.isDirect()) {
   System.out.println("int buffer is direct.");
    else {
System.out.println("int buffer is not direct.");
    }
    int[] array = new int[] {1,2,3,4,5};
    IntBuffer intBuf = IntBuffer.wrap(array);
    if (intBuf.isDirect()) {
   System.out.println("int buffer is direct.");
    else {
System.out.println("int buffer is not direct.");
    }
  }
}

Output

C:\>java IntIsDirect
int buffer is direct.
int buffer is not direct.

Ads