当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ByteBuffer allocateDirect() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。