当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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-



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Buffer isDirect() methods in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。