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
,position The index of the next element of the buffer that will be read or written ,limit The first index of the buffer that should not be read or written. , andcapacity The number of elements a buffer contains values of the read-only copy will be identical to that ofmark A recorded position of a buffer 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,
ReadOnlyBufferExceptionis thrown.
Declaration
The DoubleBuffer.asReadOnlyBuffer() method is declared as follows:
buff.asReadOnlyBuffer()
buff: TheDoubleBufferwhose read-only copy will be created.
Return value
The DoubleBuffer.asReadOnlyBuffer() method returns a read-only copy of buff.
Note: If
buffis read-only, theDoubleBuffer.asReadOnlyBuffer()method behaves in the same way as theDoubleBuffer.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
DoubleBufferbuff1is declared in line 7 with the capacityn1 = 6. - Six elements are added to
buff1using theput()method in lines 8-13. - The position of
buff1is set to2using theposition()method in line 15. - The limit of
buff1is set to5using thelimit()method in line 16. - A
DoubleBufferbuff2is declared in line 22 that is the read-only copy ofbuff1. The read-only copybuff2is created using theDoubleBuffer.asReadOnlyBuffer()method. - The
DoubleBuffer.equals()method is used in line 24 to check ifbuff1is equal to its read-only copybuff2. TheDoubleBuffer.equals()method returnstrue, which means thatbuff1is equal tobuff2.
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
DoubleBufferbuff1is declared in line 7 with the capacityn1 = 6. - Six elements are added to
buff1using theput()method in lines 8-13. - The position of
buff1is set to2using theposition()method in line 15. - The limit of
buff1is set to5using thelimit()method in line 16. - A
DoubleBufferbuff2is declared in line 22 that is the read-only copy ofbuff1. The read-only copybuff2is created using theDoubleBuffer.asReadOnlyBuffer()method. - The
DoubleBuffer.put()method is used in line 24 to try writing a value tobuff2.ReadOnlyBufferExceptionis thrown becausebuff2is read-only and cannot be modified.
Free Resources