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

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

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

使用Java.nio.ByteBuffer类的allocateDirect()方法分配一个新的直接字节缓冲区。

新缓冲区的位置将为零,其限制将是其容量,其标记将未定义,并且其每个元素将被初始化为零。它是否有一个支持数组是未指定的。

此方法比 allocate() 方法快 25%-75%。

句法:

public static ByteBuffer allocateDirect(int capacity)

参数:此方法将容量(以字节为单位)作为参数。

返回值:此方法返回新的字节缓冲区。

异常:此方法抛出IllegalArgumentException ,如果容量为负整数。

下面是说明 allocateDirect() 方法的示例:

示例 1:

// Java program to demonstrate
// allocateDirect() 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.allocateDirect(capacity);
  
            // creating byte array of size capacity
            byte[] value = { 20, 30, 40, 50 };
  
            // wrap the byte array into ByteBuffer
            bb = ByteBuffer.wrap(value);
  
            // print the ByteBuffer
            System.out.println("Direct ByteBuffer:  "
                               + Arrays.toString(bb.array()));
  
            // print the state of the buffer
            System.out.print("\nState of the ByteBuffer : ");
            System.out.println(bb.toString());
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Direct ByteBuffer:  [20, 30, 40, 50]

State of the ByteBuffer : java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]

示例 2:显示 IllegalArgumentException

// Java program to demonstrate
// allocateDirect() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity
        // with negative value
        int capacity = -4;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
  
            System.out.println("Trying to allocate"
                               + " negative value in ByteBuffer");
            ByteBuffer bb = ByteBuffer.allocateDirect(capacity);
  
            // creating byte array of size capacity
            byte[] value = { 20, 30, 40, 50 };
  
            // wrap the byte array into ByteBuffer
            bb = ByteBuffer.wrap(value);
  
            // print the ByteBuffer
            System.out.println("Direct ByteBuffer:  "
                               + Arrays.toString(bb.array()));
  
            // print the state of the buffer
            System.out.print("\nState of the ByteBuffer : ");
            System.out.println(bb.toString());
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Trying to allocate negative value in ByteBuffer
Exception thrown : java.lang.IllegalArgumentException: Negative capacity: -4