📌  相关文章
📜  Java中的 ShortBuffer isDirect() 方法及示例

📅  最后修改于: 2022-05-13 01:54:48.641000             🧑  作者: Mango

Java中的 ShortBuffer isDirect() 方法及示例

Java.nio.ShortBufferisDirect()方法用于检查这个短缓冲区是否是直接的。

语法

public abstract boolean isDirect()

返回值:当且仅当此缓冲区是直接的,该方法才返回true

下面的程序说明了isDirect()方法的使用:

程序 1

// Java program to demonstrate
// isDirect() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        short[] array = { 10000, 10640, 10189, -2000, -16780 };
  
        // creating short array
        ShortBuffer shortBuf1 = ShortBuffer.wrap(array);
  
        // checking if the array is Direct or not
        if (shortBuf1.isDirect()) {
            System.out.println("Short buffer is direct.");
        }
        else {
            System.out.println("Short buffer is not direct.");
        }
    }
}
输出:
Short buffer is not direct.

方案二

// Java program to demonstrate
// isDirect() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        ByteBuffer b = ByteBuffer.allocateDirect(512);
  
        ShortBuffer shortBuf = b.asShortBuffer();
  
        // checking if the array is Direct or not
        if (shortBuf.isDirect()) {
            System.out.println("Short buffer is direct.");
        }
        else {
            System.out.println("Short buffer is not direct.");
        }
    }
}
输出:
Short buffer is direct.

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/ShortBuffer.html#isDirect–