CharBuffer asReadOnlyBuffer() method in Java

The asReadOnlyBuffer() method of java.nio.CharBuffer Class is used to create a new, read-only char buffer with this buffer’s content. The new buffer is a replica of this buffer. Hence changes made to this buffer’s content will be visible in the new buffer.

Since the new buffer is read-only, therefore any modification to its content won’t be allowed. The two buffers’ position, limit, and mark values will be independent. The new buffer’s capacity, limit, position, and mark values will be identical to those of this buffer. If this buffer is itself read-only then this method behaves in exactly the same way as the duplicate method.

Syntax :

public abstract CharBuffer asReadOnlyBuffer()

Return Value: This method returns the new, read-only char buffer with the same content as that of this buffer.

Below are the examples to illustrate the asReadOnlyBuffer() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the CharBuffer
        int capacity = 10;
  
        // Creating the CharBuffer
        try {
  
            // creating object of charbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
  
            // putting the value in charbuffer
            cb.put('a');
            cb.put(2, 'b');
            cb.rewind();
  
            // print the charBuffer
            System.out.println("Original CharBuffer:  "
                               + Arrays.toString(cb.array()));
  
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer charBuffer = cb.asReadOnlyBuffer();
  
            // print the CharBuffer
            System.out.print("\nReadOnlyBuffer CharBuffer: ");
  
            while (charBuffer.hasRemaining())
                System.out.print(charBuffer.get() + ", ");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

chevron_right


Output:

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

ReadOnlyBuffer CharBuffer: a, , b, , , , , , , ,

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args) throws Exception
    {
  
        // Declaring the capacity of the cb
        int capacity1 = 10;
  
        // Declaring the capacity of the cb1
        int capacity2 = 5;
  
        // Creating the CharBuffer
        try {
  
            //
            // cb
            //
            // creating object of charbuffer cb
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity1);
  
            // putting the value in cb
            cb.put('a');
            cb.put(2, 'b');
            cb.rewind();
  
            // print the CharBuffer
            System.out.println("CharBuffer cb: "
                               + Arrays.toString(cb.array()));
  
            //
            // cb1
            //
            // creating object of charbuffer cb1
            // and allocating size capacity
            CharBuffer cb1 = CharBuffer.allocate(capacity2);
  
            // putting the value in cb1
            cb1.put(1, 'c');
            cb1.put(2, 'd');
            cb1.rewind();
  
            // print the CharBuffer
            System.out.println("\nCharBuffer cb1: "
                               + Arrays.toString(cb1.array()));
  
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer readOnlyCb = cb.asReadOnlyBuffer();
  
            // print the CharBuffer
            System.out.print("\nReadOnlyBuffer CharBuffer: ");
  
            while (readOnlyCb.hasRemaining())
                System.out.print(readOnlyCb.get() + ", ");
  
            // try to change readOnlyCb
            System.out.println("\n\nTrying to get the array"
                               + " from ReadOnlyCb for editing");
  
            char[] fbarr = readOnlyCb.array();
        }
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
        catch (ReadOnlyBufferException e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

CharBuffer cb: [a, , b, , , , , , , ]

CharBuffer cb1: [, c, d, , ]

ReadOnlyBuffer CharBuffer: a, , b, , , , , , , , 

Trying to get the array from ReadOnlyCb for editing
Exception thrown: java.nio.ReadOnlyBufferException


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.