Home Tutorial Java Corejava Nio How to create a duplicate buffer of a long buffer in java.

 
 

How to create a duplicate buffer of a long buffer in java.
Posted on: August 6, 2010 at 12:00 AM
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

Advertisement

Related Tags for How to create a duplicate buffer of a long buffer in java.:


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.

Posted on: August 6, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com