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


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


java.nio.ByteBuffer類的array()方法用於返回支持采用緩衝區的字節數組。

對該緩衝區內容的修改將導致返回數組的內容被修改,反之亦然。

在調用此方法之前,請先調用hasArray()方法,以確保此緩衝區具有可訪問的後備數組。


用法:

public final byte[] array()

返回值:此方法返回支持此緩衝區的數組。

異常:如果此緩衝區由數組支持但為隻讀,則此方法引發ReadOnlyBufferException。

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

示例1:

// Java program to demonstrate 
// array() 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 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.put((byte)50); 
  
            // print the ByteBuffer 
            System.out.println("ByteBuffer:  "
                               + Arrays.toString(bb.array())); 
  
            // getting byte array from ByteBuffer 
            // using array() method 
            byte[] arr = bb.array(); 
  
            // print the byte array 
            System.out.println("\nbyte array: " +  
                                    Arrays.toString(arr)); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception throws: " + e); 
        } 
    } 
}
輸出:
ByteBuffer:  [20, 30, 40, 50]

byte array: [20, 30, 40, 50]

示例2:

// Java program to demonstrate 
// array() 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 typcast value 
            // in ByteBuffer 
            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())); 
  
            // Creating a read-only copy of ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
            bb1.rewind(); 
  
            // print the ByteBuffer 
            System.out.print("\nReadOnlyBuffer ByteBuffer : "); 
  
            while (bb1.hasRemaining()) 
                System.out.print(bb1.get() + ", "); 
  
            // getting byte array from read-only 
            // ByteBuffer using array() method 
            System.out.println("\n\nTrying to get the array"
                               + " from bb1 for editing"); 
            byte[] arr = bb1.array(); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception throws: " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws: " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer:  [20, 30, 40, 50]

ReadOnlyBuffer ByteBuffer : 20, 30, 40, 50, 

Trying to get the array from bb1 for editing
Exception throws: java.nio.ReadOnlyBufferException


相關用法


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