ShortBuffer in java, Clean a short buffer by using clear method.


 

ShortBuffer in java, Clean a short buffer by using clear method.

In this tutorial, you will see how to clean a short buffer by using clear method.

In this tutorial, you will see how to clean a short buffer by using clear method.

Clean a short buffer by using clear method.

In this tutorial, we will discuss how to clean a short buffer by using clear method.

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 put(short s) The put(..)method write a given short value at current position.

Buffer API:

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

Return type Method Description
final Buffer clear() The clear() method clear the buffer. 
Int remaining() The remaining() method returns remaining element in buffer.

code

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

public class ClearShortBuff  {
  public static void main(String[] arg) {
short[] array = new short[]{ 23546666677 };
ShortBuffer shortBuf = ShortBuffer.wrap(array);
System.out.println("Value in a short buffer.");
    while (shortBuf.hasRemaining()) {
      System.out.println(shortBuf.get());
    }
    shortBuf.clear();
    shortBuf.flip();
    int remainElement = shortBuf.remaining();
    ystem.out.println("Number of remaining " 
        "elements in short buffer."
        + remainElement);
    if (remainElement == 0) {
      System.out.println("Clear.");
    else {
      System.out.println("Not clear.");
    }
  }
}

Output

C:\>java ClearShortBuff
Short data in a short buffer.
2354
666
6677
Number of remaining elements in short buffer : 0
Clear.

Download this code

Ads