Share on Google+Share on Google+

Create a short buffer by using wrap method of ShortBuffer class.

Advertisement
In this tutorial, you will see how to create a short buffer by using wrap method of ShortBuffer class.

Create a short buffer by using wrap method of ShortBuffer class.

In this tutorial, we will see how to create a shortt buffer by using wrap method of ShorttBuffer class.

ShortBuffer API.

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

Return type Method Description
static ShortBuffer wrap(short[] array)  The wrap() method create a short buffer by wrapping  the associated short array. 
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 WrapShortBuffer  {
  public static void main(String[] arg) {
    short[] array = new short[] { 123456 };
ShortBuffer shortBuff = ShortBuffer.wrap(array);
System.out.println("Limit of short buffer : " 
                            + shortBuff.limit());
System.out.println("capacity of short buffer : " 
                         + shortBuff.capacity());
  System.out.println("Content in shortbuffer.");
    while (shortBuff.hasRemaining()) {
      System.out.print(shortBuff.get() " ");
    }
  }
}

Output

C:\>java WrapShortBuffer
Limit of short buffer : 3
capacity of short buffer : 3
Content in shortbuffer.
12 34 56

Download this code

Advertisements

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.