Open In App

Buffer isDirect() methods in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The isDirect() method of java.nio.Buffer Class is used to tell whether or not this buffer is direct.

Syntax:

public abstract boolean isDirect()

Return Value: This method returns true if, and only if, this buffer is direct.

Below are the examples to illustrate the isDirect() method:

Examples 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");
    }
}


Output:

buffer is direct

Examples 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");
    }
}


Output:

buffer is not direct

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#isDirect–



Last Updated : 16 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads