Open In App

java.nio.CharBuffer Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

CharBuffer holds a sequence of integer values to be used in an I/O operation. The CharBuffer class provides the following four categories of operations upon long buffers:

  • Absolute and relative get method that read single Chars.
  • Absolute and relative put methods that write single Chars.
  • Relative bulk put and get methods that transfer contiguous sequences of Chars from an int array or some other Chars 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 CharBuffer class are directly analogous to the methods defined by ByteBuffer.

Class Declaration:

public abstract class CharBuffer

extends Buffer

implements Comparable<CharBuffer>, Appendable, CharSequence, Readable

Methods of CharBuffer class:

Method Description
allocate(int capacity) This method allocates a new char buffer.
append(char c) This method appends the specified char to this buffer.
append(CharSequence csq) This method appends the specified character sequence to this buffer.
append(CharSequence csq, int start, int end) This method appends a subsequence of the specified character sequence to this buffer 
array() This method returns the char array that backs this buffer
arrayOffset() This method returns the offset within this buffer’s backing array of the first element of the buffer
asReadOnlyBuffer() This method creates a new, read-only char buffer that shares this buffer’s content.
charAt(int index) This method reads the character at the given index relative to the current position.
compact() This method compacts this buffer 
compareTo(CharBuffer that) This method compares this buffer to another.
duplicate() This method creates a new char 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
get(char[] dst) This method is a relative bulk get method and returns
get(char[] dst, int offset, int length) This method is a relative bulk get method and returns
get(int index) This method is an absolute get method and returns
hasArray() This method tells whether this buffer is backed by an accessible char array.
hashCode() This method returns the current hash code of this buffer.
isDirect() This method tells whether this char buffer is direct.
length() This method returns the length of this character buffer.
order() This method retrieves this buffer’s byte order.
put(char c) This method is a relative put method and returns
put(char[] src) This method is a relative bulk put method and returns
put(char[] src, int offset, int length) This method is a relative bulk put method and returns
put(CharBuffer src) This method is a relative bulk put method and returns
put(int index, char c) This method is an absolute put method and returns
put(String src) This method is a relative bulk put method and returns
put(String src, int start, int end) This method is a relative bulk put method and returns
read(CharBuffer target) This method attempts to read characters into the specified character buffer.
slice() This method creates a new char buffer whose content is a shared subsequence of this buffer’s content.
subSequence(int start, int end) This method creates a new character buffer that represents the specified subsequence of this buffer, relative to the current position.
toString() This method returns a string containing the characters in this buffer.
wrap(char[] array) This method wraps a char array into a buffer.
wrap(char[] array, int offset, int length) This method wraps a char array into a buffer.
wrap(CharSequence csq) This method wraps a character sequence into a buffer.
wrap(CharSequence csq, int start, int end) This method wraps a character sequence into a buffer.

Following are some programs to demonstrate CharBuffer class and its methods:

Example 1:

Java




// Java program to demonstrate
// CharBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the CharBuffer
        char capacity = 10;
  
        // Creating the CharBuffer
  
        // creating object of Charbuffer
        // and allocating size capacity
        CharBuffer fb = CharBuffer.allocate(capacity);
  
        // putting the value in charbuffer
        fb.put('a');
        fb.put(5, 'b');
          fb.put(7, 'c');
  
        // Printing the CharBuffer
        System.out.println("ChartBuffer: "
                        + Arrays.toString(fb.array()));
    }
}


Output:

ChartBuffer: [a, , , , , b, , c, , ]

Example 2:

Java




// Java program to demonstrate
// CharBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the CharBuffer
        int capacity = 4;
  
        // Creating the CharBuffer
        try {
  
            // creating object of CharBuffer
            // and allocating size capacity
            CharBuffer charbuffer
                = CharBuffer.allocate(capacity);
  
            // append the value in CharBuffer
            // using append() method
            charbuffer.append('a')
                .append('b')
                .append('c')
                  .append('d')
                .rewind();
  
            // print the CharBuffer
            System.out.println("Original CharBuffer: "
                            + Arrays.toString(
                                    charbuffer.array()));
        }
  
        catch (BufferOverflowException e) {
  
            System.out.println("Exception throws : " + e);
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("Exception throws : " + e);
        }
    }
}


Output:

Original CharBuffer: [a, b, c, d]


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