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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。