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


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


java.nio.Buffer类的hasArray()方法用于告诉此缓冲区是否由可访问数组支持。如果此方法返回true,则可以安全地调用array和arrayOffset()方法。

用法:

public abstract boolean hasArray()

返回值:当且仅当此缓冲区由数组支持并且不是只读时,此方法才会返回true。否则返回false。


下面是说明hasArray()方法的示例:

范例1:当缓冲区由数组支持时

// Java program to demonstrate 
// hasArray() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 10; 
  
        // 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)10); 
            bb.put((byte)20); 
            bb.rewind(); 
  
            // Typecast bytebuffer to Buffer 
            Buffer buffer = (Buffer)bb; 
  
            // checking buffer is backed by array or not 
            boolean isArray = buffer.hasArray(); 
  
            // checking if else condition 
            if (isArray) 
                System.out.println("buffer is"
                                   + " backed by array"); 
            else
                System.out.println("buffer is"
                                   + " not backed by any array"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
buffer is backed by array

范例2:当缓冲区不由数组支持时

// Java program to demonstrate 
// hasArray() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 10; 
  
        // 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)8.56F); 
            bb.put((byte)10); 
            bb.rewind(); 
  
            // Creating a read-only copy of ByteBuffer 
            // using asReadOnlyBuffer() method 
            ByteBuffer bb1 = bb.asReadOnlyBuffer(); 
  
            // Typecast read-only ByteBuffer to read-only buffer 
            Buffer buffer = (Buffer)bb1; 
  
            // checking Buffer buffer is backed by array or not 
            boolean isArray = buffer.hasArray(); 
  
            // checking if else condition 
            if (isArray) 
                System.out.println("buffer is"
                                   + " backed by array"); 
            else
                System.out.println("buffer is"
                                   + " not backed by any array"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
buffer is not backed by any array

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



相关用法


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