Open In App

ByteBuffer allocateDirect() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The allocateDirect() method of java.nio.ByteBuffer class is used to Allocate a new direct byte buffer. The new buffer’s position will be zero, its limit will be its capacity, its mark will be undefined, and each of its elements will be initialized to zero. Whether or not it has a backing array is unspecified. This method is 25%-75% faster than the allocate() method.

Syntax:

public static ByteBuffer allocateDirect(int capacity)

Parameters: This method takes capacity, in bytes, as a parameter.
Return Value: This method returns the new byte buffer.
Exception: This method throws IllegalArgumentException If the capacity is a negative integer.

Program for ByteBuffer allocateDirect() method in Java

Example 1:

Below is the implemented ByteBuffer allocateDirect() method :

Java
// Java program to demonstrate 
// allocateDirect() method 
import java.nio.*; 
import java.util.*; 

// Driver Class
public class GFG { 
    public static void main(String[] args) 
    { 
        // Declaring the capacity of the ByteBuffer 
        int capacity = 4; 

        // Creating the ByteBuffer 
        try { 

            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocateDirect(capacity); 

            // creating byte array of size capacity 
            byte[] value = { 20, 30, 40, 50 }; 

            // wrap the byte array into ByteBuffer 
            bb.put(value);

            // print the state of the buffer 
            System.out.print("\nState of the ByteBuffer : "); 
            System.out.println(bb.toString()); 
        } 

        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown : " + e); 
        } 

        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
} 

Output
State of the ByteBuffer : java.nio.DirectByteBuffer[pos=4 lim=4 cap=4]

Examples 2:

Below is the implementation of ByteBuffer allocateDirect() method in Java to show IllegalArgumentException

Java
// Java program to demonstrate 
// allocateDirect() method 

import java.nio.*; 
import java.util.*; 

public class GFG { 
    public static void main(String[] args) 
    { 
        // Declaring the capacity 
        // with negative value 
        int capacity = -4; 

        // Creating the ByteBuffer 
        try { 
            // creating object of ByteBuffer 
            // and allocating size capacity 

            System.out.println("Trying to allocate"
                            + " negative value in ByteBuffer"); 
          
            ByteBuffer bb = ByteBuffer.allocateDirect(capacity); 

            // creating byte array of size capacity 
            byte[] value = { 20, 30, 40, 50 }; 

            // wrap the byte array into ByteBuffer 
            bb.put(value);  

            // print the state of the buffer 
            System.out.print("\nState of the ByteBuffer : "); 
            System.out.println(bb.toString()); 
        } 

        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown : " + e); 
        } 

        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
} 

Output
Trying to allocate negative value in ByteBuffer
Exception thrown : java.lang.IllegalArgumentException: capacity < 0: (-4 < 0)


Last Updated : 20 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads