Open In App

java.nio.ByteBuffer Class in Java

Last Updated : 29 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Method Description
allocate(int capacity) This method allocates a new byte buffer.
allocateDirect(int capacity) This method allocates a new direct byte buffer.
array() This method returns the byte array that backs this buffer
arrayOffset() This method returns the offset within this buffer’s backing array of the first element of the buffer. 
asCharBuffer() This method creates a view of this byte buffer as a char buffer.
asDoubleBuffer() This method creates a view of this byte buffer as a double buffer.
asFloatBuffer() This method creates a view of this byte buffer as a float buffer.
asIntBuffer() This method creates a view of this byte buffer as an int buffer.
asLongBuffer() This method creates a view of this byte buffer as a long buffer.
asReadOnlyBuffer() This method creates a new, read-only byte buffer that shares this buffer’s content.
asShortBuffer() This method creates a view of this byte buffer as a short buffer.
compact() This method compacts this buffer.
compareTo(ByteBuffer that) This method compares this buffer to another.
duplicate() This method creates a new byte buffer that shares this buffer’s content.
equals(Object ob) This method tells whether this buffer is equal to another object.
get() This method is a relative get method and returns the byte at the buffer’s current position.
get(byte[] dst) This method is a relative bulk get method and returns this buffer. 
get(byte[] dst, int offset, int length) This method is a Relative bulk get method and returns this buffer.
get(int index) This method is an Absolute get method and returns the byte at the given index.
getChar() This method is a Relative get method for reading a char value and returns the char value at the buffer’s current position.
getChar(int index) This method is an Absolute get method for reading a char value and returns the char value at the given index.
getDouble() This method is a Relative get method for reading a double value and returns the double value at the buffer’s current position.
getDouble(int index) This method is an Absolute get method for reading a double value and returns the double value at the given index.
getFloat() This method is a Relative get method for reading a float value and returns the float value at the buffer’s current position.
getFloat(int index) This method is an Absolute get method for reading a float value and returns the float value at the given index.
getInt() This method is a Relative get method for reading an int value and returns the int value at the buffer’s current position.
getInt(int index) This method is an Absolute get method for reading an int value and returns the int value at the given index.
getLong() This method is a Relative get method for reading a long value and returns the long value at the buffer’s current position.
getLong(int index) This method is an Absolute get method for reading a long value and returns the int value at the given index.
getShort() This method is a Relative get method for reading a short value and returns the long value at the buffer’s current position.
getShort(int index) This method is an Absolute get method for reading a short value and returns the int value at the given index.
hasArray() This method tells whether this buffer is backed by an accessible byte array.
hashCode() This method returns the current hash code of this buffer.
isDirect() This method tells whether this byte buffer is direct.
order() This method retrieves this buffer’s byte order.
order(ByteOrder bo) This method modifies this buffer’s byte order.
put(byte b) This method is a Relative put method and returns this buffer. 
put(byte[] src) This method is a Relative bulk put method and returns this buffer.
put(ByteBuffer src) This method is a Relative bulk put method and returns this buffer.
put(int index, byte b) This method is an Absolute put method and returns this buffer.
putChar(char value) This method is a Relative put method for writing a char value .
putChar(int index, char value) This method is an absolute put method for writing a char value.
putDouble(double value) This method is a relative put method for writing a double value.
putDouble(int index, double value) This method is an absolute put method for writing a double value.
putFloat(float value) This method is a relative put method for writing a float value.  
putFloat(int index, float value) This method is an absolute put method for writing a float value. 
putInt(int value) This method is a relative put method for writing an int value.
putInt(int index, int value) This method is an absolute put method for writing an int value. 
putLong(int index, long value) This method is an absolute put method for writing a long value.
putLong(long value) This method is a relative put method for writing a long value. 
putShort(int index, short value) This method is an absolute put method for writing a short value. 
putShort(short value) This method is a relative put method for writing a short value. 
slice() This method creates a new byte buffer whose content is a shared subsequence of this buffer’s content.
toString() This method returns a string summarizing the state of this buffer.
wrap(byte[] array) This method wraps a byte array into a buffer.
wrap(byte[] array, int offset, int length) This method wraps a byte array into a buffer.

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


Output

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


Output

Char buffer : G e e k s F o r G e e k s 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads