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


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


java.nio.ByteBuffer類的arrayOffset()方法用於返回給定緩衝區的第一個元素的後備數組內的偏移量。

如果此緩衝區由數組支持,則緩衝區位置p對應於數組索引p + arrayOffset()。

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


用法:

public final int arrayOffset()

返回值:此方法返回緩衝區第一個元素在此緩衝區數組內的偏移量

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

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

範例1:

// Java program to demonstrate 
// arrayOffset() 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); 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("ByteBuffer: "
                               + Arrays.toString(bb.array())); 
  
            // print the arrayOffset 
            System.out.println("arrayOffset: "
                               + bb.arrayOffset()); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception throws" + e); 
        } 
    } 
}
輸出:
ByteBuffer: [20, 30, 40, 50]
arrayOffset: 0

範例2:

// Java program to demonstrate 
// arrayOffset() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the  ByteBuffer 
        int capacity = 3; 
  
        // Creating the  ByteBuffer 
        try { 
  
            // creating object of  ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the value in  ByteBuffer 
            bb.put((byte)20); 
            bb.put((byte)30); 
            bb.put((byte)40); 
            bb.rewind(); 
  
            // Creating a read-only copy of  ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
  
            // print the  ByteBuffer 
            System.out.print("Read only buffer : "); 
            while (bb1.hasRemaining()) 
                System.out.print(bb1.get() + ", "); 
  
            // next line 
            System.out.println(""); 
  
            // print the arrayOffset 
            System.out.println("\nTry to print the array offset"
                               + " of read only buffer"); 
            System.out.println("arrayOffset: " + bb1.arrayOffset()); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception throws: " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception throws: " + e); 
        } 
    } 
}
輸出:
Read only buffer : 20, 30, 40, 

Try to print the array offset of read only buffer
Exception throws: java.nio.ReadOnlyBufferException


相關用法


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