Java ShortBuffer isDirect()方法及示例
java.nio.ShortBuffer 的 isDirect() 方法用于检查这个短缓冲区是否是直接的。
语法:
public abstract boolean isDirect()
返回值 :当且仅当该缓冲区是直接的,该方法返回真。
下面的程序说明了 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.
程序2 :
// 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-