ByteBuffer allocate() method in Java with Examples - GeeksforGeeks

25 Jul, 2019
The allocate() method of java.nio.ByteBuffer class is used to allocate a new 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. It will have a backing array, and its array offset will be zero.
Syntax :
public static ByteBuffer allocate(int capacity)
Parameters: This method takes capacity, in bytes as parameter.
Return Value: This method returns the new byte buffer .
Throws: This method throws IllegalArgumentException, If the capacity is a negative integer
Below are the examples to illustrate the allocate() method:
Examples 1:
// Java program to demonstrate// allocate() 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.allocate(capacity);              // putting the int to byte typecast value            // in ByteBuffer using putInt() method            bb.put((byte)20);            bb.put((byte)30);            bb.put((byte)40);            bb.put((byte)50);            bb.rewind();              // print the ByteBuffer            System.out.println("Original ByteBuffer:  "                               + Arrays.toString(bb.array()));        }          catch (IllegalArgumentException e) {              System.out.println("IllegalArgumentException catched");        }          catch (ReadOnlyBufferException e) {              System.out.println("ReadOnlyBufferException catched");        }    }}
Original ByteBuffer:  [20, 30, 40, 50]

Examples 2:
// Java program to demonstrate// allocate() method  import java.nio.*;import java.util.*;  public class GFG {      public static void main(String[] args)    {          // Declaring the capacity with negative        // value of the ByteBuffer        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.allocate(capacity);              // putting int to byte typecast value            // in ByteBuffer using putInt() method            bb.put((byte)20);            bb.put((byte)30);            bb.put((byte)40);            bb.put((byte)50);            bb.rewind();              // print the ByteBuffer            System.out.println("Original ByteBuffer:  "                               + Arrays.toString(bb.array()));        }          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

nidhi_biet
Java-ByteBuffer
Java-Functions
Java-NIO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Stream In Java
Singleton Class in Java