The asReadOnlyBuffer() method of java.nio.ByteBuffer class is used to create a new, read-only byte buffer that shares this buffer’s content.
The content of the new buffer will be that of this buffer. Changes to this buffer’s content will be visible in the new buffer; the new buffer itself, however, will be read-only and will not allow the shared content to be modified. 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 ByteBuffer asReadOnlyBuffer()
Return Value: This method returns the new, read-only byte buffer with the same content as that of this buffer.
Below are the examples to illustrate the asReadOnlyBuffer() method:
Examples 1:
// 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 ByteBuffer int capacity = 4 ; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the int to byte typecast value in ByteBuffer bb.put(( byte ) 20 ); bb.put(( byte ) 30 ); bb.put(( byte ) 40 ); bb.put(( byte ) 50 ); bb.rewind(); // print the ByteBuffer System.out.println( "Original ByteBuffer: " + Arrays.toString(bb.array())); // Creating a read-only copy of ByteBuffer // using asReadOnlyBuffer() method ByteBuffer bb1 = bb.asReadOnlyBuffer(); // print the ByteBuffer System.out.print( "\nReadOnlyBuffer ByteBuffer: " ); while (bb1.hasRemaining()) System.out.print(bb1.get() + ", " ); } catch (IllegalArgumentException e) { System.out.println( "Exception thrown : " + e); } catch (ReadOnlyBufferException e) { System.out.println( "Exception thrown : " + e); } } } |
Original ByteBuffer: [20, 30, 40, 50] ReadOnlyBuffer ByteBuffer: 20, 30, 40, 50,
Examples 2:
// 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 ByteBuffer int capacity = 4 ; // Creating the ByteBuffer try { // creating object of ByteBuffer // and allocating size capacity ByteBuffer bb = ByteBuffer.allocate(capacity); // putting the int to byte typecast value in ByteBuffer bb.put(( byte ) 20 ); bb.put(( byte ) 30 ); bb.put(( byte ) 40 ); bb.put(( byte ) 50 ); bb.rewind(); // print the ByteBuffer System.out.println( "Original ByteBuffer: " + Arrays.toString(bb.array())); // Creating a read-only copy of ByteBuffer // using asReadOnlyBuffer() method ByteBuffer bb1 = bb.asReadOnlyBuffer(); // print the ByteBuffer System.out.print( "\nReadOnlyBuffer ByteBuffer: " ); while (bb1.hasRemaining()) System.out.print(bb1.get() + ", " ); // try to change read only bytebuffer System.out.println( "\n\nTrying to get the array" + " from bb1 for editing" ); byte [] bbarr = bb1.array(); } catch (IllegalArgumentException e) { System.out.println( "Exception thrown : " + e); } catch (ReadOnlyBufferException e) { System.out.println( "Exception thrown : " + e); } } } |
Original ByteBuffer: [20, 30, 40, 50] ReadOnlyBuffer ByteBuffer: 20, 30, 40, 50, Trying to get the array from bb1 for editing Exception thrown : java.nio.ReadOnlyBufferException