java.nio.ByteBuffer Class in Java

29 Mar, 2021
ByteBuffer holds a sequence of integer values to be used in an I/O operation. The ByteBuffer class provides the following four categories of operations upon long buffers:
Absolute and relative get method that read single bytes.
Absolute and relative put methods that write single bytes.
Relative bulk put and get methods that transfer contiguous sequences of bytes from an int array or some other bytes buffer into this buffer and from this buffer into an array.
Short buffers can be created by:
allocate(): This allocates space for the buffer’s content.
wrap(): This wraps an existing long array into a buffer.
Most of the methods of ByteBuffer class are directly analogous to the methods defined by ByteBuffer.
Class Declaration:
public abstract class ByteBuffer
extends Buffer
implements Comparable<ByteBuffer>
Methods of CharBuffer class:
Following are some programs to demonstrate CharBuffer class and its methods:
Example 1:
Java
// Java program to demonstrate// ByteBuffer class  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)10);            bb.put((byte)20);            bb.put((byte)30);            bb.put((byte)40);            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: [10, 20, 30, 40]

Example 2:
Java
// Java program to demonstrate// ByteBuffer class  import java.nio.*;import java.util.*;  public class GFG {      public static void main(String[] args)    {          // Declaring the capacity of the ByteBuffer        int capacity = 50;          // Creating the ByteBuffer        try {              // creating object of ByteBuffer            // and allocating size capacity            ByteBuffer bb = ByteBuffer.allocate(capacity);              // changeing bytebuffer view as char buffer            // and putting a string value            bb.asCharBuffer().put("GeeksForGeeks");              // Declaring char variable c            char c;              // print the ByteBuffer            System.out.print("Char buffer : ");            while ((c = bb.getChar()) != 0)                System.out.print(c + " ");            System.out.println();        }          catch (IllegalArgumentException e) {              System.out.println("Exception thrown : " + e);        }          catch (ReadOnlyBufferException e) {              System.out.println("Exception thrown : " + e);        }    }}
Char buffer : G e e k s F o r G e e k s 

Java-ByteBuffer
Java-NIO package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java