java.nio ShortBuffer Example

Ads
 

java.nio ShortBuffer Example

Here we are discussing the example of java.nio ShortBuffer class.

Here we are discussing the example of java.nio ShortBuffer class.

java.nio ShortBuffer Example - Learn to use ShortBuffer in Java program

In this tutorial we are presenting example of java.nio ShortBuffer class. The java.nio.ShortBuffer class is created by extending java.nio.Buffer class which is used to perform operation on the short buffer. Today we will also see how to define the order of byte in short buffer.

The java.nio.ShortBuffer class is used to work with the short buffer data. The short buffer can be created either by allocating space for buffer's content or by wrapping and existing shortarray into a buffer or by creating a view of an existing byte buffer.

This class is available in Java from Java 1.4. The java.nio.ShortBuffer class defines methods for performing following 4 types of operations:

  • It defines Absolute and relative get and put methods which is used to read and write single shorts
  • The relative build get methods for transferring contiguous sequences of shorts from this buffer into an array
  • The relative bulk put methods for transferring contiguous sequences of shorts from a short array or some other short buffer into this buffer
  • It also provides various methods for compacting, duplicating, and slicing a short buffer

So, the java.nio.ShortBuffer provides lots of functionality to developers for working on the short buffer data.

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.

Example Code of ShortBuffer class

Here is complete example of using the ShortBuffer class in Java.

import java.nio.*;
import java.nio.ShortBuffer;
/*
Web: http://www.roseindia.net
*/

public class ShortBufferExample{
	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());	
	}
}

 

You can compile this program using Java 11 javac tool and then execute is with java interpretor.

Output of example program

Here is the output of ShortBufferExample code.

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

In this section we have learned to use java.io.ShortBuffer class in Java program. Here are more tutorials of Java:

Ads

Ads