Share on Google+Share on Google+

ShortBuffer in java, Use of remaining() method in short buffer.

Advertisement
In this tutorial, you will see use of remaining() method in short buffer.

ShortBuffer in java, Use of remaining() method in short buffer.

In this tutorial, we will see how to use of remaining() method in 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 new short buffer.
 ShortBuffer put(short [] array) The put(..)method transfer the elements of a short array into short buffer.

Buffer API:

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

Return type Method Description
final int remaining() The reset() method  returns remaining elements in buffer.

  Code

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

public class ElementRemain{
  public static void main(String[] args) {
    ShortBuffer shortBuf = ShortBuffer.allocate(124);
    short[] array = new short[] { 34543876 };
    shortBuf.put(array);
    shortBuf.flip();
    System.out.println("Current position of buffer : "
        + shortBuf.position());
    System.out.println("Current limit of buffer : " 
        + shortBuf.limit());
System.out.println("Remaining elements in short buffer :"
        + shortBuf.remaining());
    shortBuf.put((short341);
    System.out.println("New position of buffer : "
        + shortBuf.position());
    System.out.println("New limit of buffer : " 
        + shortBuf.limit());
System.out.println("Remaining elements in short buffer :"
        + shortBuf.remaining());
  }
}

Output

C:\>java ElementRemain
Current position of buffer : 0
Current limit of buffer : 3
Remaining elements in short buffer : 3
New position of buffer : 1
New limit of buffer : 3
Remaining elements in short buffer : 2

Download this code

Advertisements

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