當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。