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:
buff
.buff
.ReadOnlyBufferException
is thrown.The DoubleBuffer.asReadOnlyBuffer()
method is declared as follows:
buff.asReadOnlyBuffer()
buff
: The DoubleBuffer
whose read-only copy will be created.The DoubleBuffer.asReadOnlyBuffer()
method returns a read-only copy of buff
.
Note: If
buff
is read-only, theDoubleBuffer.asReadOnlyBuffer()
method behaves in the same way as theDoubleBuffer.duplicate()
method.
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");}}}
DoubleBuffer
buff1
is declared in line 7 with the capacity n1 = 6
.buff1
using the put()
method in lines 8-13.buff1
is set to 2
using the position()
method in line 15.buff1
is set to 5
using the limit()
method in line 16.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.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
.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");}}}
DoubleBuffer
buff1
is declared in line 7 with the capacity n1 = 6
.buff1
using the put()
method in lines 8-13.buff1
is set to 2
using the position()
method in line 15.buff1
is set to 5
using the limit()
method in line 16.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.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