Java中的 ByteBuffer isDirect() 方法及示例
Java.nio.ByteBuffer 类的isDirect()方法用于判断这个字节缓冲区是否是直接的。
句法:
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)
{
// defining and allocating ByteBuffer
// using allocate() method
ByteBuffer byteBuffer
= ByteBuffer.allocateDirect(4);
// check the byteBuffer
// using isDirect() method
boolean val = byteBuffer.isDirect();
// checking the condition
if (val)
System.out.println("buffer is direct");
else
System.out.println("buffer is not direct");
}
}
输出:
buffer is direct
示例 2:
// Java program to demonstrate
// isDirect() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating ByteBuffer
// using allocate() method
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
// check the byteBuffer
// using isDirect() method
boolean val = byteBuffer.isDirect();
// checking the condition
if (val)
System.out.println("buffer is direct");
else
System.out.println("buffer is not direct");
}
}
输出:
buffer is not direct
参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/ByteBuffer.html#isDirect–