import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 4;
try {
ByteBuffer bb = ByteBuffer.allocate(capacity);
bb.put((byte)20);
bb.put((byte)30);
bb.put((byte)40);
bb.put((byte)50);
bb.rewind();
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
ByteBuffer bb1 = bb.asReadOnlyBuffer();
bb1.rewind();
System.out.print("\nReadOnlyBuffer ByteBuffer : ");
while (bb1.hasRemaining())
System.out.print(bb1.get() + ", ");
System.out.println("\n\nTrying to get the array"
+ " from bb1 for editing");
byte[] arr = bb1.array();
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws: " + e);
}
}
}