Share on Google+Share on Google+

Create a duplicate short buffer that shares the content of a short buffer.

Advertisement
In this tutorial, you will see how to create a duplicate short buffer that shares the content of a short buffer.

Create a duplicate short buffer that shares the content of a short buffer.

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

ShortBuffer API:

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

Return type Method Description
static ShortBuffer allocate( int capacity)  The allocate(..) method allocate a short buffer of given capacity. 
abstract ShortBuffer duplicate() The duplicate() method returns a  duplicate buffer that share the content of available buffer.
abstract short get() The get() method read short value from current position and increment position..

Code

import java.nio.*;
import java.nio.ShortBuffer;

public class DShortBuffer {
public static void main(String[] arg) {
    ShortBuffer shortBuff = ShortBuffer.allocate(1024);
    shortBuff.put((short566);
    shortBuff.put((short878);
    shortBuff.put((short4886);
    shortBuff.flip();
System.out.println("Capacity of original short buffer : "
        + shortBuff.capacity());
System.out.println("Content in original short buffer : ");
    while (shortBuff.hasRemaining()) {
      System.out.print(shortBuff.get() " ");
    }
    System.out.println();
    ShortBuffer dBuffer = shortBuff.duplicate();
    int capacity = dBuffer.capacity();
    dBuffer.flip();
System.out.println("Capacity of duplicate short buffer :"
     
+ capacity);
System.out.println("Content in duplicate short buffer : ");
    for (int i = 0; i < dBuffer.limit(); i++) {
      System.out.print(dBuffer.get() " ");
    }
  }
}

Output

C:\>java DShortBuffer
Capacity of original short buffer : 1024
Content in original short buffer :
566 878 4886
Capacity of duplicate short buffer :1024
Content in duplicate short buffer :
566 878 4886

Download this code

Advertisements

Advertisement

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