Home Tutorial Java Corejava Nio Use of isDirect() method of intBuffer class in java.

 
 

Use of isDirect() method of intBuffer class in java.
Posted on: August 4, 2010 at 12:00 AM
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.

Related Tags for Use of isDirect() method of intBuffer class in java.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.