Home Tutorial Java Corejava Nio How to check long buffer is direct or not in java.

 
 

How to check long buffer is direct or not in java.
Posted on: August 5, 2010 at 12:00 AM
Advertisement
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

Advertisement

Related Tags for How to check long buffer is direct or not in java.:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 5, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com