The getShort() method of java.nio.ByteBuffer class is used to read the next two bytes at this buffer’s current position, composing them into a short value according to the current byte order, and then increments the position by two.
Syntax:
public abstract short getShort()
Return Value: This method returns the short value at the buffer’s current position.
Throws: This method throws BufferUnderflowException if there are fewer than four bytes remaining in this buffer.
Below are the examples to illustrate the getShort() method:
Examples 1:
// Java program to demonstrate // getShort() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 6; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the short value in the bytebuffer bb.asShortBuffer() .put((short)1034) .put((short)1035) .put((short)1036); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 2; i++) System.out.print(bb.getShort() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getShort() method long value = bb.getShort(); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the short at this buffer's next position // using getShort() method long value1 = bb.getShort(); // print the short value System.out.print("\nNext Byte Value: " + value1); } catch (BufferUnderflowException e) { System.out.println("\nException Thrown : " + e); } } } |
Original ByteBuffer: 1034 1035 1036 Byte Value: 1034 Next Byte Value: 1035
Examples 2:
// Java program to demonstrate // getShort() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the short value in the bytebuffer bb.asShortBuffer() .put((short)1034) .put((short)1036); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 2; i++) System.out.print(bb.getShort() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getShort() method long value = bb.getShort(); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the short at this buffer's next position // using getShort() method long value1 = bb.getShort(); // print the short value System.out.print("\nNext Byte Value: " + value1); // Reads the short at this buffer's next position // using getShort() method long value2 = bb.getShort(); } catch (BufferUnderflowException e) { System.out.println("\nthere are fewer than" + " two bytes remaining in this buffer"); System.out.println("Exception Thrown : " + e); } } } |
Original ByteBuffer: 1034 1036 Byte Value: 1034 Next Byte Value: 1036 there are fewer than two bytes remaining in this buffer Exception Thrown : java.nio.BufferUnderflowException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getShort–
The getLong(int index) method of ByteBuffer is used to read four bytes at the given index, composing them into a float value according to the current byte order.
Syntax:
public abstract long getLong(int index)
Parameters: This method takes index (The index from which the Byte will be read) as a parameter.
Return Value: This method returns the long value at the given index.
Exception: This method throws IndexOutOfBoundsException. If index is negative or not smaller than the buffer’s limit this exception is thrown.
Below are the examples to illustrate the getLong(int index) method:
Examples 1:
// Java program to demonstrate // getShort() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the short value in the bytebuffer bb.asShortBuffer() .put((short)1034) .put((short)1036); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 2; i++) System.out.print(bb.getShort() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getShort() method long value = bb.getShort(0); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the short at this buffer's next position // using getShort() method long value1 = bb.getShort(2); // print the short value System.out.print("\nNext Byte Value: " + value1); } catch (IndexOutOfBoundsException e) { System.out.println("\nindex is negative or " + "smaller than the buffer's limit, " + " minus seven"); System.out.println("Exception Thrown : " + e); } } } |
Original ByteBuffer: 1034 1036 Byte Value: 1034 Next Byte Value: 1036
Examples 2:
// Java program to demonstrate // getShort() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the ByteBuffer int capacity = 4; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the short value in the bytebuffer bb.asShortBuffer() .put((short)1034) .put((short)1036); // rewind the Bytebuffer bb.rewind(); // print the ByteBuffer System.out.println("Original ByteBuffer: "); for (int i = 1; i <= capacity / 2; i++) System.out.print(bb.getShort() + " "); // rewind the Bytebuffer bb.rewind(); // Reads the long at this buffer's current position // using getShort() method long value = bb.getShort(0); // print the long value System.out.println("\n\nByte Value: " + value); // Reads the short at this buffer's next position // using getShort() method long value1 = bb.getShort(3); // print the short value System.out.print("\nNext Byte Value: " + value1); } catch (IndexOutOfBoundsException e) { System.out.println("\nindex is negative or smaller " + "than the buffer's limit, " + "minus one"); System.out.println("Exception Thrown : " + e); } } } |
Original ByteBuffer: 1034 1036 Byte Value: 1034 index is negative or smaller than the buffer's limit, minus one Exception Thrown : java.lang.IndexOutOfBoundsException
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getShort-int-
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Field getShort() method in Java with Examples
- Array getShort() method in Java
- ByteBuffer compareTo() method in Java With Examples
- ByteBuffer allocate() method in Java with Examples
- ByteBuffer asCharBuffer() method in Java with Examples
- ByteBuffer array() method in Java with Examples
- ByteBuffer asFloatBuffer() method in Java with Examples
- ByteBuffer asDoubleBuffer() method in Java with Examples
- ByteBuffer asIntBuffer() method in Java with Examples
- ByteBuffer allocateDirect() method in Java with Examples
- ByteBuffer arrayOffset() method in Java with Examples
- ByteBuffer asLongBuffer() method in Java with Examples
- ByteBuffer asShortBuffer() method in Java with Examples
- ByteBuffer asReadOnlyBuffer() method in Java with Examples
- ByteBuffer compact() method in Java with Examples
- ByteBuffer duplicate() method in Java with Examples
- ByteBuffer equals() method in Java with Examples
- ByteBuffer get() method in Java with Examples
- ByteBuffer getChar() method in Java with Examples
- ByteBuffer getFloat() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

