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


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


java.nio.Buffer類的array()方法用於返回支持已采用緩衝區的數組。此方法旨在使array-backed緩衝區更有效地傳遞給本機代碼。具體的子類為此方法提供了更強類型的返回值。

對該緩衝區內容的修改將導致返回數組的內容被修改,反之亦然。在調用此方法之前,請先調用hasArray方法,以確保此緩衝區具有可訪問的後備數組。

用法:


public abstract Object 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); 
  
            // Typecasting ByteBuffer into Buffer 
            Buffer bb1 = (Buffer)bb; 
  
            // getting array that backs this buffer 
            // using array() method 
            byte[] arr = (byte[])bb1.array(); 
  
            // print the array 
            System.out.print("array is:["); 
            for (int i = 0; i < arr.length; i++) 
                System.out.print(" " + arr[i]); 
            System.out.print(" ]"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("Exception throws:"
                               + e); 
        } 
    } 
}
輸出:
array is:[ 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 typecast 
            // value in ByteBuffer 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.put((byte)50); 
  
            // Creating a read-only copy of ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
  
            // Typecasting Read only ByteBuffer 
            // into Read-only Buffer 
            Buffer buffer = (Buffer)bb1; 
  
            // getting array that backs this buffer 
            // using array() method 
            byte[] arr = (byte[])buffer.array(); 
  
            // print the array 
            System.out.print("array is:["); 
            for (int i = 0; i < arr.length; i++) 
                System.out.print(" " + arr[i]); 
            System.out.print(" ]"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("buffer is backed by "
                               + "an array but is read-only"); 
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
buffer is backed by an array but is read-only
Exception throws:java.nio.ReadOnlyBufferException

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#array-



相關用法


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