How to create a duplicate buffer of a long buffer in java.


 

How to create a duplicate buffer of a long buffer in java.

In this tutorial, you will see how to create a duplicate buffer of a long buffer in java.

In this tutorial, you will see how to create a duplicate buffer of a long buffer in java.

How to create a duplicate buffer of a long buffer in java.

In this tutorial, we will see how to create a duplicate buffer that shares the content of long buffer.

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 allocate a long buffer of given capacity. 
abstract LongBuffer duplicate() The duplicate() method returns a duplicate buffer that share the content of available buffer.

Code

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

public class DuplicateBuffer {
  public static void main(String[] args){
  long[] array=new long[] {44645669,57554776,77557434};
    LongBuffer longBuf = LongBuffer.wrap(array);
System.out.println("Content in original long buffer.");
    while (longBuf.hasRemaining()) {
      System.out.println(longBuf.get());
    }
    LongBuffer longBuf2= longBuf.duplicate();
    longBuf2.flip();
System.out.println("Content in duplicate long buffer.");
    while (longBuf2.hasRemaining()) {
      System.out.println(longBuf2.get());
    }
  }
}

Output

C:\>java DuplicateBuffer
Content in original long buffer.
44645669
57554776
77557434
Content in duplicate long buffer.
44645669
57554776
77557434

Download this code

Ads