Share on Google+Share on Google+

How to transfer value from long buffer to long array.

Advertisement
In this tutorial, you will see how to transfer value from long buffer to long array.

How to transfer value from long buffer to long array.

 In this tutorial, we will discuss how to transfer value from long buffer to long array.

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. 
LongBuffer get(Long[] array) The get(...) method transfer long value from long buffer into long array.

Code

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

public class ValueToArray {
  public static void main(String[] args) {
    LongBuffer longBuf = LongBuffer.allocate(256);
    longBuf.put(8765677);
    longBuf.put(21543543);
    longBuf.put(54322676);
    longBuf.flip();
    System.out.println("Content in long buffer.");
    while (longBuf.hasRemaining()) {
      System.out.println(longBuf.get());
    }
    longBuf.flip();
    long[] longArray = new long[longBuf.limit()];
    longBuf.get(longArray);
System.out.println("Long buffer value into long array.");
    for (int i = 0; i < longArray.length; i++) {
      System.out.println(longArray[i]);
    }
  }
}

Output

C:\>java ValueToArray
Content in long buffer.
8765677
21543543
54322676
Long buffer value into long array.
8765677
21543543
54322676

Download this code

Advertisements

Advertisement

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.