Share on Google+Share on Google+

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

Advertisement
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

Advertisements

Posted on: August 6, 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.