ShortBuffer in java, How to transfer content from short buffer to short array.


 

ShortBuffer in java, How to transfer content from short buffer to short array.

In this tutorial, you will see how to transfer content from short buffer to short array.

In this tutorial, you will see how to transfer content from short buffer to short array.

Write a short value into short buffer at given index.

 In this tutorial, we will see how to write the given short value into short buffer at the given index.

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  ShortBuffer put(int index, short value) The put(..) method write the given short value into associated buffer at given index.
abstract short get( ) The get(...) method returns short value from current position .

Code

import java.nio.*;
import java.nio.ShortBuffer;
public class PutValueAtIndex {
  public static final int index=2;
  public static void main(String[] arg) {
short[] array = new short[] { 1234};
ShortBuffer shortBuf = ShortBuffer.wrap(array);
System.out.println("Content in short buffer.");
    while (shortBuf.hasRemaining())
    {
    System.out.print(shortBuf.get()+" ");    
    }
    System.out.println();
System.out.println("Put value at index . "
                                      +index
);
System.out.println("Content in short buffer.");
    shortBuf.put(2(short655);
    shortBuf.flip();
    while (shortBuf.hasRemaining())
    {
    System.out.print(shortBuf.get()+" ");  
    }
  }
}

Output

C:\>java PutValueAtIndex
Content in short buffer.
1 2 3 4 5
Put value at index . 2
Content in short buffer.
1 2 655 4 5

Download this code

Ads