VOOZH about

URL: https://www.geeksforgeeks.org/java/bytebuffer-getshort-method-in-java-with-examples/

⇱ ByteBuffer getShort() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

ByteBuffer getShort() method in Java with Examples

Last Updated : 17 Jun, 2019
getShort()
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:
Output:
Original ByteBuffer: 
1034 1035 1036 

Byte Value: 1034

Next Byte Value: 1035
Examples 2:
Output:
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--
getLong(int index)
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:
Output:
Original ByteBuffer: 
1034 1036 

Byte Value: 1034

Next Byte Value: 1036
Examples 2:
Output:
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-
Comment