Java ByteBuffer getLong()方法的例子

Java ByteBuffer getLong()方法的例子

getLong()

java.nio.ByteBuffer 类的 getLong() 方法用于读取该缓冲区当前位置的下8个字节,根据当前的字节顺序将其合成为一个长值,然后将该位置增加8。

语法

public abstract long getLong()

返回值: 该方法返回缓冲区当前位置的长值。

抛出: 该方法抛出 BufferUnderflowException - 如果该缓冲区内剩余的字节数少于4个。

下面是说明getLong()方法的例子。

例子 1 :

// Java program to demonstrate
// getLong() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 16;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the long value in the bytebuffer
            bb.asLongBuffer()
                .put(1233003)
                .put(2292292);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the long at this buffer's current position
            // using getLong() method
            long value = bb.getLong();
  
            // print the long value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  long at this buffer's next position
            // using getLong() method
            long value1 = bb.getLong();
  
            // print the long value
            System.out.print("\nNext Byte Value: " + value1);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown : " + e);
        }
    }
}

输出。

Original ByteBuffer: 
1233003 2292292 

Byte Value: 1233003

Next Byte Value: 2292292

例子 2 :

// Java program to demonstrate
// getLong() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 16;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the long value in the bytebuffer
            bb.asLongBuffer()
                .put(1233003)
                .put(2292292);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the long at this buffer's current position
            // using getLong() method
            long value = bb.getLong();
  
            // print the long value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  long at this buffer's next position
            // using getLong() method
            long value1 = bb.getLong();
  
            // print the long value
            System.out.print("\nNext Byte Value: " + value1);
  
            // Reads the  long at this buffer's next position
            // using getLong() method
            long value2 = bb.getLong();
        }
  
        catch (BufferUnderflowException e) {
            System.out.println("\nthere are fewer than "
                               + "eight bytes remaining in this buffer");
            System.out.println("Exception Thrown : " + e);
        }
    }
}

输出。

Original ByteBuffer: 
1233003 2292292 

Byte Value: 1233003

Next Byte Value: 2292292
there are fewer than eight bytes remaining in this buffer
Exception Thrown : java.nio.BufferUnderflowException

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getLong-

getLong(int index)

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

语法

public abstract long getLong(int index)

参数: 该方法以index(将被读取的字节的索引)为参数。

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

异常: 该方法抛出 IndexOutOfBoundsException。 如果索引为负数或不小于缓冲区的限制,就会抛出这个异常。

下面是一些例子来说明getLong(int index)方法。

例子 1 :

// Java program to demonstrate
// getLong() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 16;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the long value in the bytebuffer
            bb.asLongBuffer()
                .put(1233003)
                .put(2292292);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the long at this buffer's current position
            // using getLong() method
            long value = bb.getLong(0);
  
            // print the long value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  long at this buffer's next position
            // using getLong() method
            long value1 = bb.getLong(8);
  
            // print the long 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: 
1233003 2292292 

Byte Value: 1233003

Next Byte Value: 2292292

例子 2 :

// Java program to demonstrate
// getLong() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 16;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the long value in the bytebuffer
            bb.asLongBuffer()
                .put(1233003)
                .put(2292292);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 8; i++)
                System.out.print(bb.getLong() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the long at this buffer's current position
            // using getLong() method
            long value = bb.getLong(0);
  
            // print the long value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  long at this buffer's next position
            // using getLong() method
            long value1 = bb.getLong(11);
  
            // print the long 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: 
1233003 2292292 

Byte Value: 1233003

index is negative or smaller than the buffer's limit, minus seven
Exception Thrown : java.lang.IndexOutOfBoundsException

参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getLong-int-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程