The allocateDirect() method of java.nio.ByteBuffer class is used Allocates 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 allocate() method.
Syntax:
public static ByteBuffer allocateDirect(int capacity)
Parameters: This method takes capacity, in bytes, as parameter.
Return Value: This method returns the new byte buffer.
Exception: This method throws IllegalArgumentException, If the capacity is a negative integer.
Below are the examples to illustrate the allocateDirect() method:
Examples 1:
// Java program to demonstrate // allocateDirect() method import java.nio.*; import java.util.*; 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 = ByteBuffer.wrap(value); // print the ByteBuffer System.out.println("Direct ByteBuffer: " + Arrays.toString(bb.array())); // 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); } } } |
Direct ByteBuffer: [20, 30, 40, 50] State of the ByteBuffer : java.nio.HeapByteBuffer[pos=0 lim=4 cap=4]
Examples 2: To show IllegalArgumentException
// 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 = ByteBuffer.wrap(value); // print the ByteBuffer System.out.println("Direct ByteBuffer: " + Arrays.toString(bb.array())); // 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); } } } |
Trying to allocate negative value in ByteBuffer Exception thrown : java.lang.IllegalArgumentException: Negative capacity: -4
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- ByteBuffer compareTo() method in Java With Examples
- ByteBuffer allocate() method in Java with Examples
- ByteBuffer asCharBuffer() method in Java with Examples
- ByteBuffer array() method in Java with Examples
- ByteBuffer asFloatBuffer() method in Java with Examples
- ByteBuffer asDoubleBuffer() method in Java with Examples
- ByteBuffer asIntBuffer() method in Java with Examples
- ByteBuffer arrayOffset() method in Java with Examples
- ByteBuffer asLongBuffer() method in Java with Examples
- ByteBuffer asShortBuffer() method in Java with Examples
- ByteBuffer asReadOnlyBuffer() method in Java with Examples
- ByteBuffer compact() method in Java with Examples
- ByteBuffer duplicate() method in Java with Examples
- ByteBuffer equals() method in Java with Examples
- ByteBuffer get() method in Java with Examples
- ByteBuffer getChar() method in Java with Examples
- ByteBuffer getFloat() method in Java with Examples
- ByteBuffer getDouble() method in Java with Examples
- ByteBuffer getShort() method in Java with Examples
- ByteBuffer getLong() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

