当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Buffer arrayOffset()用法及代码示例


java.nio.Buffer类的arrayOffset()方法用于返回给定缓冲区的第一个元素的后备数组内的偏移量。

如果此缓冲区由数组支持,则缓冲区位置p对应于数组索引p + arrayOffset()。

在调用此方法之前,请先调用hasArray方法,以确保此缓冲区具有可访问的后备数组。


用法:

public abstract 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); 
  
            // Typecasting ByteBuffer into Buffer 
            Buffer buffer = (Buffer)bb; 
  
            // offset within this buffer's array 
            // of the first element of the buffer 
            // using arrayOffset() method 
            int offset = buffer.arrayOffset(); 
  
            // print the array 
            System.out.println("arrayOffset is : "
                               + offset); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("buffer is backed by an "
                               + "array but is read-only"); 
            System.out.println("Exception throws: " + e); 
        } 
    } 
}
输出:
arrayOffset is : 0

范例2:对于ReadOnlyBufferException

// 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); 
  
            // 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; 
  
            // offset within this buffer's array 
            // of the first element of the buffer 
            // using arrayOffset() method 
            int offset = buffer.arrayOffset(); 
  
            // print the array 
            System.out.println("arrayOffset is : "
                               + offset); 
        } 
  
        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#arrayOffset-



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Buffer arrayOffset() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。