Create a long buffer by wrapping an long array.


 

Create a long buffer by wrapping an long array.

In this tutorial, you will see how to create a long buffer by wrapping an long array.

In this tutorial, you will see how to create a long buffer by wrapping an long array.

Create a long buffer by wrapping an long array.

 In this tutorial, we will see how to create a long buffer by wrapping an long array into a buffer.

LongBuffer API:

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

Return type Method Description
static LongBuffer wrap(long[] array)  The wrap(....) method wrap an existing long array and create long buffer.

Buffer API:

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

Return type Method Description
int limit() The limit() method returns the limit of int buffer.
int position() The position() method returns the position of int buffer.
final int capacity() The capacity() method returns the capacity of associated int buffer.

Code

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

public class WrapLongBuffer {
  public static void main(String[] argsthrows Exception {
    long[] array = new long[] { 65455557878677 };
    LongBuffer longBuf = LongBuffer.wrap(array);
    System.out.println("Content in long buffer.");
    while (longBuf.hasRemaining()) {
      System.out.println(longBuf.get());
    }
System.out.println("Capacity of LongBuffer : " + longBuf.capacity());
System.out.println("Limit of LongBuffer : " + longBuf.limit());
System.out.println("Position of LongBuffer : " + longBuf.position());
  }
}

Output

C:\>java WrapLongBuffer
Content in long buffer.
6545555
7878677
Capacity of LongBuffer : 2
Limit of LongBuffer : 2
Position of LongBuffer : 2

Download this code

Ads