📌  相关文章
📜  Java中的 ByteBuffer getShort() 方法及示例

📅  最后修改于: 2022-05-13 01:54:22.850000             🧑  作者: Mango

Java中的 ByteBuffer getShort() 方法及示例

得到短()

Java.nio.ByteBuffer类的getShort()方法用于读取该缓冲区当前位置的下两个字节,根据当前字节顺序将它们组合成一个short 值,然后将该位置加2。

句法:

public abstract short getShort()

返回值:此方法返回缓冲区当前位置的短值。

抛出:如果此缓冲区中剩余的字节少于四个,则此方法将抛出BufferUnderflowException

以下是说明 getShort() 方法的示例:

示例 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

示例 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

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/ByteBuffer.html#getShort–

getLong(整数索引)

ByteBuffergetLong(int index)方法用于读取给定索引处的四个字节,根据当前字节顺序将它们组合成一个浮点值。

句法:

public abstract long getLong(int index)

参数:此方法以索引(将从中读取字节的索引)作为参数。

返回值:此方法返回给定索引处的长值。

异常:此方法抛出IndexOutOfBoundsException 。如果 index 为负数或不小于缓冲区的限制,则会引发此异常。

以下是说明getLong(int index)方法的示例:

示例 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

示例 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

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/ByteBuffer.html#getShort-int-