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


Java ByteBuffer allocate()用法及代碼示例


java.nio.ByteBuffer類的allocate()方法用於分配新的字節緩衝區。

新緩衝區的位置將為零,其極限將是其容量,其標記將是未定義的,並且其每個元素都將初始化為零。它將有一個支持數組,其數組偏移量將為零。

用法:


public static ByteBuffer allocate(int capacity)

參數:此方法將容量(以字節為單位)作為參數。

返回值:此方法返回新的字節緩衝區。

拋出:如果容量為負整數,則此方法拋出IllegalArgumentException

下麵是說明allocate()方法的示例:

範例1:

// Java program to demonstrate 
// allocate() 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 int to byte typecast value 
            // in ByteBuffer using putInt() method 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.put((byte)50); 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array())); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
Original ByteBuffer:  [20, 30, 40, 50]

範例2:

// Java program to demonstrate 
// allocate() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity with negative 
        // value of the ByteBuffer 
        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.allocate(capacity); 
  
            // putting int to byte typecast value 
            // in ByteBuffer using putInt() method 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.put((byte)50); 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer:  "
                               + Arrays.toString(bb.array())); 
        } 
  
        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


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer allocate() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。