Share on Google+Share on Google+

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

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

Advertisements

Posted on: August 5, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.