Java Buffer isDirect()方法及示例
java.nio.Buffer类 的 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);
// Typecast byteBuffer to buffer
Buffer buffer = (Buffer)byteBuffer;
// check the Buffer
// using isDirect() method
boolean val = buffer.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);
// Typecast byteBuffer to buffer
Buffer buffer = (Buffer)byteBuffer;
// check the byteBuffer
// using isDirect() method
boolean val = buffer.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/Buffer.html#isDirect-