Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the CharBuffer asReadOnlyBuffer() method in Java?

Syed Muhammad Ali Mustafa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

java.nio.CharBuffer is a class that we can use to store a buffer of characters. The asReadOnlyBuffer() method of the class java.nio.CharBuffer creates a read-only copy of a buffer.

Creating a read-only copy of a buffer buff means:

  • The read-only copy has the same elements as buff.
  • The positionThe index of the next element of the buffer that will be read or written, limitThe first index of the buffer that should not be read or written, capacityThe number of elements a buffer contains, and markA recorded position of a buffer values of the read-only copy will be identical to that of buff.
  • Any modifications to the content of the read-only copy will not be allowed. If a modification is tried on a read-only buffer, the ReadOnlyBufferException is thrown.

Declaration

The CharBuffer.asReadOnlyBuffer() method is declared as follows:

buff.asReadOnlyBuffer()
  • buff: The CharBuffer whose read-only copy is created.

Return value

The CharBuffer.asReadOnlyBuffer() method returns a read-only copy of buff.

Note: If buff is read-only, the CharBuffer.asReadOnlyBuffer() method behaves in the same way as the CharBuffer.duplicate() method.

Code

Example 1

Consider the code snippet below, which demonstrates the use of the CharBuffer.asReadOnlyBuffer() method.

import java.nio.*;
import java.util.*;
public class main {
   public static void main(String[] args) {
      int n1 = 6;
      try {
         CharBuffer buff1 = CharBuffer.allocate(n1);
         buff1.put('a');
         buff1.put('b');
         buff1.put('c');
         buff1.put('d');
         buff1.put('e');
         buff1.put('f');

         buff1.position(2);
         buff1.limit(5);

         System.out.println("buff1: " + Arrays.toString(buff1.array()));
         System.out.println("position at: " + buff1.position());
         System.out.println("Limit at: " + buff1.limit());

         CharBuffer buff2 = buff1.asReadOnlyBuffer();

         boolean foo = buff2.equals(buff1);
         System.out.println("buff2 is equal to buff1: " + foo);
         
      } catch (IllegalArgumentException e) {
         System.out.println("Error!!! IllegalArgumentException");
      } catch (ReadOnlyBufferException e) {
         System.out.println("Error!!! ReadOnlyBufferException");
      } 
   }
}

Explanation

  • A CharBuffer buff1 is declared in line 7 with the capacity n1 = 6.
  • Six elements are added to buff1 using the put() method in lines 8-13.
  • The position of buff1 is set to 2 using the position() method in line 15.
  • The limit of buff1 is set to 5 using the limit() method in line 16.
  • A CharBuffer buff2 is declared in line 22 that is the read-only copy of buff1. The read-only copy buff2 is created using the CharBuffer.asReadOnlyBuffer() method.
  • The CharBuffer.equals() method is used in line 24 to check if buff1 is equal to its read-only copy buff2. The CharBuffer.equals() method returns true which means that buff1 is equal to buff2.

Example 2

As explained above, any modification on a read-only buffer throws the ReadOnlyBufferException. Consider the code snippet below, in which a read-only buffer is modified.

import java.nio.*;
import java.util.*;
public class main {
   public static void main(String[] args) {
      int n1 = 6;
      try {
         CharBuffer buff1 = CharBuffer.allocate(n1);
         buff1.put('a');
         buff1.put('b');
         buff1.put('c');
         buff1.put('d');
         buff1.put('e');
         buff1.put('f');

         buff1.position(2);
         buff1.limit(5);

         System.out.println("buff1: " + Arrays.toString(buff1.array()));
         System.out.println("position at: " + buff1.position());
         System.out.println("Limit at: " + buff1.limit());

         CharBuffer buff2 = buff1.asReadOnlyBuffer();

         buff2.put('f');
         
      } catch (IllegalArgumentException e) {
         System.out.println("Error!!! IllegalArgumentException");
      } catch (ReadOnlyBufferException e) {
         System.out.println("Error!!! ReadOnlyBufferException");
      } 
   }
}

Explanation

  • A CharBuffer buff1 is declared in line 7 with the capacity n1 = 6.
  • Six elements are added to buff1 using the put() method in lines 8-13.
  • The position of buff1 is set to 2 using the position() method in line 15.
  • The limit of buff1 is set to 5 using the limit() method in line 16.
  • A CharBuffer buff2 is declared in line 22 that is the read-only copy of buff1. The read-only copy buff2 is created using the CharBuffer.asReadOnlyBuffer() method.
  • The CharBuffer.put() method is used in line 24 which tries writing a value to buff2. The ReadOnlyBufferException is thrown because buff2 is read-only and cannot be modified.

RELATED TAGS

java

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring