How to check long buffer is direct or not in java.


 

How to check long buffer is direct or not in java.

In this tutorial, you will see how to check long buffer is direct or not in java.

In this tutorial, you will see how to check long buffer is direct or not in java.

How to check long buffer is direct or not in java.

In this tutorial, we will discuss how to check long buffer is direct or not in java.

LongBuffer API.

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

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

Code

import java.nio.*;
import java.nio.LongBuffer;
import java.nio.ByteBuffer;

public class BufferIsDirect {
  public static void main(String[] args) {
    LongBuffer longBuf = LongBuffer.allocate(256);
    if (longBuf.isDirect()) {
      System.out.println("long buffer is direct.");
    else {
System.out.println("long buffer is not direct.");
    }
long[] arr = new long[] { 2354567766664787875 };
    LongBuffer longBuf1 = LongBuffer.wrap(arr);
    if (longBuf1.isDirect()) {
      System.out.println("long buffer is direct.");
    else {
  System.out.println("long buffer is not direct.");
    }
ByteBuffer byteBuf = ByteBuffer.allocateDirect(1024);
    LongBuffer longBuf2 = byteBuf.asLongBuffer();
    if (longBuf2.isDirect()) {
      System.out.println("long buffer is direct.");
    else {
  System.out.println("long buffer is not direct.");
    }
  }
}

Output

C:\>java BufferIsDirect
long buffer is not direct.
long buffer is not direct.
long buffer is direct.

Download this code

Ads