ShortBuffer in java, Define the order of byte in short buffer.


 

ShortBuffer in java, Define the order of byte in short buffer.

In this tutorial, you will see how to define the order of byte in short buffer.

In this tutorial, you will see how to define the order of byte in short buffer.

ShortBuffer in java, Define the order of byte in short buffer.

In this tutorial, we will see how to define the order of byte 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 wrap(short [] array)  The wrap(....) method wrapping an existing short array into short buffer.
 abstract ByteOrder order() The order(..) method returns order of bytes used by
short buffer.

ByteBuffer API:

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

Return type Method Description
static ByteBuffer wrap([] array) The wrap(....) method wrapping an existing byte array into byte buffer.
 abstrtact ShortBuffer  asShortBuffer() The asShortBuffer() method returns short buffer based on byte buffer.

  Code

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

public class ByteOrder{
  public static void main(String[] args) {
    byte[] array=new byte[]{3,4,65};
    ByteBuffer byteBuff=ByteBuffer.wrap(array);
    ShortBuffer shortBuff=byteBuff.asShortBuffer();
System.out.println("Byte order of short buffer : "
  + shortBuff.order
());
    short[] arr=new short[] {3,4,65};
    ShortBuffer shortBuff1=ShortBuffer.wrap(arr);
System.out.println("Byte order of short buffer : "
    + shortBuff1.order
());  
  }
}

Output

C:\>java ByteOrder
Byte order of short buffer : BIG_ENDIAN
Byte order of short buffer : LITTLE_ENDIAN

Download this code

Ads