ShortBuffer in java, Use of isDirect() method of ShortBuffer class in java.


 

ShortBuffer in java, Use of isDirect() method of ShortBuffer class in java.

In this tutorial, you will see how to use of isDirect() method of ShortBuffer class in java.

In this tutorial, you will see how to use of isDirect() method of ShortBuffer class in java.

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

In this tutorial, we will check short buffer is direct or not. If direct memory is allocated their memory address is fixed for the lifetime of the buffer.

ShortBuffer API:

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

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

Code

import java.nio.*;
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

public class DirectShortBuffer {
  public static void main(String[] arg) {
    short[] array = new short[] { 1234};
  ShortBuffer shortBuf1 = ShortBuffer.wrap(array);
    if (shortBuf1.isDirect()) {
    System.out.println("Short buffer is direct.");
    else {
System.out.println("Short buffer is not direct.");
    }
    ByteBuffer b = ByteBuffer.allocateDirect(512);
    ShortBuffer shortBuf = b.asShortBuffer();
    if (shortBuf.isDirect()) {
   System.out.println("Short buffer is direct.");
    else {
System.out.println("Short buffer is not direct.");
    }
  }
}

Output

C:\>java DirectShortBuffer
Short buffer is not direct.
Short buffer is direct.

Download this code

Ads