Java ByteBuffer hasArray()方法及示例

Java ByteBuffer hasArray()方法及示例

java.nio.ByteBuffer 类的 hasArray() 方法是用来确保给定的缓冲区是否有一个可访问的字节数组作为支持。如果这个缓冲区有一个可访问的支持数组,它就返回true,否则就返回false。如果该方法返回true,那么可以安全地调用array()和arrayOffset()方法,因为它们在支持数组上工作。

语法

public final 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();
  
            // checking FloatBuffer fb is backed by array or not
            boolean isArray = bb.hasArray();
  
            // checking if else condition
            if (isArray)
                System.out.println("ByteBuffer bb is"
                                   + " backed by array");
            else
                System.out.println("ByteBuffer bb is"
                                   + " not backed by any array");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出。

ByteBuffer bb 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();
  
            // checking ByteBuffer bb is backed by array or not
            boolean isArray = bb1.hasArray();
  
            // checking if else condition
            if (isArray)
                System.out.println("ByteBuffer bb is"
                                   + " backed by array");
            else
                System.out.println("ByteBuffer bb is"
                                   + " not backed by any array");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出。

ByteBuffer bb is not backed by any array

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程