當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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-



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ByteBuffer isDirect() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。