What is the DoubleBuffer asReadOnlyBuffer() method in Java?

java.nio.DoubleBuffer is a class that is used to store a buffer of doubles. The asReadOnlyBuffer() method of the class java.nio.DoubleBuffer is used to create 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, ReadOnlyBufferException is thrown.

Declaration

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

buff.asReadOnlyBuffer()
  • buff: The DoubleBuffer whose read-only copy will be created.

Return value

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

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

Examples

Example 1

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

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 6;
try {
DoubleBuffer buff1 = DoubleBuffer.allocate(n1);
buff1.put(1.3);
buff1.put(2.8);
buff1.put(3.2);
buff1.put(4.5);
buff1.put(5.2);
buff1.put(6.6);
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());
DoubleBuffer 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 DoubleBuffer 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 DoubleBuffer buff2 is declared in line 22 that is the read-only copy of buff1. The read-only copy buff2 is created using the DoubleBuffer.asReadOnlyBuffer() method.
  • The DoubleBuffer.equals() method is used in line 24 to check if buff1 is equal to its read-only copy buff2. The DoubleBuffer.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 {
DoubleBuffer buff1 = DoubleBuffer.allocate(n1);
buff1.put(1.3);
buff1.put(2.8);
buff1.put(3.2);
buff1.put(4.5);
buff1.put(5.2);
buff1.put(6.6);
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());
DoubleBuffer buff2 = buff1.asReadOnlyBuffer();
buff2.put(6.6);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A DoubleBuffer 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 DoubleBuffer buff2 is declared in line 22 that is the read-only copy of buff1. The read-only copy buff2 is created using the DoubleBuffer.asReadOnlyBuffer() method.
  • The DoubleBuffer.put() method is used in line 24 to try writing a value to buff2. ReadOnlyBufferException is thrown because buff2 is read-only and cannot be modified.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved