ShortBuffer in java, Transfer the content of a short buffer into another.


 

ShortBuffer in java, Transfer the content of a short buffer into another.

In this tutorial, you will see how to transfer the content of a short buffer into another.

In this tutorial, you will see how to transfer the content of a short buffer into another.

ShortBuffer in java, Transfer the content of a short buffer into another.

 In this tutorial, we will see how to transfer the content of a short buffer into another 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 wrap(short [] array)  The wrap(....) method wrapping an existing short array into short buffer.
 ShortBuffer put(ShortBuffer buffer) The put(..)method transfer the content of a short buffer into another short buffer.

Code

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

public class BufferToBuffer {
  public static void main(String[] arg) {
    short[] array = new short[] { 789734};
    ShortBuffer shortBuf = ShortBuffer.wrap(array);
    ShortBuffer shortBuf1 = ShortBuffer.allocate(256);
    System.out.println("Transfer the content from one"
        " short buffer into another short buffer.");
  System.out.println("Elements in this short buffer.");
    while (shortBuf.hasRemaining()) {
      System.out.print(shortBuf.get() " ");
    }
    shortBuf.flip();
    shortBuf1.put(shortBuf);
    shortBuf1.flip();
    System.out.println();
System.out.println("Elements in another short buffer.");
    while (shortBuf1.hasRemaining()) {
      System.out.print(shortBuf1.get() " ");
    }
  }
}

Output

C:\>java BufferToBuffer
Transfer the content from one short buffer into another short buffer.
Elements in this short buffer.
75 33 442 23
Elements in another short buffer.
75 33 442 23

Download this code

Ads