Home Tutorial Java Corejava Nio How to transfer value from long buffer to long array.

 
 

How to transfer value from long buffer to long array.
Posted on: August 5, 2010 at 12:00 AM
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

Related Tags for How to transfer value from long buffer to long array.:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.